From 101d88fe59c87356b56b057d09f641e20b86000d Mon Sep 17 00:00:00 2001 From: SylvainJuge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:05:03 +0200 Subject: [PATCH] make hibernate-* indy compatible (#11553) --- .../v3_3/CriteriaInstrumentation.java | 43 +++------ .../v3_3/HibernateInstrumentationModule.java | 9 +- .../hibernate/v3_3/QueryInstrumentation.java | 38 ++------ .../v3_3/SessionInstrumentation.java | 41 +++------ .../v3_3/TransactionInstrumentation.java | 39 ++------ .../v4_0/CriteriaInstrumentation.java | 43 +++------ .../v4_0/HibernateInstrumentationModule.java | 10 +-- .../hibernate/v4_0/QueryInstrumentation.java | 38 ++------ .../v4_0/SessionInstrumentation.java | 41 +++------ .../v4_0/TransactionInstrumentation.java | 39 ++------ .../v6_0/HibernateInstrumentationModule.java | 10 +-- .../hibernate/v6_0/QueryInstrumentation.java | 40 ++------- .../v6_0/SessionInstrumentation.java | 41 +++------ .../v6_0/TransactionInstrumentation.java | 39 ++------ .../hibernate/HibernateOperationScope.java | 88 +++++++++++++++++++ .../v4_3/HibernateInstrumentationModule.java | 10 +-- .../v4_3/ProcedureCallInstrumentation.java | 40 ++------- 17 files changed, 224 insertions(+), 385 deletions(-) create mode 100644 instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/HibernateOperationScope.java diff --git a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/CriteriaInstrumentation.java b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/CriteriaInstrumentation.java index e995738cd723..962d82fea4b3 100644 --- a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/CriteriaInstrumentation.java +++ b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/CriteriaInstrumentation.java @@ -13,13 +13,12 @@ import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -50,17 +49,11 @@ public void transform(TypeTransformer transformer) { public static class CriteriaMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( - @Advice.This Criteria criteria, - @Advice.Origin("#m") String name, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + public static HibernateOperationScope startMethod( + @Advice.This Criteria criteria, @Advice.Origin("#m") String name) { + + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } String entityName = null; @@ -73,31 +66,17 @@ public static void startMethod( SessionInfo sessionInfo = criteriaVirtualField.get(criteria); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = new HibernateOperation("Criteria." + name, entityName, sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } + HibernateOperation hibernateOperation = + new HibernateOperation("Criteria." + name, entityName, sessionInfo); - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } } diff --git a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/HibernateInstrumentationModule.java b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/HibernateInstrumentationModule.java index d9f6d5a919ac..8a9dd3a82040 100644 --- a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/HibernateInstrumentationModule.java +++ b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/HibernateInstrumentationModule.java @@ -11,11 +11,13 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.List; import net.bytebuddy.matcher.ElementMatcher; @AutoService(InstrumentationModule.class) -public class HibernateInstrumentationModule extends InstrumentationModule { +public class HibernateInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public HibernateInstrumentationModule() { super("hibernate", "hibernate-3.3"); @@ -30,6 +32,11 @@ public ElementMatcher.Junction classLoaderMatcher() { "org.hibernate.transaction.JBossTransactionManagerLookup"); } + @Override + public String getModuleGroup() { + return "hibernate"; + } + @Override public List typeInstrumentations() { return asList( diff --git a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/QueryInstrumentation.java b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/QueryInstrumentation.java index 5a9742dcbcd0..e978128b4b22 100644 --- a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/QueryInstrumentation.java +++ b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/QueryInstrumentation.java @@ -14,13 +14,12 @@ import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -50,16 +49,10 @@ public void transform(TypeTransformer transformer) { public static class QueryMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( - @Advice.This Query query, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + public static HibernateOperationScope startMethod(@Advice.This Query query) { - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } VirtualField queryVirtualField = @@ -67,32 +60,17 @@ public static void startMethod( SessionInfo sessionInfo = queryVirtualField.get(query); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = + HibernateOperation hibernateOperation = new HibernateOperation(getOperationNameForQuery(query.getQueryString()), sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } } diff --git a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java index 8cdefda3d53d..b01cd3b5bc7a 100644 --- a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java @@ -18,13 +18,12 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -92,52 +91,32 @@ public void transform(TypeTransformer transformer) { public static class SessionMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( + public static HibernateOperationScope startMethod( @Advice.This Object session, @Advice.Origin("#m") String name, @Advice.Origin("#d") String descriptor, @Advice.Argument(0) Object arg0, - @Advice.Argument(value = 1, optional = true) Object arg1, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + @Advice.Argument(value = 1, optional = true) Object arg1) { + + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } Context parentContext = Java8BytecodeBridge.currentContext(); SessionInfo sessionInfo = SessionUtil.getSessionInfo(session); String entityName = getEntityName(descriptor, arg0, arg1, EntityNameUtil.bestGuessEntityName(session)); - hibernateOperation = + HibernateOperation hibernateOperation = new HibernateOperation(getSessionMethodOperationName(name), entityName, sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope enterScope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(enterScope, throwable); } } diff --git a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/TransactionInstrumentation.java b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/TransactionInstrumentation.java index c9f8631f54b8..4004151e1cd2 100644 --- a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/TransactionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/TransactionInstrumentation.java @@ -13,13 +13,12 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -49,16 +48,10 @@ public void transform(TypeTransformer transformer) { public static class TransactionCommitAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startCommit( - @Advice.This Transaction transaction, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + public static HibernateOperationScope startCommit(@Advice.This Transaction transaction) { - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } VirtualField transactionVirtualField = @@ -66,31 +59,17 @@ public static void startCommit( SessionInfo sessionInfo = transactionVirtualField.get(transaction); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = new HibernateOperation("Transaction.commit", sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } + HibernateOperation hibernateOperation = + new HibernateOperation("Transaction.commit", sessionInfo); - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endCommit( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } } diff --git a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/CriteriaInstrumentation.java b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/CriteriaInstrumentation.java index aec640bc6b07..495d6f9c595b 100644 --- a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/CriteriaInstrumentation.java +++ b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/CriteriaInstrumentation.java @@ -13,13 +13,12 @@ import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -50,17 +49,11 @@ public void transform(TypeTransformer transformer) { public static class CriteriaMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( - @Advice.This Criteria criteria, - @Advice.Origin("#m") String name, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + public static HibernateOperationScope startMethod( + @Advice.This Criteria criteria, @Advice.Origin("#m") String name) { + + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } String entityName = null; @@ -73,31 +66,17 @@ public static void startMethod( SessionInfo sessionInfo = criteriaVirtualField.get(criteria); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = new HibernateOperation("Criteria." + name, entityName, sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } + HibernateOperation hibernateOperation = + new HibernateOperation("Criteria." + name, entityName, sessionInfo); - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } } diff --git a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/HibernateInstrumentationModule.java b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/HibernateInstrumentationModule.java index 9685997529c2..e8a919d13c37 100644 --- a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/HibernateInstrumentationModule.java +++ b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/HibernateInstrumentationModule.java @@ -11,11 +11,13 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.List; import net.bytebuddy.matcher.ElementMatcher; @AutoService(InstrumentationModule.class) -public class HibernateInstrumentationModule extends InstrumentationModule { +public class HibernateInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public HibernateInstrumentationModule() { super("hibernate", "hibernate-4.0"); @@ -31,10 +33,8 @@ public ElementMatcher.Junction classLoaderMatcher() { } @Override - public boolean isIndyModule() { - // shares classes with hibernate-procedure-call-4.3, these classes should be in the same class - // loader - return false; + public String getModuleGroup() { + return "hibernate"; } @Override diff --git a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/QueryInstrumentation.java b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/QueryInstrumentation.java index af0618e6c773..635634ea73fc 100644 --- a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/QueryInstrumentation.java +++ b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/QueryInstrumentation.java @@ -14,13 +14,12 @@ import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -50,16 +49,10 @@ public void transform(TypeTransformer transformer) { public static class QueryMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( - @Advice.This Query query, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + public static HibernateOperationScope startMethod(@Advice.This Query query) { - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } VirtualField queryVirtualField = @@ -67,32 +60,17 @@ public static void startMethod( SessionInfo sessionInfo = queryVirtualField.get(query); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = + HibernateOperation hibernateOperation = new HibernateOperation(getOperationNameForQuery(query.getQueryString()), sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } } diff --git a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java index e5e9fbc5116e..9457fbfbee51 100644 --- a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java @@ -18,13 +18,12 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -96,20 +95,15 @@ public void transform(TypeTransformer transformer) { public static class SessionMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( + public static HibernateOperationScope startMethod( @Advice.This SharedSessionContract session, @Advice.Origin("#m") String name, @Advice.Origin("#d") String descriptor, @Advice.Argument(0) Object arg0, - @Advice.Argument(value = 1, optional = true) Object arg1, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + @Advice.Argument(value = 1, optional = true) Object arg1) { + + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } VirtualField virtualField = @@ -119,32 +113,17 @@ public static void startMethod( Context parentContext = Java8BytecodeBridge.currentContext(); String entityName = getEntityName(descriptor, arg0, arg1, EntityNameUtil.bestGuessEntityName(session)); - hibernateOperation = + HibernateOperation hibernateOperation = new HibernateOperation(getSessionMethodOperationName(name), entityName, sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } diff --git a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/TransactionInstrumentation.java b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/TransactionInstrumentation.java index 16be5e006ad9..738cf85db6b1 100644 --- a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/TransactionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/TransactionInstrumentation.java @@ -13,13 +13,12 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -49,16 +48,10 @@ public void transform(TypeTransformer transformer) { public static class TransactionCommitAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startCommit( - @Advice.This Transaction transaction, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + public static HibernateOperationScope startCommit(@Advice.This Transaction transaction) { - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } VirtualField transactionVirtualField = @@ -66,31 +59,17 @@ public static void startCommit( SessionInfo sessionInfo = transactionVirtualField.get(transaction); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = new HibernateOperation("Transaction.commit", sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } + HibernateOperation hibernateOperation = + new HibernateOperation("Transaction.commit", sessionInfo); - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endCommit( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } } diff --git a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/HibernateInstrumentationModule.java b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/HibernateInstrumentationModule.java index b8c8a34dd120..83af9e7b0f90 100644 --- a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/HibernateInstrumentationModule.java +++ b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/HibernateInstrumentationModule.java @@ -11,11 +11,13 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.List; import net.bytebuddy.matcher.ElementMatcher; @AutoService(InstrumentationModule.class) -public class HibernateInstrumentationModule extends InstrumentationModule { +public class HibernateInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public HibernateInstrumentationModule() { super("hibernate", "hibernate-6.0"); @@ -29,10 +31,8 @@ public ElementMatcher.Junction classLoaderMatcher() { } @Override - public boolean isIndyModule() { - // shares classes with hibernate-procedure-call-4.3, these classes should be in the same class - // loader - return false; + public String getModuleGroup() { + return "hibernate"; } @Override diff --git a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/QueryInstrumentation.java b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/QueryInstrumentation.java index 141175c30269..56ebecb8a2af 100644 --- a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/QueryInstrumentation.java +++ b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/QueryInstrumentation.java @@ -14,13 +14,12 @@ import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -64,16 +63,10 @@ public void transform(TypeTransformer transformer) { public static class QueryMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( - @Advice.This CommonQueryContract query, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + public static HibernateOperationScope startMethod(@Advice.This CommonQueryContract query) { + + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } String queryString = null; @@ -93,32 +86,17 @@ public static void startMethod( SessionInfo sessionInfo = queryVirtualField.get(query); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = + HibernateOperation hibernateOperation = new HibernateOperation(getOperationNameForQuery(queryString), sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } } diff --git a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionInstrumentation.java b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionInstrumentation.java index fde18f963d42..6853916ca629 100644 --- a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionInstrumentation.java @@ -18,13 +18,12 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import jakarta.persistence.criteria.CriteriaQuery; import net.bytebuddy.asm.Advice; @@ -95,20 +94,15 @@ public void transform(TypeTransformer transformer) { public static class SessionMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( + public static HibernateOperationScope startMethod( @Advice.This SharedSessionContract session, @Advice.Origin("#m") String name, @Advice.Origin("#d") String descriptor, @Advice.Argument(0) Object arg0, - @Advice.Argument(value = 1, optional = true) Object arg1, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + @Advice.Argument(value = 1, optional = true) Object arg1) { + + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } VirtualField virtualField = @@ -118,32 +112,17 @@ public static void startMethod( Context parentContext = Java8BytecodeBridge.currentContext(); String entityName = getEntityName(descriptor, arg0, arg1, EntityNameUtil.bestGuessEntityName(session)); - hibernateOperation = + HibernateOperation hibernateOperation = new HibernateOperation(getSessionMethodOperationName(name), entityName, sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (callDepth.decrementAndGet() > 0) { - return; - } - - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } diff --git a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/TransactionInstrumentation.java b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/TransactionInstrumentation.java index 711773e4e5b4..cea7315139c2 100644 --- a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/TransactionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/TransactionInstrumentation.java @@ -13,13 +13,12 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -49,16 +48,10 @@ public void transform(TypeTransformer transformer) { public static class TransactionCommitAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startCommit( - @Advice.This Transaction transaction, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + public static HibernateOperationScope startCommit(@Advice.This Transaction transaction) { - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } VirtualField transactionVirtualField = @@ -66,31 +59,17 @@ public static void startCommit( SessionInfo sessionInfo = transactionVirtualField.get(transaction); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = new HibernateOperation("Transaction.commit", sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } + HibernateOperation hibernateOperation = + new HibernateOperation("Transaction.commit", sessionInfo); - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endCommit( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } } diff --git a/instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/HibernateOperationScope.java b/instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/HibernateOperationScope.java new file mode 100644 index 000000000000..6c8831718da1 --- /dev/null +++ b/instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/HibernateOperationScope.java @@ -0,0 +1,88 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.hibernate; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import io.opentelemetry.javaagent.bootstrap.CallDepth; + +public class HibernateOperationScope { + + private final HibernateOperation hibernateOperation; + private final Context context; + private final Scope scope; + private final Instrumenter instrumenter; + + private HibernateOperationScope( + HibernateOperation hibernateOperation, + Context context, + Scope scope, + Instrumenter instrumenter) { + this.hibernateOperation = hibernateOperation; + this.context = context; + this.scope = scope; + this.instrumenter = instrumenter; + } + + /** + * Starts operation scope + * + * @param hibernateOperation hibernate operation + * @param parentContext parent context + * @param instrumenter instrumenter + * @return operation scope, to be ended with {@link #end(HibernateOperationScope, Throwable)} on + * exit advice. Might return {@literal null} when operation should not be captured. + */ + public static HibernateOperationScope start( + HibernateOperation hibernateOperation, + Context parentContext, + Instrumenter instrumenter) { + + if (!instrumenter.shouldStart(parentContext, hibernateOperation)) { + return null; + } + + Context context = instrumenter.start(parentContext, hibernateOperation); + + return new HibernateOperationScope( + hibernateOperation, context, context.makeCurrent(), instrumenter); + } + + /** + * Performs call depth increase and returns {@literal true} when depth is > 0, which indicates a + * nested hibernate operation is in progress. Must be called in the enter advice with an + * unconditional corresponding call to {@link #end(HibernateOperationScope, Throwable)} to + * decrement call depth. + */ + public static boolean enterDepthSkipCheck() { + CallDepth callDepth = CallDepth.forClass(HibernateOperation.class); + return callDepth.getAndIncrement() > 0; + } + + /** + * Ends operation scope. + * + * @param scope hibernate operation scope or {@literal null} when there is none + * @param throwable thrown exception + */ + public static void end(HibernateOperationScope scope, Throwable throwable) { + + CallDepth callDepth = CallDepth.forClass(HibernateOperation.class); + if (callDepth.decrementAndGet() > 0) { + return; + } + + if (scope != null) { + scope.end(throwable); + } + } + + private void end(Throwable throwable) { + scope.close(); + instrumenter.end(context, hibernateOperation, null, throwable); + } +} diff --git a/instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/HibernateInstrumentationModule.java b/instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/HibernateInstrumentationModule.java index 11b04b34903e..3e914808d004 100644 --- a/instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/HibernateInstrumentationModule.java +++ b/instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/HibernateInstrumentationModule.java @@ -11,11 +11,13 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.List; import net.bytebuddy.matcher.ElementMatcher; @AutoService(InstrumentationModule.class) -public class HibernateInstrumentationModule extends InstrumentationModule { +public class HibernateInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public HibernateInstrumentationModule() { super("hibernate-procedure-call", "hibernate-procedure-call-4.3", "hibernate"); } @@ -26,10 +28,8 @@ public ElementMatcher.Junction classLoaderMatcher() { } @Override - public boolean isIndyModule() { - // uses SessionInfo class from hibernate common which is now in separate class loader for all - // instrumentations - return false; + public String getModuleGroup() { + return "hibernate"; } @Override diff --git a/instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/ProcedureCallInstrumentation.java b/instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/ProcedureCallInstrumentation.java index ed9671808c2f..db6092567598 100644 --- a/instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/ProcedureCallInstrumentation.java +++ b/instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/ProcedureCallInstrumentation.java @@ -12,13 +12,12 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.util.VirtualField; -import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation; +import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -48,17 +47,11 @@ public void transform(TypeTransformer transformer) { public static class ProcedureCallMethodAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void startMethod( - @Advice.This ProcedureCall call, - @Advice.Origin("#m") String name, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + public static HibernateOperationScope startMethod( + @Advice.This ProcedureCall call, @Advice.Origin("#m") String name) { - callDepth = CallDepth.forClass(HibernateOperation.class); - if (callDepth.getAndIncrement() > 0) { - return; + if (HibernateOperationScope.enterDepthSkipCheck()) { + return null; } VirtualField criteriaVirtualField = @@ -66,32 +59,17 @@ public static void startMethod( SessionInfo sessionInfo = criteriaVirtualField.get(call); Context parentContext = Java8BytecodeBridge.currentContext(); - hibernateOperation = + HibernateOperation hibernateOperation = new HibernateOperation("ProcedureCall." + name, call.getProcedureName(), sessionInfo); - if (!instrumenter().shouldStart(parentContext, hibernateOperation)) { - return; - } - context = instrumenter().start(parentContext, hibernateOperation); - scope = context.makeCurrent(); + return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void endMethod( - @Advice.Thrown Throwable throwable, - @Advice.Local("otelCallDepth") CallDepth callDepth, - @Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (callDepth.decrementAndGet() > 0) { - return; - } + @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) { - if (scope != null) { - scope.close(); - instrumenter().end(context, hibernateOperation, null, throwable); - } + HibernateOperationScope.end(scope, throwable); } } }