Use new CallDepth pattern for hibernate spans
MODIFY
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/CriteriaInstrumentation.java
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/CriteriaInstrumentation.java
@@ -15,6 +15,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
+import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
@@ -50,9 +51,15 @@ public class CriteriaInstrumentation implements TypeInstrumentation {
public static void startMethod(
@Advice.This Criteria criteria,
@Advice.Origin("#m") String name,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
ContextStore<Criteria, Context> contextStore =
InstrumentationContext.get(Criteria.class, Context.class);
@@ -67,12 +74,17 @@ public class CriteriaInstrumentation implements TypeInstrumentation {
@Advice.Thrown Throwable throwable,
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object entity,
@Advice.Origin("#m") String name,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
- SessionMethodUtils.end(context, throwable, "Criteria." + name, entity);
scope.close();
+ SessionMethodUtils.end(context, throwable, "Criteria." + name, entity);
}
}
}

MODIFY
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/QueryInstrumentation.java
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/QueryInstrumentation.java
@@ -15,6 +15,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
+import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
@@ -48,9 +49,15 @@ public class QueryInstrumentation implements TypeInstrumentation {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void startMethod(
@Advice.This Query query,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
ContextStore<Query, Context> contextStore =
InstrumentationContext.get(Query.class, Context.class);
@@ -63,12 +70,17 @@ public class QueryInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
- SessionMethodUtils.end(context, throwable, null, null);
scope.close();
+ SessionMethodUtils.end(context, throwable, null, null);
}
}
}

