Skip to content

Commit

Permalink
make hibernate-* indy compatible (#11553)
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Aug 22, 2024
1 parent d0d39b8 commit 101d88f
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -30,6 +32,11 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
"org.hibernate.transaction.JBossTransactionManagerLookup");
}

@Override
public String getModuleGroup() {
return "hibernate";
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,49 +49,28 @@ 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<Query, SessionInfo> queryVirtualField =
VirtualField.find(Query.class, SessionInfo.class);
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,48 +48,28 @@ 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<Transaction, SessionInfo> transactionVirtualField =
VirtualField.find(Transaction.class, SessionInfo.class);
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Loading

0 comments on commit 101d88f

Please sign in to comment.