|
14 | 14 | import org.opensearch.telemetry.IntegrationTestOTelTelemetryPlugin;
|
15 | 15 | import org.opensearch.telemetry.OTelTelemetrySettings;
|
16 | 16 | import org.opensearch.telemetry.TelemetrySettings;
|
| 17 | +import org.opensearch.telemetry.metrics.tags.Tags; |
17 | 18 | import org.opensearch.test.OpenSearchIntegTestCase;
|
18 | 19 | import org.junit.After;
|
19 | 20 |
|
| 21 | +import java.io.Closeable; |
20 | 22 | import java.util.ArrayList;
|
21 | 23 | import java.util.Arrays;
|
22 | 24 | import java.util.Collection;
|
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | +import java.util.function.Supplier; |
23 | 28 | import java.util.stream.Collectors;
|
24 | 29 |
|
25 | 30 | import io.opentelemetry.sdk.metrics.data.DoublePointData;
|
| 31 | +import io.opentelemetry.sdk.metrics.data.MetricData; |
26 | 32 | import io.opentelemetry.sdk.metrics.internal.data.ImmutableExponentialHistogramPointData;
|
27 | 33 |
|
28 | 34 | @OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.SUITE, minNumDataNodes = 1)
|
@@ -118,6 +124,41 @@ public void testHistogram() throws Exception {
|
118 | 124 | assertEquals(1.0, histogramPointData.getMin(), 1.0);
|
119 | 125 | }
|
120 | 126 |
|
| 127 | + public void testObservableGauge() throws Exception { |
| 128 | + String metricName = "test-observable-gauge"; |
| 129 | + MetricsRegistry metricsRegistry = internalCluster().getInstance(MetricsRegistry.class); |
| 130 | + InMemorySingletonMetricsExporter.INSTANCE.reset(); |
| 131 | + Tags tags = Tags.create().addTag("test", "integ-test"); |
| 132 | + final AtomicInteger testValue = new AtomicInteger(0); |
| 133 | + Supplier<Double> valueProvider = () -> { return Double.valueOf(testValue.incrementAndGet()); }; |
| 134 | + Closeable gaugeCloseable = metricsRegistry.createGauge(metricName, "test", "ms", valueProvider, tags); |
| 135 | + // Sleep for about 2.2s to wait for metrics to be published. |
| 136 | + Thread.sleep(2200); |
| 137 | + |
| 138 | + InMemorySingletonMetricsExporter exporter = InMemorySingletonMetricsExporter.INSTANCE; |
| 139 | + |
| 140 | + assertEquals(2.0, getMaxObservableGaugeValue(exporter, metricName), 0.0); |
| 141 | + gaugeCloseable.close(); |
| 142 | + double observableGaugeValueAfterStop = getMaxObservableGaugeValue(exporter, metricName); |
| 143 | + |
| 144 | + // Sleep for about 1.2s to wait for metrics to see that closed observableGauge shouldn't execute the callable. |
| 145 | + Thread.sleep(1200); |
| 146 | + assertEquals(observableGaugeValueAfterStop, getMaxObservableGaugeValue(exporter, metricName), 0.0); |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + private static double getMaxObservableGaugeValue(InMemorySingletonMetricsExporter exporter, String metricName) { |
| 151 | + List<MetricData> dataPoints = exporter.getFinishedMetricItems() |
| 152 | + .stream() |
| 153 | + .filter(a -> a.getName().contains(metricName)) |
| 154 | + .collect(Collectors.toList()); |
| 155 | + double totalValue = 0; |
| 156 | + for (MetricData metricData : dataPoints) { |
| 157 | + totalValue = Math.max(totalValue, ((DoublePointData) metricData.getDoubleGaugeData().getPoints().toArray()[0]).getValue()); |
| 158 | + } |
| 159 | + return totalValue; |
| 160 | + } |
| 161 | + |
121 | 162 | @After
|
122 | 163 | public void reset() {
|
123 | 164 | InMemorySingletonMetricsExporter.INSTANCE.reset();
|
|
0 commit comments