|
| 1 | +package io.quarkus.micrometer.opentelemetry.deployment; |
| 2 | + |
| 3 | +import io.micrometer.common.annotation.ValueResolver; |
| 4 | +import io.micrometer.core.annotation.Counted; |
| 5 | +import io.micrometer.core.aop.MeterTag; |
| 6 | +import io.opentelemetry.sdk.metrics.data.MetricData; |
| 7 | +import io.quarkus.micrometer.opentelemetry.deployment.common.InMemoryMetricExporter; |
| 8 | +import io.quarkus.micrometer.opentelemetry.deployment.common.InMemoryMetricExporterProvider; |
| 9 | +import io.quarkus.micrometer.opentelemetry.deployment.common.Util; |
| 10 | +import io.quarkus.test.QuarkusUnitTest; |
| 11 | +import jakarta.enterprise.context.ApplicationScoped; |
| 12 | +import jakarta.inject.Inject; |
| 13 | +import jakarta.inject.Singleton; |
| 14 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 15 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 16 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 17 | +import org.junit.jupiter.api.Assertions; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 21 | + |
| 22 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
| 23 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry; |
| 24 | + |
| 25 | + |
| 26 | +public class MicrometerCounterInterceptorTest { |
| 27 | + |
| 28 | + @RegisterExtension |
| 29 | + static final QuarkusUnitTest TEST = new QuarkusUnitTest() |
| 30 | + .setArchiveProducer( |
| 31 | + () -> ShrinkWrap.create(JavaArchive.class) |
| 32 | + .addClasses(Util.class, CountedBean.class, TestValueResolver.class) |
| 33 | + .addClasses(InMemoryMetricExporter.class, InMemoryMetricExporterProvider.class) |
| 34 | + .addAsResource(new StringAsset(InMemoryMetricExporterProvider.class.getCanonicalName()), |
| 35 | + "META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider") |
| 36 | + .add(new StringAsset(""" |
| 37 | + quarkus.otel.sdk.disabled=false\n |
| 38 | + quarkus.otel.metrics.enabled=true\n |
| 39 | + quarkus.otel.traces.exporter=none\n |
| 40 | + quarkus.otel.logs.exporter=none\n |
| 41 | + quarkus.otel.metrics.exporter=in-memory\n |
| 42 | + quarkus.otel.metric.export.interval=300ms\n |
| 43 | + quarkus.micrometer.binder.http-client.enabled=true\n |
| 44 | + quarkus.micrometer.binder.http-server.enabled=true\n |
| 45 | + quarkus.redis.devservices.enabled=false\n |
| 46 | + """), |
| 47 | + "application.properties")); |
| 48 | + |
| 49 | + @Inject |
| 50 | + CountedBean countedBean; |
| 51 | + |
| 52 | + @Inject |
| 53 | + InMemoryMetricExporter exporter; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void setup() { |
| 57 | + exporter.reset(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testCountAllMetrics() { |
| 62 | + countedBean.countAllInvocations(false); |
| 63 | + Assertions.assertThrows(NullPointerException.class, () -> countedBean.countAllInvocations(true)); |
| 64 | + |
| 65 | + exporter.assertCountDataPointsAtLeastOrEqual("metric.all", null, 2); |
| 66 | + |
| 67 | + MetricData metricAll = exporter.getFinishedMetricItem("metric.all"); |
| 68 | + assertThat(metricAll) |
| 69 | + .isNotNull() |
| 70 | + .hasName("metric.all") |
| 71 | + .hasDescription("")// currently empty |
| 72 | + .hasUnit("")// currently empty |
| 73 | + .hasDoubleSumSatisfying(sum -> sum.hasPointsSatisfying( |
| 74 | + point -> point |
| 75 | + .hasValue(1d) |
| 76 | + .hasAttributes(attributeEntry( |
| 77 | + "class", |
| 78 | + "io.quarkus.micrometer.opentelemetry.deployment.MicrometerCounterInterceptorTest$CountedBean"), |
| 79 | + attributeEntry("method", "countAllInvocations"), |
| 80 | + attributeEntry("extra", "tag"), |
| 81 | + attributeEntry("do_fail", "prefix_false"), |
| 82 | + attributeEntry("exception", "none"), |
| 83 | + attributeEntry("result", "success")), |
| 84 | + point -> point |
| 85 | + .hasValue(1d) |
| 86 | + .hasAttributes(attributeEntry( |
| 87 | + "class", |
| 88 | + "io.quarkus.micrometer.opentelemetry.deployment.MicrometerCounterInterceptorTest$CountedBean"), |
| 89 | + attributeEntry("method", "countAllInvocations"), |
| 90 | + attributeEntry("extra", "tag"), |
| 91 | + attributeEntry("do_fail", "prefix_true"), |
| 92 | + attributeEntry("exception", "NullPointerException"), |
| 93 | + attributeEntry("result", "failure")))); |
| 94 | + } |
| 95 | + |
| 96 | + @ApplicationScoped |
| 97 | + public static class CountedBean { |
| 98 | + @Counted(value = "metric.none", recordFailuresOnly = true) |
| 99 | + public void onlyCountFailures() { |
| 100 | + } |
| 101 | + |
| 102 | + @Counted(value = "metric.all", extraTags = {"extra", "tag"}) |
| 103 | + public void countAllInvocations(@MeterTag(key = "do_fail", resolver = TestValueResolver.class) boolean fail) { |
| 104 | + if (fail) { |
| 105 | + throw new NullPointerException("Failed on purpose"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Counted(description = "nice description") |
| 110 | + public void emptyMetricName(@MeterTag boolean fail) { |
| 111 | + if (fail) { |
| 112 | + throw new NullPointerException("Failed on purpose"); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Singleton |
| 118 | + public static class TestValueResolver implements ValueResolver { |
| 119 | + @Override |
| 120 | + public String resolve(Object parameter) { |
| 121 | + return "prefix_" + parameter; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | +} |
0 commit comments