MODIFY
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java
@@ -132,9 +132,15 @@ public class SessionInstrumentation implements TypeInstrumentation {
@Advice.This Object session,
@Advice.Origin("#m") String name,
@Advice.Argument(0) Object entity,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context spanContext,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
Context sessionContext = null;
if (session instanceof Session) {
ContextStore<Session, Context> contextStore =
@@ -150,10 +156,6 @@ public class SessionInstrumentation implements TypeInstrumentation {
return; // No state found. We aren't in a Session.
}
- if (CallDepth.forClass(SessionMethodUtils.class).getAndIncrement() > 0) {
- return; // This method call is being traced already.
- }
-
if (!SCOPE_ONLY_METHODS.contains(name)) {
spanContext = tracer().startSpan(sessionContext, "Session." + name, entity);
scope = spanContext.makeCurrent();
@@ -167,9 +169,14 @@ public class SessionInstrumentation implements TypeInstrumentation {
@Advice.Thrown Throwable throwable,
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object returned,
@Advice.Origin("#m") String name,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context spanContext,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
scope.close();
SessionMethodUtils.end(spanContext, throwable, "Session." + name, returned);

MODIFY
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/TransactionInstrumentation.java
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/TransactionInstrumentation.java
@@ -15,6 +15,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
+import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
@@ -48,9 +49,15 @@ public class TransactionInstrumentation implements TypeInstrumentation {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void startCommit(
@Advice.This Transaction transaction,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
ContextStore<Transaction, Context> contextStore =
InstrumentationContext.get(Transaction.class, Context.class);
@@ -64,12 +71,17 @@ public class TransactionInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endCommit(
@Advice.Thrown Throwable throwable,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
- SessionMethodUtils.end(context, throwable, null, null);
scope.close();
+ SessionMethodUtils.end(context, throwable, null, null);
}
}
}

MODIFY
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/CriteriaInstrumentation.java
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/CriteriaInstrumentation.java
@@ -15,6 +15,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
+import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
@@ -50,9 +51,15 @@ public class CriteriaInstrumentation implements TypeInstrumentation {
public static void startMethod(
@Advice.This Criteria criteria,
@Advice.Origin("#m") String name,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
ContextStore<Criteria, Context> contextStore =
InstrumentationContext.get(Criteria.class, Context.class);
@@ -67,9 +74,14 @@ public class CriteriaInstrumentation implements TypeInstrumentation {
@Advice.Thrown Throwable throwable,
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object entity,
@Advice.Origin("#m") String name,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, "Criteria." + name, entity);

MODIFY
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/QueryInstrumentation.java
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/QueryInstrumentation.java
@@ -15,6 +15,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
+import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
@@ -48,9 +49,15 @@ public class QueryInstrumentation implements TypeInstrumentation {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void startMethod(
@Advice.This Query query,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
ContextStore<Query, Context> contextStore =
InstrumentationContext.get(Query.class, Context.class);
@@ -63,9 +70,14 @@ public class QueryInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, null, null);

MODIFY
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java
@@ -121,9 +121,15 @@ public class SessionInstrumentation implements TypeInstrumentation {
@Advice.This SharedSessionContract session,
@Advice.Origin("#m") String name,
@Advice.Argument(0) Object entity,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context spanContext,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
ContextStore<SharedSessionContract, Context> contextStore =
InstrumentationContext.get(SharedSessionContract.class, Context.class);
Context sessionContext = contextStore.get(session);
@@ -132,10 +138,6 @@ public class SessionInstrumentation implements TypeInstrumentation {
return; // No state found. We aren't in a Session.
}
- if (CallDepth.forClass(SessionMethodUtils.class).getAndIncrement() > 0) {
- return; // This method call is being traced already.
- }
-
if (!SCOPE_ONLY_METHODS.contains(name)) {
spanContext = tracer().startSpan(sessionContext, "Session." + name, entity);
scope = spanContext.makeCurrent();
@@ -149,9 +151,14 @@ public class SessionInstrumentation implements TypeInstrumentation {
@Advice.Thrown Throwable throwable,
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object returned,
@Advice.Origin("#m") String name,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context spanContext,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
scope.close();
SessionMethodUtils.end(spanContext, throwable, "Session." + name, returned);

MODIFY
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/TransactionInstrumentation.java
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/TransactionInstrumentation.java
@@ -15,6 +15,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
+import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
@@ -48,9 +49,15 @@ public class TransactionInstrumentation implements TypeInstrumentation {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void startCommit(
@Advice.This Transaction transaction,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
ContextStore<Transaction, Context> contextStore =
InstrumentationContext.get(Transaction.class, Context.class);
@@ -64,9 +71,14 @@ public class TransactionInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endCommit(
@Advice.Thrown Throwable throwable,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, null, null);

MODIFY
instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/SessionMethodUtils.java
instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/SessionMethodUtils.java
@@ -11,7 +11,6 @@ import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.db.SqlStatementInfo;
import io.opentelemetry.instrumentation.api.db.SqlStatementSanitizer;
-import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import java.util.Arrays;
import java.util.HashSet;
@@ -43,11 +42,6 @@ public final class SessionMethodUtils {
return null; // No state found. We aren't in a Session.
}
- int depth = CallDepth.forClass(SessionMethodUtils.class).getAndIncrement();
- if (depth > 0) {
- return null; // This method call is being traced already.
- }
-
return tracer().startSpan(sessionContext, operationNameSupplier.get(), entity);
}
@@ -73,8 +67,6 @@ public final class SessionMethodUtils {
public static void end(
@Nullable Context context, Throwable throwable, String operationName, Object entity) {
- CallDepth.forClass(SessionMethodUtils.class).reset();
-
if (context == null) {
return;
}

MODIFY
instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/ProcedureCallInstrumentation.java
instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/ProcedureCallInstrumentation.java
@@ -14,6 +14,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
+import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
@@ -48,9 +49,15 @@ public class ProcedureCallInstrumentation implements TypeInstrumentation {
public static void startMethod(
@Advice.This ProcedureCall call,
@Advice.Origin("#m") String name,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ callDepth = CallDepth.forClass(SessionMethodUtils.class);
+ if (callDepth.getAndIncrement() > 0) {
+ return;
+ }
+
ContextStore<ProcedureCall, Context> contextStore =
InstrumentationContext.get(ProcedureCall.class, Context.class);
@@ -65,9 +72,14 @@ public class ProcedureCallInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable,
+ @Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
+ if (callDepth.decrementAndGet() > 0) {
+ return;
+ }
+
if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, null, null);

Use new CallDepth pattern for hibernate spans (#3510)