From 138a35bd59a080ee4f88059666cd51c400025ea4 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 26 May 2022 17:06:08 -0700 Subject: [PATCH 1/7] add tomcat-jdbc connection pool metrics instrumentation --- .../tomcat/tomcat-jdbc/build.gradle.kts | 17 +++ .../jdbc/DataSourceProxyInstrumentation.java | 53 +++++++++ .../jdbc/TomcatConnectionPoolMetrics.java | 55 +++++++++ .../jdbc/TomcatJdbcInstrumentationModule.java | 30 +++++ .../jdbc/TomcatJdbcInstrumentationTest.java | 105 ++++++++++++++++++ settings.gradle.kts | 1 + 6 files changed, 261 insertions(+) create mode 100644 instrumentation/tomcat/tomcat-jdbc/build.gradle.kts create mode 100644 instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/DataSourceProxyInstrumentation.java create mode 100644 instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatConnectionPoolMetrics.java create mode 100644 instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationModule.java create mode 100644 instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java diff --git a/instrumentation/tomcat/tomcat-jdbc/build.gradle.kts b/instrumentation/tomcat/tomcat-jdbc/build.gradle.kts new file mode 100644 index 000000000000..ab8684db2fd1 --- /dev/null +++ b/instrumentation/tomcat/tomcat-jdbc/build.gradle.kts @@ -0,0 +1,17 @@ +plugins { + id("otel.javaagent-instrumentation") +} + +muzzle { + pass { + group.set("org.apache.tomcat") + module.set("tomcat-jdbc") + versions.set("[8.5.0,)") + // no assertInverse because tomcat-jdbc < 8.5 doesn't have methods that we hook into + } +} + +dependencies { + compileOnly("org.apache.tomcat:tomcat-jdbc:8.5.0") + testImplementation("org.apache.tomcat:tomcat-jdbc:8.5.0") +} diff --git a/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/DataSourceProxyInstrumentation.java b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/DataSourceProxyInstrumentation.java new file mode 100644 index 000000000000..07d79f4ce801 --- /dev/null +++ b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/DataSourceProxyInstrumentation.java @@ -0,0 +1,53 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.tomcat.jdbc; + +import static net.bytebuddy.matcher.ElementMatchers.isPublic; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; + +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.tomcat.jdbc.pool.DataSourceProxy; + +class DataSourceProxyInstrumentation implements TypeInstrumentation { + + @Override + public ElementMatcher typeMatcher() { + return named("org.apache.tomcat.jdbc.pool.DataSourceProxy"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod( + isPublic().and(named("createPool")).and(takesNoArguments()), + this.getClass().getName() + "$CreatePoolAdvice"); + + transformer.applyAdviceToMethod( + isPublic().and(named("close")).and(takesArguments(1)), + this.getClass().getName() + "$CloseAdvice"); + } + + @SuppressWarnings("unused") + public static class CreatePoolAdvice { + @Advice.OnMethodExit(suppress = Throwable.class) + public static void onExit(@Advice.This DataSourceProxy dataSource) { + TomcatConnectionPoolMetrics.registerMetrics(dataSource); + } + } + + @SuppressWarnings("unused") + public static class CloseAdvice { + @Advice.OnMethodExit(suppress = Throwable.class) + public static void onExit(@Advice.This DataSourceProxy dataSource) { + TomcatConnectionPoolMetrics.unregisterMetrics(dataSource); + } + } +} diff --git a/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatConnectionPoolMetrics.java b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatConnectionPoolMetrics.java new file mode 100644 index 000000000000..506de0da75e8 --- /dev/null +++ b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatConnectionPoolMetrics.java @@ -0,0 +1,55 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.tomcat.jdbc; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.metrics.ObservableLongUpDownCounter; +import io.opentelemetry.instrumentation.api.metrics.db.DbConnectionPoolMetrics; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.tomcat.jdbc.pool.DataSourceProxy; + +public final class TomcatConnectionPoolMetrics { + + private static final OpenTelemetry openTelemetry = GlobalOpenTelemetry.get(); + private static final String INSTRUMENTATION_NAME = "io.opentelemetry.tomcat-jdbc"; + + private static final Map> dataSourceMetrics = + new ConcurrentHashMap<>(); + + public static void registerMetrics(DataSourceProxy dataSource) { + dataSourceMetrics.computeIfAbsent(dataSource, TomcatConnectionPoolMetrics::createCounters); + } + + private static List createCounters(DataSourceProxy dataSource) { + + DbConnectionPoolMetrics metrics = + DbConnectionPoolMetrics.create( + openTelemetry, INSTRUMENTATION_NAME, dataSource.getPoolName()); + + return Arrays.asList( + metrics.usedConnections(dataSource::getActive), + metrics.idleConnections(dataSource::getIdle), + metrics.minIdleConnections(dataSource::getMinIdle), + metrics.maxIdleConnections(dataSource::getMaxIdle), + metrics.maxConnections(dataSource::getMaxActive), + metrics.pendingRequestsForConnection(dataSource::getWaitCount)); + } + + public static void unregisterMetrics(DataSourceProxy dataSource) { + List counters = dataSourceMetrics.remove(dataSource); + if (counters != null) { + for (ObservableLongUpDownCounter meter : counters) { + meter.close(); + } + } + } + + private TomcatConnectionPoolMetrics() {} +} diff --git a/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationModule.java b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationModule.java new file mode 100644 index 000000000000..73dd931be33a --- /dev/null +++ b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationModule.java @@ -0,0 +1,30 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.tomcat.jdbc; + +import static java.util.Collections.singletonList; + +import com.google.auto.service.AutoService; +import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import java.util.List; + +@AutoService(InstrumentationModule.class) +public class TomcatJdbcInstrumentationModule extends InstrumentationModule { + public TomcatJdbcInstrumentationModule() { + super("tomcat-jdbc"); + } + + @Override + public boolean isHelperClass(String className) { + return className.startsWith("io.opentelemetry.javaagent.instrumentation"); + } + + @Override + public List typeInstrumentations() { + return singletonList(new DataSourceProxyInstrumentation()); + } +} diff --git a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java new file mode 100644 index 000000000000..de943f7ba078 --- /dev/null +++ b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java @@ -0,0 +1,105 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.tomcat.jdbc; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; +import static org.mockito.BDDMockito.given; + +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.db.DbConnectionPoolMetricsAssertions; +import java.sql.Connection; +import java.util.concurrent.TimeUnit; +import org.apache.tomcat.jdbc.pool.DataSource; +import org.assertj.core.api.AbstractIterableAssert; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class TomcatJdbcInstrumentationTest { + + // @RegisterExtension + // static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); + @RegisterExtension + static final AgentInstrumentationExtension testing = AgentInstrumentationExtension.create(); + + // @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); + @Mock javax.sql.DataSource dataSourceMock; + @Mock Connection connectionMock; + + @Test + void shouldReportMetrics() throws Exception { + // given + given(dataSourceMock.getConnection()).willReturn(connectionMock); + + DataSource tomcatDataSource = new DataSource(); + tomcatDataSource.setDataSource(dataSourceMock); + + // there shouldn't be any problems if this methods gets called more than once + tomcatDataSource.createPool(); + tomcatDataSource.createPool(); + + // when + Connection connection = tomcatDataSource.getConnection(); + connection.close(); + + // then + await() + .atMost(20, TimeUnit.SECONDS) + .untilAsserted(() -> assertConnectionPoolMetrics(tomcatDataSource.getPoolName())); + + // when + // this one too shouldn't cause any problems when called more than once + tomcatDataSource.close(); + tomcatDataSource.close(); + testing.clearData(); + + // then + await() + .atMost(20, TimeUnit.SECONDS) + .untilAsserted(TomcatJdbcInstrumentationTest::assertNoConnectionPoolMetrics); + } + + private static void assertConnectionPoolMetrics(String poolName) { + assertThat(poolName) + .as("tomcat-jdbc generates a unique pool name if it's not explicitly provided") + .isNotEmpty(); + + DbConnectionPoolMetricsAssertions.create(testing, "io.opentelemetry.tomcat-jdbc", poolName) + // no timeouts happen during this test + .disableConnectionTimeouts() + .disableCreateTime() + .disableWaitTime() + .disableUseTime() + .assertConnectionPoolEmitsMetrics(); + } + + private static void assertNoConnectionPoolMetrics() { + testing.waitAndAssertMetrics( + "io.opentelemetry.tomcat-jdbc", + "db.client.connections.usage", + AbstractIterableAssert::isEmpty); + testing.waitAndAssertMetrics( + "io.opentelemetry.tomcat-jdbc", + "db.client.connections.idle.min", + AbstractIterableAssert::isEmpty); + testing.waitAndAssertMetrics( + "io.opentelemetry.tomcat-jdbc", + "db.client.connections.idle.max", + AbstractIterableAssert::isEmpty); + testing.waitAndAssertMetrics( + "io.opentelemetry.tomcat-jdbc", + "db.client.connections.max", + AbstractIterableAssert::isEmpty); + testing.waitAndAssertMetrics( + "io.opentelemetry.tomcat-jdbc", + "db.client.connections.pending_requests", + AbstractIterableAssert::isEmpty); + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 47416970b032..cdc942c4a988 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -432,6 +432,7 @@ include(":instrumentation:tapestry-5.4:javaagent") include(":instrumentation:tomcat:tomcat-7.0:javaagent") include(":instrumentation:tomcat:tomcat-10.0:javaagent") include(":instrumentation:tomcat:tomcat-common:javaagent") +include(":instrumentation:tomcat:tomcat-jdbc") include(":instrumentation:twilio-6.6:javaagent") include(":instrumentation:undertow-1.4:bootstrap") include(":instrumentation:undertow-1.4:javaagent") From 70359bbba2b2114ac42a85a69673500177edb0ae Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 26 May 2022 17:17:31 -0700 Subject: [PATCH 2/7] use duration --- .../tomcat/jdbc/TomcatJdbcInstrumentationTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java index de943f7ba078..3755b411a59a 100644 --- a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java +++ b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java @@ -12,7 +12,7 @@ import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.db.DbConnectionPoolMetricsAssertions; import java.sql.Connection; -import java.util.concurrent.TimeUnit; +import java.time.Duration; import org.apache.tomcat.jdbc.pool.DataSource; import org.assertj.core.api.AbstractIterableAssert; import org.junit.jupiter.api.Test; @@ -51,7 +51,7 @@ void shouldReportMetrics() throws Exception { // then await() - .atMost(20, TimeUnit.SECONDS) + .atMost(Duration.ofSeconds(20)) .untilAsserted(() -> assertConnectionPoolMetrics(tomcatDataSource.getPoolName())); // when @@ -62,7 +62,7 @@ void shouldReportMetrics() throws Exception { // then await() - .atMost(20, TimeUnit.SECONDS) + .atMost(Duration.ofSeconds(20)) .untilAsserted(TomcatJdbcInstrumentationTest::assertNoConnectionPoolMetrics); } From fe5383000d2d636c651123869e3ec5122dfd1d61 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 27 May 2022 08:03:23 -0700 Subject: [PATCH 3/7] code review comments --- .../tomcat/jdbc/TomcatJdbcInstrumentationModule.java | 5 ----- .../tomcat/jdbc/TomcatJdbcInstrumentationTest.java | 3 --- 2 files changed, 8 deletions(-) diff --git a/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationModule.java b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationModule.java index 73dd931be33a..177f9bdd3773 100644 --- a/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationModule.java +++ b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationModule.java @@ -18,11 +18,6 @@ public TomcatJdbcInstrumentationModule() { super("tomcat-jdbc"); } - @Override - public boolean isHelperClass(String className) { - return className.startsWith("io.opentelemetry.javaagent.instrumentation"); - } - @Override public List typeInstrumentations() { return singletonList(new DataSourceProxyInstrumentation()); diff --git a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java index 3755b411a59a..80da76c0dfec 100644 --- a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java +++ b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java @@ -24,12 +24,9 @@ @ExtendWith(MockitoExtension.class) public class TomcatJdbcInstrumentationTest { - // @RegisterExtension - // static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); @RegisterExtension static final AgentInstrumentationExtension testing = AgentInstrumentationExtension.create(); - // @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); @Mock javax.sql.DataSource dataSourceMock; @Mock Connection connectionMock; From 7f516d8592d5add402298ee259e4b836b97bc1ba Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 27 May 2022 14:09:08 -0700 Subject: [PATCH 4/7] remove unnecessary awaits --- .../tomcat/jdbc/TomcatJdbcInstrumentationTest.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java index 80da76c0dfec..d45b65e42b00 100644 --- a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java +++ b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java @@ -6,13 +6,11 @@ package io.opentelemetry.javaagent.instrumentation.tomcat.jdbc; import static org.assertj.core.api.Assertions.assertThat; -import static org.awaitility.Awaitility.await; import static org.mockito.BDDMockito.given; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.db.DbConnectionPoolMetricsAssertions; import java.sql.Connection; -import java.time.Duration; import org.apache.tomcat.jdbc.pool.DataSource; import org.assertj.core.api.AbstractIterableAssert; import org.junit.jupiter.api.Test; @@ -47,9 +45,7 @@ void shouldReportMetrics() throws Exception { connection.close(); // then - await() - .atMost(Duration.ofSeconds(20)) - .untilAsserted(() -> assertConnectionPoolMetrics(tomcatDataSource.getPoolName())); + assertConnectionPoolMetrics(tomcatDataSource.getPoolName()); // when // this one too shouldn't cause any problems when called more than once @@ -58,9 +54,7 @@ void shouldReportMetrics() throws Exception { testing.clearData(); // then - await() - .atMost(Duration.ofSeconds(20)) - .untilAsserted(TomcatJdbcInstrumentationTest::assertNoConnectionPoolMetrics); + assertNoConnectionPoolMetrics(); } private static void assertConnectionPoolMetrics(String poolName) { From 847b352744522609c818458ddd5fab2d836423e2 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 27 May 2022 14:12:15 -0700 Subject: [PATCH 5/7] udpate supported-libraries.md --- docs/supported-libraries.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/supported-libraries.md b/docs/supported-libraries.md index 8b92cfd52e67..b6adc75094f8 100644 --- a/docs/supported-libraries.md +++ b/docs/supported-libraries.md @@ -110,6 +110,7 @@ These are the supported libraries and frameworks: | [Spring Web Services](https://spring.io/projects/spring-ws) | 2.0+ | | [Spring WebFlux](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/package-summary.html) | 5.0+ | | [Spymemcached](https://github.com/couchbase/spymemcached) | 2.12+ | +| [Tomcat JDBC Pool](https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html) | 8.5.0+ | | [Twilio](https://github.com/twilio/twilio-java) | 6.6+ (not including 8.x yet) | | [Undertow](https://undertow.io/) | 1.4+ | | [Vaadin](https://vaadin.com/) | 14.2+ | From 5e211819320062339b835b4943c563b9c5abd3a8 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 27 May 2022 14:17:17 -0700 Subject: [PATCH 6/7] add comment about weakmap --- .../tomcat/jdbc/TomcatConnectionPoolMetrics.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatConnectionPoolMetrics.java b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatConnectionPoolMetrics.java index 506de0da75e8..79734df8e8b8 100644 --- a/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatConnectionPoolMetrics.java +++ b/instrumentation/tomcat/tomcat-jdbc/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatConnectionPoolMetrics.java @@ -20,6 +20,9 @@ public final class TomcatConnectionPoolMetrics { private static final OpenTelemetry openTelemetry = GlobalOpenTelemetry.get(); private static final String INSTRUMENTATION_NAME = "io.opentelemetry.tomcat-jdbc"; + // a weak map does not make sense here because each Meter holds a reference to the dataSource + // DataSourceProxy does not implement equals()/hashCode(), so it's safe to keep them in a plain + // ConcurrentHashMap private static final Map> dataSourceMetrics = new ConcurrentHashMap<>(); From bea29aef5e9485767bf83ac1c6928cafeaf1c23c Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 2 Jun 2022 15:59:40 -0700 Subject: [PATCH 7/7] add sleeps for safety --- .../tomcat/jdbc/TomcatJdbcInstrumentationTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java index d45b65e42b00..c2902c228fe8 100644 --- a/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java +++ b/instrumentation/tomcat/tomcat-jdbc/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/jdbc/TomcatJdbcInstrumentationTest.java @@ -42,6 +42,7 @@ void shouldReportMetrics() throws Exception { // when Connection connection = tomcatDataSource.getConnection(); + Thread.sleep(100); connection.close(); // then @@ -51,7 +52,9 @@ void shouldReportMetrics() throws Exception { // this one too shouldn't cause any problems when called more than once tomcatDataSource.close(); tomcatDataSource.close(); + Thread.sleep(100); testing.clearData(); + Thread.sleep(100); // then assertNoConnectionPoolMetrics();