diff --git a/instrumentation/jdbc/src/main/java/com/appoptics/opentelemetry/instrumentation/AoPreparedStatementInstrumentation.java b/instrumentation/jdbc/src/main/java/com/appoptics/opentelemetry/instrumentation/AoPreparedStatementInstrumentation.java index d0621fca..fa377dba 100644 --- a/instrumentation/jdbc/src/main/java/com/appoptics/opentelemetry/instrumentation/AoPreparedStatementInstrumentation.java +++ b/instrumentation/jdbc/src/main/java/com/appoptics/opentelemetry/instrumentation/AoPreparedStatementInstrumentation.java @@ -58,6 +58,7 @@ public static void onEnter( // is called - the first recursive Statement call is just skipped and we do not create a span // for it if (CallDepth.forClass(Statement.class).getAndIncrement() != 0) { //only report back when depth is one to avoid duplications + CallDepth.forClass(Statement.class).decrementAndGet(); return; } QueryArgsCollector.collect(currentContext(), index, value); @@ -92,6 +93,9 @@ public static void onEnter( public static void onExit( @Advice.Thrown Throwable throwable) { if (CallDepth.forClass(Statement.class).getAndIncrement() != 1) { //only report back when depth is one to avoid duplications + // Note that we need to decrement the call depth counter at every branch, otherwise the JDBC instrumentation of the + // Otel agent will break. + CallDepth.forClass(Statement.class).decrementAndGet(); return; } QueryArgsCollector.maybeAttach(currentContext()); diff --git a/instrumentation/jdbc/src/main/java/com/appoptics/opentelemetry/instrumentation/AoStatementInstrumentation.java b/instrumentation/jdbc/src/main/java/com/appoptics/opentelemetry/instrumentation/AoStatementInstrumentation.java index 3764365c..e285d0fb 100644 --- a/instrumentation/jdbc/src/main/java/com/appoptics/opentelemetry/instrumentation/AoStatementInstrumentation.java +++ b/instrumentation/jdbc/src/main/java/com/appoptics/opentelemetry/instrumentation/AoStatementInstrumentation.java @@ -48,6 +48,9 @@ public static class StatementAdvice { @Advice.OnMethodEnter public static void onEnter(@Advice.Argument(value = 0, readOnly = false) String sql) { if (CallDepth.forClass(Statement.class).getAndIncrement() != 1) { //only report back when depth is one to avoid duplications + // Note that we need to decrement the call depth counter at every branch, otherwise the JDBC instrumentation of the + // Otel agent will break. + CallDepth.forClass(Statement.class).decrementAndGet(); return; } sql = TraceContextInjector.inject(currentContext(), sql);