diff --git a/instrumentation/hikaricp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hikaricp/HikariPoolInstrumentation.java b/instrumentation/hikaricp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hikaricp/HikariPoolInstrumentation.java index 703c7d0c52f8..d93e1bb59491 100644 --- a/instrumentation/hikaricp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hikaricp/HikariPoolInstrumentation.java +++ b/instrumentation/hikaricp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hikaricp/HikariPoolInstrumentation.java @@ -39,8 +39,16 @@ public static class SetMetricsTrackerFactoryAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onEnter( - @Advice.Argument(value = 0, readOnly = false) MetricsTrackerFactory userMetricsTracker) { - + @Advice.Argument(value = 0, readOnly = false) MetricsTrackerFactory userMetricsTracker, + @Advice.FieldValue(value = "metricsTracker") AutoCloseable existingMetricsTracker) + throws Exception { + + if (existingMetricsTracker != null) { + // we call close on the existing metrics tracker in case it's our wrapper, so that our + // wrapper will unregister itself and won't keep recording metrics which leads to warnings + // about duplicate metrics + existingMetricsTracker.close(); + } userMetricsTracker = HikariSingletons.createMetricsTrackerFactory(userMetricsTracker); } }