|
| 1 | +package io.prometheus.metrics.exporter.opentelemetry; |
| 2 | + |
| 3 | +import com.github.tomakehurst.wiremock.http.Request; |
| 4 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 5 | +import com.github.tomakehurst.wiremock.matching.MatchResult; |
| 6 | +import com.github.tomakehurst.wiremock.matching.ValueMatcher; |
| 7 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 8 | +import io.opentelemetry.api.trace.Span; |
| 9 | +import io.opentelemetry.api.trace.Tracer; |
| 10 | +import io.opentelemetry.context.Scope; |
| 11 | +import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest; |
| 12 | +import io.opentelemetry.proto.metrics.v1.DoubleDataPoint; |
| 13 | +import io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics; |
| 14 | +import io.opentelemetry.proto.metrics.v1.Metric; |
| 15 | +import io.opentelemetry.proto.metrics.v1.ResourceMetrics; |
| 16 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 17 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 18 | +import io.prometheus.metrics.core.metrics.Counter; |
| 19 | +import io.prometheus.metrics.model.registry.PrometheusRegistry; |
| 20 | +import org.awaitility.core.ConditionTimeoutException; |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Rule; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 27 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 28 | +import static org.awaitility.Awaitility.await; |
| 29 | + |
| 30 | +public class ExemplarTest { |
| 31 | + private static final String ENDPOINT_PATH = "/v1/metrics"; |
| 32 | + private static final int TIMEOUT = 3; |
| 33 | + private static final String INSTRUMENTATION_SCOPE_NAME = "testInstrumentationScope"; |
| 34 | + private static final String SPAN_NAME = "test-span"; |
| 35 | + public static final String TEST_COUNTER_NAME = "test_counter"; |
| 36 | + private Counter testCounter; |
| 37 | + private OpenTelemetryExporter openTelemetryExporter; |
| 38 | + @Rule |
| 39 | + public WireMockRule wireMockRule = new WireMockRule(4317); |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + openTelemetryExporter = OpenTelemetryExporter.builder() |
| 44 | + .endpoint("http://localhost:4317") |
| 45 | + .protocol("http/protobuf") |
| 46 | + .intervalSeconds(1) |
| 47 | + .buildAndStart(); |
| 48 | + |
| 49 | + testCounter = Counter.builder() |
| 50 | + .name(TEST_COUNTER_NAME) |
| 51 | + .withExemplars() |
| 52 | + .register(); |
| 53 | + |
| 54 | + wireMockRule.stubFor(post(ENDPOINT_PATH) |
| 55 | + .withHeader("Content-Type", containing("application/x-protobuf")) |
| 56 | + .willReturn(ok() |
| 57 | + .withHeader("Content-Type", "application/json") |
| 58 | + .withBody("{\"partialSuccess\":{}}"))); |
| 59 | + } |
| 60 | + |
| 61 | + @After |
| 62 | + public void tearDown() { |
| 63 | + PrometheusRegistry.defaultRegistry.unregister(testCounter); |
| 64 | + openTelemetryExporter.close(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void sampledExemplarIsForwarded() { |
| 69 | + try (SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder() |
| 70 | + .setSampler(Sampler.alwaysOn()) |
| 71 | + .build()) { |
| 72 | + |
| 73 | + Tracer test = sdkTracerProvider.get(INSTRUMENTATION_SCOPE_NAME); |
| 74 | + Span span = test.spanBuilder(SPAN_NAME) |
| 75 | + .startSpan(); |
| 76 | + try (Scope scope = span.makeCurrent()) { |
| 77 | + testCounter.inc(2); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + await().atMost(TIMEOUT, SECONDS) |
| 83 | + .ignoreException(com.github.tomakehurst.wiremock.client.VerificationException.class) |
| 84 | + .until(() -> { |
| 85 | + verify(postRequestedFor(urlEqualTo(ENDPOINT_PATH)) |
| 86 | + .withHeader("Content-Type", equalTo("application/x-protobuf")) |
| 87 | + .andMatching(getExemplarCountMatcher(1))); |
| 88 | + return true; |
| 89 | + }); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + @Test(expected = ConditionTimeoutException.class) |
| 94 | + public void notSampledExemplarIsNotForwarded() { |
| 95 | + try (SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder() |
| 96 | + .setSampler(Sampler.alwaysOff()) |
| 97 | + .build()) { |
| 98 | + |
| 99 | + Tracer test = sdkTracerProvider.get(INSTRUMENTATION_SCOPE_NAME); |
| 100 | + Span span = test.spanBuilder(SPAN_NAME) |
| 101 | + .startSpan(); |
| 102 | + try (Scope scope = span.makeCurrent()) { |
| 103 | + testCounter.inc(2); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + await().atMost(TIMEOUT, SECONDS) |
| 108 | + .ignoreException(com.github.tomakehurst.wiremock.client.VerificationException.class) |
| 109 | + .until(() -> { |
| 110 | + verify(postRequestedFor(urlEqualTo(ENDPOINT_PATH)) |
| 111 | + .withHeader("Content-Type", equalTo("application/x-protobuf")) |
| 112 | + .andMatching(getExemplarCountMatcher(1))); |
| 113 | + return true; |
| 114 | + }); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + private static ValueMatcher<Request> getExemplarCountMatcher(int expectedCount) { |
| 119 | + return request -> { |
| 120 | + try { |
| 121 | + ExportMetricsServiceRequest exportMetricsServiceRequest = ExportMetricsServiceRequest.parseFrom(request.getBody()); |
| 122 | + for (ResourceMetrics resourceMetrics : exportMetricsServiceRequest.getResourceMetricsList()) { |
| 123 | + for (InstrumentationLibraryMetrics instrumentationLibraryMetrics : resourceMetrics.getInstrumentationLibraryMetricsList()) { |
| 124 | + for (Metric metric : instrumentationLibraryMetrics.getMetricsList()) { |
| 125 | + for (DoubleDataPoint doubleDataPoint : metric.getDoubleSum().getDataPointsList()) { |
| 126 | + if (doubleDataPoint.getExemplarsCount() == expectedCount) { |
| 127 | + return MatchResult.exactMatch(); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } catch (InvalidProtocolBufferException e) { |
| 134 | + throw new RuntimeException(e); |
| 135 | + } |
| 136 | + return MatchResult.noMatch(); |
| 137 | + }; |
| 138 | + } |
| 139 | +} |
0 commit comments