From 8f7c78ed64d1c96027f990b6f35199d2c1f8d8c4 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Fri, 15 Jul 2022 09:59:28 +0300 Subject: [PATCH] Hikaricp: Avoid registering duplicate metrics (#6325) * Hikaricp: Avoid registering duplicate metrics * Update instrumentation/hikaricp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hikaricp/HikariPoolInstrumentation.java Co-authored-by: Trask Stalnaker * spotless * Trigger build Co-authored-by: Trask Stalnaker --- .../hikaricp/HikariPoolInstrumentation.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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); } }