From aa6578dfb8dbe25913874a9be29b36fe12988c14 Mon Sep 17 00:00:00 2001 From: jack-berg <34418638+jack-berg@users.noreply.github.com> Date: Thu, 8 Sep 2022 11:34:20 -0500 Subject: [PATCH] Use micrometer library instrumentation in agent (#6560) * Use micrometer library instrumentation in agent * PR feedback --- .../micrometer-1.5/javaagent/build.gradle.kts | 7 +- .../v1_5/MicrometerInstrumentationModule.java | 5 - .../micrometer/v1_5/MicrometerSingletons.java | 2 +- .../micrometer/v1_5/TimeUnitParser.java | 2 +- .../micrometer/v1_5/CounterTest.java | 66 +--- .../v1_5/DistributionSummaryTest.java | 205 +--------- .../micrometer/v1_5/FunctionCounterTest.java | 107 +----- .../v1_5/FunctionTimerSecondsTest.java | 93 +---- .../micrometer/v1_5/FunctionTimerTest.java | 171 +-------- .../micrometer/v1_5/GaugeTest.java | 134 +------ .../v1_5/LongTaskTimerSecondsTest.java | 126 +------ .../micrometer/v1_5/LongTaskTimerTest.java | 126 +------ .../micrometer/v1_5/MeterTest.java | 225 +---------- .../micrometer/v1_5/PrometheusModeTest.java | 352 +----------------- .../micrometer/v1_5/TestTimer.java | 31 -- .../micrometer/v1_5/TimerSecondsTest.java | 89 +---- .../micrometer/v1_5/TimerTest.java | 231 +----------- .../v1_5/AbstractFunctionTimerTest.java | 2 +- .../micrometer/v1_5/AbstractGaugeTest.java | 2 +- .../AbstractLongTaskTimerHistogramTest.java | 2 +- ...ringBootActuatorInstrumentationModule.java | 5 - .../spring/actuator/ActuatorTest.java | 2 +- 22 files changed, 105 insertions(+), 1880 deletions(-) delete mode 100644 instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TestTimer.java diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/build.gradle.kts b/instrumentation/micrometer/micrometer-1.5/javaagent/build.gradle.kts index e979065b857f..cc2ba4246dfe 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/build.gradle.kts +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/build.gradle.kts @@ -14,10 +14,9 @@ muzzle { dependencies { library("io.micrometer:micrometer-core:1.5.0") - implementation("io.opentelemetry:opentelemetry-micrometer1-shim") { - // just get the instrumentation, without micrometer itself - exclude("io.micrometer", "micrometer-core") - } + implementation(project(":instrumentation:micrometer:micrometer-1.5:library")) + + testImplementation(project(":instrumentation:micrometer:micrometer-1.5:testing")) } tasks { diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MicrometerInstrumentationModule.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MicrometerInstrumentationModule.java index 8de7580d983a..22e6e0096711 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MicrometerInstrumentationModule.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MicrometerInstrumentationModule.java @@ -27,11 +27,6 @@ public ElementMatcher.Junction classLoaderMatcher() { return hasClassesNamed("io.micrometer.core.instrument.config.validate.Validated"); } - @Override - public boolean isHelperClass(String className) { - return className.startsWith("io.opentelemetry.micrometer1shim."); - } - @Override public List typeInstrumentations() { return Collections.singletonList(new MetricsInstrumentation()); diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MicrometerSingletons.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MicrometerSingletons.java index 21d1189de61f..bc4e2b714607 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MicrometerSingletons.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MicrometerSingletons.java @@ -7,8 +7,8 @@ import io.micrometer.core.instrument.MeterRegistry; import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry; import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; -import io.opentelemetry.micrometer1shim.OpenTelemetryMeterRegistry; public final class MicrometerSingletons { diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimeUnitParser.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimeUnitParser.java index 9d6637118578..e4a47994d334 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimeUnitParser.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimeUnitParser.java @@ -7,7 +7,7 @@ import static java.util.logging.Level.WARNING; -import io.opentelemetry.micrometer1shim.OpenTelemetryMeterRegistry; +import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry; import java.util.Locale; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/CounterTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/CounterTest.java index 592faff1114d..cdc8d8ae2016 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/CounterTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/CounterTest.java @@ -5,75 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Metrics; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractCounterTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class CounterTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class CounterTest extends AbstractCounterTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testCounter() throws Exception { - // given - Counter counter = - Counter.builder("testCounter") - .description("This is a test counter") - .tags("tag", "value") - .baseUnit("items") - .register(Metrics.globalRegistry); - - // when - counter.increment(); - counter.increment(2); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testCounter", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test counter") - .hasUnit("items") - .hasDoubleSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(3) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.clearData(); - - // when - Metrics.globalRegistry.remove(counter); - counter.increment(); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testCounter", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/DistributionSummaryTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/DistributionSummaryTest.java index ef0f78b37c0e..80594c7eac94 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/DistributionSummaryTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/DistributionSummaryTest.java @@ -5,214 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.DistributionSummary; import io.micrometer.core.instrument.Metrics; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractDistributionSummaryTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class DistributionSummaryTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class DistributionSummaryTest extends AbstractDistributionSummaryTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testDistributionSummary() throws Exception { - // given - DistributionSummary summary = - DistributionSummary.builder("testSummary") - .description("This is a test distribution summary") - .baseUnit("things") - .scale(2.0) - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - summary.record(21); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testSummary", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test distribution summary") - .hasUnit("things") - .hasHistogramSatisfying( - histogram -> - histogram.hasPointsSatisfying( - point -> - point - .hasSum(42) - .hasCount(1) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.clearData(); - - // when - Metrics.globalRegistry.remove(summary); - summary.record(6); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testSummary", AbstractIterableAssert::isEmpty); - } - - @Test - void testMicrometerHistogram() { - // given - DistributionSummary summary = - DistributionSummary.builder("testSummaryHistogram") - .description("This is a test distribution summary") - .baseUnit("things") - .tags("tag", "value") - .serviceLevelObjectives(1, 10, 100, 1000) - .distributionStatisticBufferLength(10) - .register(Metrics.globalRegistry); - - // when - summary.record(0.5); - summary.record(5); - summary.record(50); - summary.record(500); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testSummaryHistogram.histogram", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(1) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("le"), "1")), - point -> - point - .hasValue(2) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("le"), "10")), - point -> - point - .hasValue(3) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("le"), "100")), - point -> - point - .hasValue(4) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("le"), "1000")))))); - } - - @Test - void testMicrometerPercentiles() { - // given - DistributionSummary summary = - DistributionSummary.builder("testSummaryPercentiles") - .description("This is a test distribution summary") - .baseUnit("things") - .tags("tag", "value") - .publishPercentiles(0.5, 0.95, 0.99) - .register(Metrics.globalRegistry); - - // when - summary.record(50); - summary.record(100); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testSummaryPercentiles.percentile", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("phi"), "0.5")), - point -> - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("phi"), "0.95")), - point -> - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("phi"), "0.99")))))); - } - - @Test - void testMicrometerMax() throws InterruptedException { - // given - DistributionSummary summary = - DistributionSummary.builder("testSummaryMax") - .description("This is a test distribution summary") - .baseUnit("things") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - summary.record(1); - summary.record(2); - summary.record(4); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testSummaryMax.max", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test distribution summary") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(4) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - - // when - Metrics.globalRegistry.remove(summary); - Thread.sleep(100); // give time for any inflight metric export to be received - testing.clearData(); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testSummaryMax.max", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionCounterTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionCounterTest.java index 946a4af6f7c9..d0df46135d85 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionCounterTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionCounterTest.java @@ -5,116 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.FunctionCounter; import io.micrometer.core.instrument.Metrics; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractFunctionCounterTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.util.concurrent.atomic.AtomicLong; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class FunctionCounterTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class FunctionCounterTest extends AbstractFunctionCounterTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - final AtomicLong num = new AtomicLong(12); - final AtomicLong anotherNum = new AtomicLong(13); - - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testFunctionCounter() throws InterruptedException { - // when - FunctionCounter counter = - FunctionCounter.builder("testFunctionCounter", num, AtomicLong::get) - .description("This is a test function counter") - .tags("tag", "value") - .baseUnit("items") - .register(Metrics.globalRegistry); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testFunctionCounter", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test function counter") - .hasUnit("items") - .hasDoubleSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(12) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - - // when - Metrics.globalRegistry.remove(counter); - Thread.sleep(100); // give time for any inflight metric export to be received - testing.clearData(); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testFunctionCounter", AbstractIterableAssert::isEmpty); - } - - @Test - void functionCountersWithSameNameAndDifferentTags() { - // when - FunctionCounter.builder("testFunctionCounterWithTags", num, AtomicLong::get) - .description("This is a test function counter") - .tags("tag", "1") - .baseUnit("items") - .register(Metrics.globalRegistry); - FunctionCounter.builder("testFunctionCounterWithTags", anotherNum, AtomicLong::get) - .description("This is a test function counter") - .tags("tag", "2") - .baseUnit("items") - .register(Metrics.globalRegistry); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testFunctionCounterWithTags", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test function counter") - .hasUnit("items") - .hasDoubleSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(12) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "1")), - point -> - point - .hasValue(13) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "2")))))); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionTimerSecondsTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionTimerSecondsTest.java index 6295bf5f626f..80d9b1806c32 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionTimerSecondsTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionTimerSecondsTest.java @@ -5,102 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.FunctionTimer; import io.micrometer.core.instrument.Metrics; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractFunctionTimerSecondsTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.util.concurrent.TimeUnit; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class FunctionTimerSecondsTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class FunctionTimerSecondsTest extends AbstractFunctionTimerSecondsTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - final TestTimer timerObj = new TestTimer(); - - @BeforeEach - void cleanupMeters() { - timerObj.reset(); + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testFunctionTimerWithBaseUnitSeconds() throws InterruptedException { - // given - FunctionTimer functionTimer = - FunctionTimer.builder( - "testFunctionTimerSeconds", - timerObj, - TestTimer::getCount, - TestTimer::getTotalTimeNanos, - TimeUnit.NANOSECONDS) - .description("This is a test function timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - timerObj.add(42, TimeUnit.SECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testFunctionTimerSeconds.count", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test function timer") - .hasUnit("1") - .hasLongSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(1) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testFunctionTimerSeconds.sum", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test function timer") - .hasUnit("s") - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(42) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - - // when - Metrics.globalRegistry.remove(functionTimer); - Thread.sleep(100); // give time for any inflight metric export to be received - testing.clearData(); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testFunctionTimerSeconds.count", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testFunctionTimerSeconds.sum", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionTimerTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionTimerTest.java index c0170159dba5..b33453376961 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionTimerTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/FunctionTimerTest.java @@ -5,180 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.FunctionTimer; import io.micrometer.core.instrument.Metrics; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractFunctionTimerTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.util.concurrent.TimeUnit; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class FunctionTimerTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class FunctionTimerTest extends AbstractFunctionTimerTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - final TestTimer timerObj = new TestTimer(); - final TestTimer anotherTimerObj = new TestTimer(); - - @BeforeEach - void cleanupMeters() { - timerObj.reset(); - anotherTimerObj.reset(); + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testFunctionTimer() throws InterruptedException { - // given - FunctionTimer functionTimer = - FunctionTimer.builder( - "testFunctionTimer", - timerObj, - TestTimer::getCount, - TestTimer::getTotalTimeNanos, - TimeUnit.NANOSECONDS) - .description("This is a test function timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - timerObj.add(42, TimeUnit.SECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testFunctionTimer.count", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test function timer") - .hasUnit("1") - .hasLongSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(1) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testFunctionTimer.sum", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test function timer") - .hasUnit("ms") - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(42_000) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - - // when - Metrics.globalRegistry.remove(functionTimer); - Thread.sleep(100); // give time for any inflight metric export to be received - testing.clearData(); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testFunctionTimer.count", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testFunctionTimer.sum", AbstractIterableAssert::isEmpty); - } - - @Test - void testNanoPrecision() { - // given - FunctionTimer.builder( - "testNanoFunctionTimer", - timerObj, - TestTimer::getCount, - TestTimer::getTotalTimeNanos, - TimeUnit.NANOSECONDS) - .register(Metrics.globalRegistry); - - // when - timerObj.add(1_234_000, TimeUnit.NANOSECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testNanoFunctionTimer.sum", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasUnit("ms") - .hasDoubleSumSatisfying( - sum -> sum.hasPointsSatisfying(point -> point.hasValue(1.234))))); - } - - @Test - void functionTimersWithSameNameAndDifferentTags() { - // given - FunctionTimer.builder( - "testFunctionTimerWithTags", - timerObj, - TestTimer::getCount, - TestTimer::getTotalTimeNanos, - TimeUnit.NANOSECONDS) - .tags("tag", "1") - .register(Metrics.globalRegistry); - - FunctionTimer.builder( - "testFunctionTimerWithTags", - anotherTimerObj, - TestTimer::getCount, - TestTimer::getTotalTimeNanos, - TimeUnit.NANOSECONDS) - .tags("tag", "2") - .register(Metrics.globalRegistry); - - // when - timerObj.add(12, TimeUnit.SECONDS); - anotherTimerObj.add(42, TimeUnit.SECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testFunctionTimerWithTags.sum", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasUnit("ms") - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(12_000) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "1")), - point -> - point - .hasValue(42_000) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "2")))))); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/GaugeTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/GaugeTest.java index d431782e0541..90672d0d261f 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/GaugeTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/GaugeTest.java @@ -5,143 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.Gauge; import io.micrometer.core.instrument.Metrics; -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.instrumentation.test.utils.GcUtils; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractGaugeTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.lang.ref.WeakReference; -import java.util.concurrent.atomic.AtomicLong; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class GaugeTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class GaugeTest extends AbstractGaugeTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testGauge() throws InterruptedException { - // when - Gauge gauge = - Gauge.builder("testGauge", () -> 42) - .description("This is a test gauge") - .tags("tag", "value") - .baseUnit("items") - .register(Metrics.globalRegistry); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testGauge", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test gauge") - .hasUnit("items") - .hasDoubleGaugeSatisfying( - g -> - g.hasPointsSatisfying( - point -> - point - .hasValue(42) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - - // when - Metrics.globalRegistry.remove(gauge); - Thread.sleep(100); // give time for any inflight metric export to be received - testing.clearData(); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testGauge", AbstractIterableAssert::isEmpty); - } - - @Test - void gaugesWithSameNameAndDifferentTags() { - // when - Gauge.builder("testGaugeWithTags", () -> 12) - .description("This is a test gauge") - .baseUnit("items") - .tags("tag", "1") - .register(Metrics.globalRegistry); - Gauge.builder("testGaugeWithTags", () -> 42) - .description("This is a test gauge") - .baseUnit("items") - .tags("tag", "2") - .register(Metrics.globalRegistry); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testGaugeWithTags", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test gauge") - .hasUnit("items") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(12) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "1")), - point -> - point - .hasValue(42) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "2")))))); - } - - @Test - void testWeakRefGauge() throws InterruptedException { - // when - AtomicLong num = new AtomicLong(42); - Gauge.builder("testWeakRefGauge", num, AtomicLong::get) - .strongReference(false) - .register(Metrics.globalRegistry); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testWeakRefGauge", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDoubleGaugeSatisfying( - gauge -> gauge.hasPointsSatisfying(point -> point.hasValue(42))))); - - // when - WeakReference numWeakRef = new WeakReference<>(num); - num = null; - GcUtils.awaitGc(numWeakRef); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.clearData(); - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testWeakRefGauge", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/LongTaskTimerSecondsTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/LongTaskTimerSecondsTest.java index c16f01e59c9d..23a8249fb66b 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/LongTaskTimerSecondsTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/LongTaskTimerSecondsTest.java @@ -5,135 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.LongTaskTimer; import io.micrometer.core.instrument.Metrics; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractLongTaskTimerSecondsTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.util.concurrent.TimeUnit; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class LongTaskTimerSecondsTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class LongTaskTimerSecondsTest extends AbstractLongTaskTimerSecondsTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testLongTaskTimerWithBaseUnitSeconds() throws InterruptedException { - // given - LongTaskTimer timer = - LongTaskTimer.builder("testLongTaskTimerSeconds") - .description("This is a test long task timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - LongTaskTimer.Sample sample = timer.start(); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testLongTaskTimerSeconds.active", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test long task timer") - .hasUnit("tasks") - .hasLongSumSatisfying( - sum -> - sum.isNotMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(1) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testLongTaskTimerSeconds.duration", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test long task timer") - .hasUnit("s") - .hasDoubleSumSatisfying( - sum -> - sum.isNotMonotonic() - .hasPointsSatisfying( - point -> { - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value")); - // any value >0 - duration of currently running tasks - assertThat(metric.getDoubleSumData().getPoints()) - .satisfiesExactly( - p -> assertThat(p.getValue()).isPositive()); - })))); - - // when - TimeUnit.MILLISECONDS.sleep(100); - sample.stop(); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testLongTaskTimerSeconds.active", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasLongSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(0) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testLongTaskTimerSeconds.duration", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(0) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - - // when timer is removed from the registry - Metrics.globalRegistry.remove(timer); - Thread.sleep(100); // give time for any in flight metric export to be received - testing.clearData(); - timer.start(); - - // then no tasks are active after starting a new sample - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testLongTaskTimerSeconds.active", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testLongTaskTimerSeconds.duration", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/LongTaskTimerTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/LongTaskTimerTest.java index d2ae6109cf48..74373909c355 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/LongTaskTimerTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/LongTaskTimerTest.java @@ -5,135 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.LongTaskTimer; import io.micrometer.core.instrument.Metrics; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractLongTaskTimerTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.util.concurrent.TimeUnit; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class LongTaskTimerTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class LongTaskTimerTest extends AbstractLongTaskTimerTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testLongTaskTimer() throws InterruptedException { - // given - LongTaskTimer timer = - LongTaskTimer.builder("testLongTaskTimer") - .description("This is a test long task timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - LongTaskTimer.Sample sample = timer.start(); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testLongTaskTimer.active", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test long task timer") - .hasUnit("tasks") - .hasLongSumSatisfying( - sum -> - sum.isNotMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(1) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testLongTaskTimer.duration", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test long task timer") - .hasUnit("ms") - .hasDoubleSumSatisfying( - sum -> - sum.isNotMonotonic() - .hasPointsSatisfying( - point -> { - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value")); - // any value >0 - duration of currently running tasks - assertThat(metric.getDoubleSumData().getPoints()) - .satisfiesExactly( - p -> assertThat(p.getValue()).isPositive()); - })))); - - // when - TimeUnit.MILLISECONDS.sleep(100); - sample.stop(); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testLongTaskTimer.active", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasLongSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(0) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testLongTaskTimer.duration", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(0) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - - // when timer is removed from the registry - Metrics.globalRegistry.remove(timer); - Thread.sleep(100); // give time for any in flight metric export to be received - testing.clearData(); - timer.start(); - - // then no tasks are active after starting a new sample - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testLongTaskTimer.active", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testLongTaskTimer.duration", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MeterTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MeterTest.java index 8ffd2786c02e..414a21984bda 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MeterTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/MeterTest.java @@ -5,234 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.Measurement; -import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.Metrics; -import io.micrometer.core.instrument.Statistic; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractMeterTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.atomic.AtomicReference; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -class MeterTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class MeterTest extends AbstractMeterTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testMeter() throws InterruptedException { - // given - AtomicReference number = new AtomicReference<>(12345.0); - - List measurements = - Arrays.asList( - new Measurement(number::get, Statistic.TOTAL), - new Measurement(number::get, Statistic.TOTAL_TIME), - new Measurement(number::get, Statistic.COUNT), - new Measurement(number::get, Statistic.ACTIVE_TASKS), - new Measurement(number::get, Statistic.DURATION), - new Measurement(number::get, Statistic.MAX), - new Measurement(number::get, Statistic.VALUE), - new Measurement(number::get, Statistic.UNKNOWN)); - - // when - Meter meter = - Meter.builder("testMeter", Meter.Type.OTHER, measurements) - .description("This is a test meter") - .baseUnit("things") - .tag("tag", "value") - .register(Metrics.globalRegistry); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testMeter.total", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test meter") - .hasUnit("things") - .hasDoubleSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(12345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testMeter.total_time", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test meter") - .hasUnit("things") - .hasDoubleSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(12345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testMeter.count", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test meter") - .hasUnit("things") - .hasDoubleSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(12345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testMeter.active", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test meter") - .hasUnit("things") - .hasDoubleSumSatisfying( - sum -> - sum.isNotMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(12345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testMeter.duration", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test meter") - .hasUnit("things") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(12345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testMeter.max", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test meter") - .hasUnit("things") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(12345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testMeter.value", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test meter") - .hasUnit("things") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(12345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testMeter.unknown", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test meter") - .hasUnit("things") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(12345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - - // when - Metrics.globalRegistry.remove(meter); - Thread.sleep(100); // give time for any inflight metric export to be received - testing.clearData(); - - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testMeter.total", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testMeter.total_time", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testMeter.count", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testMeter.active", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testMeter.duration", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testMeter.max", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testMeter.value", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testMeter.unknown", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/PrometheusModeTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/PrometheusModeTest.java index 7a6677fc7b74..c2e57d2c5732 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/PrometheusModeTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/PrometheusModeTest.java @@ -5,361 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - -import io.micrometer.core.instrument.Counter; -import io.micrometer.core.instrument.DistributionSummary; -import io.micrometer.core.instrument.FunctionTimer; -import io.micrometer.core.instrument.Gauge; -import io.micrometer.core.instrument.LongTaskTimer; import io.micrometer.core.instrument.Metrics; -import io.micrometer.core.instrument.Timer; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractPrometheusModeTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.util.concurrent.TimeUnit; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -@SuppressWarnings("PreferJavaTimeOverload") -class PrometheusModeTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class PrometheusModeTest extends AbstractPrometheusModeTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - final TestTimer timerObj = new TestTimer(); - - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testCounter() { - // given - Counter counter = - Counter.builder("testPrometheusCounter") - .description("This is a test counter") - .tags("tag", "value") - .baseUnit("items") - .register(Metrics.globalRegistry); - - // when - counter.increment(12); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusCounter.items", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test counter") - .hasUnit("items") - .hasDoubleSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(12) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - } - - @Test - void testDistributionSummary() { - // given - DistributionSummary summary = - DistributionSummary.builder("testPrometheusSummary") - .description("This is a test summary") - .baseUnit("items") - .tag("tag", "value") - .register(Metrics.globalRegistry); - - // when - summary.record(12); - summary.record(42); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusSummary.items", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test summary") - .hasUnit("items") - .hasHistogramSatisfying( - histogram -> - histogram.hasPointsSatisfying( - point -> - point - .hasSum(54) - .hasCount(2) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusSummary.items.max", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test summary") - .hasUnit("items") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(42) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - } - - @Test - void testFunctionTimer() { - // given - FunctionTimer.builder( - "testPrometheusFunctionTimer", - timerObj, - TestTimer::getCount, - TestTimer::getTotalTimeNanos, - TimeUnit.NANOSECONDS) - .description("This is a test function timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - timerObj.add(42, TimeUnit.SECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusFunctionTimer.seconds.count", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test function timer") - .hasUnit("1") - .hasLongSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(1) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusFunctionTimer.seconds.sum", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test function timer") - .hasUnit("s") - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(42) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - } - - @Test - void testGauge() { - // when - Gauge.builder("testPrometheusGauge", () -> 42) - .description("This is a test gauge") - .tags("tag", "value") - .baseUnit("items") - .register(Metrics.globalRegistry); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusGauge.items", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test gauge") - .hasUnit("items") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(42) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - } - - @Test - void testLongTaskTimer() throws InterruptedException { - // given - LongTaskTimer timer = - LongTaskTimer.builder("testPrometheusLongTaskTimer") - .description("This is a test long task timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - LongTaskTimer.Sample sample = timer.start(); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusLongTaskTimer.seconds.active", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test long task timer") - .hasUnit("tasks") - .hasLongSumSatisfying( - sum -> - sum.isNotMonotonic() - .hasPointsSatisfying( - point -> - point - .hasValue(1) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), - "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusLongTaskTimer.seconds.duration", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test long task timer") - .hasUnit("s") - .hasDoubleSumSatisfying( - sum -> - sum.isNotMonotonic() - .hasPointsSatisfying( - point -> { - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value")); - // any value >0 - duration of currently running tasks - assertThat(metric.getDoubleSumData().getPoints()) - .satisfiesExactly( - p -> assertThat(p.getValue()).isPositive()); - })))); - - // when - TimeUnit.MILLISECONDS.sleep(100); - sample.stop(); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusLongTaskTimer.seconds.active", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasLongSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(0) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusLongTaskTimer.seconds.duration", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point - .hasValue(0) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - } - - @Test - void testTimer() { - // given - Timer timer = - Timer.builder("testPrometheusTimer") - .description("This is a test timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - timer.record(1, TimeUnit.SECONDS); - timer.record(5, TimeUnit.SECONDS); - timer.record(10_789, TimeUnit.MILLISECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusTimer.seconds", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test timer") - .hasUnit("s") - .hasHistogramSatisfying( - histogram -> - histogram.hasPointsSatisfying( - point -> - point - .hasSum(16.789) - .hasCount(3) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testPrometheusTimer.seconds.max", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test timer") - .hasUnit("s") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(10.789) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TestTimer.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TestTimer.java deleted file mode 100644 index f16e886a99be..000000000000 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TestTimer.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; - -import java.util.concurrent.TimeUnit; - -class TestTimer { - int count = 0; - long totalTimeNanos = 0; - - void add(long time, TimeUnit unit) { - count++; - totalTimeNanos += unit.toNanos(time); - } - - int getCount() { - return count; - } - - double getTotalTimeNanos() { - return totalTimeNanos; - } - - void reset() { - count = 0; - totalTimeNanos = 0; - } -} diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimerSecondsTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimerSecondsTest.java index 2e9a154848fa..f3bdb113a0dc 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimerSecondsTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimerSecondsTest.java @@ -5,98 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - import io.micrometer.core.instrument.Metrics; -import io.micrometer.core.instrument.Timer; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractTimerSecondsTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.util.concurrent.TimeUnit; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -@SuppressWarnings("PreferJavaTimeOverload") -class TimerSecondsTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class TimerSecondsTest extends AbstractTimerSecondsTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testTimerWithBaseUnitSeconds() throws InterruptedException { - // given - Timer timer = - Timer.builder("testTimerSeconds") - .description("This is a test timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - timer.record(1, TimeUnit.SECONDS); - timer.record(10, TimeUnit.SECONDS); - timer.record(12_345, TimeUnit.MILLISECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testTimerSeconds", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test timer") - .hasUnit("s") - .hasHistogramSatisfying( - histogram -> - histogram.hasPointsSatisfying( - point -> - point - .hasSum(23.345) - .hasCount(3) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testTimerSeconds.max", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test timer") - .hasUnit("s") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(12.345) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.clearData(); - - // when - Metrics.globalRegistry.remove(timer); - timer.record(12, TimeUnit.SECONDS); - Thread.sleep(100); // give time for any inflight metric export to be received - testing.clearData(); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testTimerSeconds", AbstractIterableAssert::isEmpty); - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testTimerMax.max", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimerTest.java b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimerTest.java index 5205602e5ce5..3e0d13a2e57f 100644 --- a/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimerTest.java +++ b/instrumentation/micrometer/micrometer-1.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/micrometer/v1_5/TimerTest.java @@ -5,240 +5,25 @@ package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; - import io.micrometer.core.instrument.Metrics; -import io.micrometer.core.instrument.Timer; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractTimerTest; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import java.time.Duration; -import java.util.concurrent.TimeUnit; -import org.assertj.core.api.AbstractIterableAssert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; -@SuppressWarnings("PreferJavaTimeOverload") -class TimerTest { - - static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim"; +class TimerTest extends AbstractTimerTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - void cleanupMeters() { + @AfterEach + public void cleanup() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); } - @Test - void testTimer() throws Exception { - // given - Timer timer = - Timer.builder("testTimer") - .description("This is a test timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - timer.record(42, TimeUnit.SECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testTimer", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test timer") - .hasUnit("ms") - .hasHistogramSatisfying( - histogram -> - histogram.hasPointsSatisfying( - point -> - point - .hasSum(42_000) - .hasCount(1) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - testing.clearData(); - - // when - Metrics.globalRegistry.remove(timer); - timer.record(12, TimeUnit.SECONDS); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testTimerMax", AbstractIterableAssert::isEmpty); - } - - @Test - void testNanoPrecision() { - // given - Timer timer = Timer.builder("testNanoTimer").register(Metrics.globalRegistry); - - // when - timer.record(1_234_000, TimeUnit.NANOSECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testNanoTimer", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasUnit("ms") - .hasHistogramSatisfying( - histogram -> - histogram.hasPointsSatisfying( - point -> point.hasSum(1.234).hasCount(1))))); - } - - @Test - void testMicrometerHistogram() { - // given - Timer timer = - Timer.builder("testTimerHistogram") - .description("This is a test timer") - .tags("tag", "value") - .serviceLevelObjectives( - Duration.ofSeconds(1), - Duration.ofSeconds(10), - Duration.ofSeconds(100), - Duration.ofSeconds(1000)) - .distributionStatisticBufferLength(10) - .register(Metrics.globalRegistry); - - // when - timer.record(500, TimeUnit.MILLISECONDS); - timer.record(5, TimeUnit.SECONDS); - timer.record(50, TimeUnit.SECONDS); - timer.record(500, TimeUnit.SECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testTimerHistogram.histogram", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(1) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("le"), "1000")), - point -> - point - .hasValue(2) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("le"), "10000")), - point -> - point - .hasValue(3) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("le"), "100000")), - point -> - point - .hasValue(4) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo( - AttributeKey.stringKey("le"), "1000000")))))); - } - - @Test - void testMicrometerPercentiles() { - // given - Timer timer = - Timer.builder("testTimerPercentiles") - .description("This is a test timer") - .tags("tag", "value") - .publishPercentiles(0.5, 0.95, 0.99) - .register(Metrics.globalRegistry); - - // when - timer.record(50, TimeUnit.MILLISECONDS); - timer.record(100, TimeUnit.MILLISECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testTimerPercentiles.percentile", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("phi"), "0.5")), - point -> - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("phi"), "0.95")), - point -> - point.hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("tag"), "value"), - equalTo(AttributeKey.stringKey("phi"), "0.99")))))); - } - - @Test - void testMicrometerMax() throws InterruptedException { - // given - Timer timer = - Timer.builder("testTimerMax") - .description("This is a test timer") - .tags("tag", "value") - .register(Metrics.globalRegistry); - - // when - timer.record(1, TimeUnit.SECONDS); - timer.record(2, TimeUnit.SECONDS); - timer.record(4, TimeUnit.SECONDS); - - // then - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, - "testTimerMax.max", - metrics -> - metrics.anySatisfy( - metric -> - assertThat(metric) - .hasDescription("This is a test timer") - .hasDoubleGaugeSatisfying( - gauge -> - gauge.hasPointsSatisfying( - point -> - point - .hasValue(4_000) - .hasAttributesSatisfying( - equalTo( - AttributeKey.stringKey("tag"), "value")))))); - - // when - Metrics.globalRegistry.remove(timer); - Thread.sleep(100); // give time for any inflight metric export to be received - testing.clearData(); - - // then - Thread.sleep(100); // interval of the test metrics exporter - testing.waitAndAssertMetrics( - INSTRUMENTATION_NAME, "testTimerMax.max", AbstractIterableAssert::isEmpty); + @Override + protected InstrumentationExtension testing() { + return testing; } } diff --git a/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractFunctionTimerTest.java b/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractFunctionTimerTest.java index 03345a89f94e..5f3ec61fb7d9 100644 --- a/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractFunctionTimerTest.java +++ b/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractFunctionTimerTest.java @@ -18,7 +18,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -abstract class AbstractFunctionTimerTest { +public abstract class AbstractFunctionTimerTest { protected abstract InstrumentationExtension testing(); diff --git a/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractGaugeTest.java b/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractGaugeTest.java index 50a992755a0c..95b526211432 100644 --- a/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractGaugeTest.java +++ b/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractGaugeTest.java @@ -17,7 +17,7 @@ import org.assertj.core.api.AbstractIterableAssert; import org.junit.jupiter.api.Test; -abstract class AbstractGaugeTest { +public abstract class AbstractGaugeTest { protected abstract InstrumentationExtension testing(); diff --git a/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractLongTaskTimerHistogramTest.java b/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractLongTaskTimerHistogramTest.java index 223f770720aa..3c9458a25552 100644 --- a/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractLongTaskTimerHistogramTest.java +++ b/instrumentation/micrometer/micrometer-1.5/testing/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/AbstractLongTaskTimerHistogramTest.java @@ -17,7 +17,7 @@ import java.time.Duration; import org.junit.jupiter.api.Test; -abstract class AbstractLongTaskTimerHistogramTest { +public abstract class AbstractLongTaskTimerHistogramTest { protected abstract InstrumentationExtension testing(); diff --git a/instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/SpringBootActuatorInstrumentationModule.java b/instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/SpringBootActuatorInstrumentationModule.java index fdb2f71e4568..d49def3beab0 100644 --- a/instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/SpringBootActuatorInstrumentationModule.java +++ b/instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/SpringBootActuatorInstrumentationModule.java @@ -30,11 +30,6 @@ public void registerHelperResources(HelperResourceBuilder helperResourceBuilder) "io/opentelemetry/javaagent/instrumentation/spring/actuator/OpenTelemetryMeterRegistryAutoConfiguration.class"); } - @Override - public boolean isHelperClass(String className) { - return className.startsWith("io.opentelemetry.micrometer1shim."); - } - @Override public List typeInstrumentations() { return singletonList(new AutoConfigurationImportSelectorInstrumentation()); diff --git a/instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/ActuatorTest.java b/instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/ActuatorTest.java index ffcc9bd3e767..8fa46384722a 100644 --- a/instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/ActuatorTest.java +++ b/instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/ActuatorTest.java @@ -37,7 +37,7 @@ void shouldInjectOtelMeterRegistry() { testBean.inc(); testing.waitAndAssertMetrics( - "io.opentelemetry.micrometer1shim", + "io.opentelemetry.micrometer-1.5", "test-counter", metrics -> metrics.anySatisfy(