Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make hibernate-* indy compatible #11553

Merged
merged 8 commits into from
Aug 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
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 +50,12 @@ 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);
public static Object startMethod(
trask marked this conversation as resolved.
Show resolved Hide resolved
@Advice.This Criteria criteria, @Advice.Origin("#m") String name) {

CallDepth callDepth = CallDepth.forClass(HibernateOperation.class);
if (callDepth.getAndIncrement() > 0) {
return;
return callDepth;
}

String entityName = null;
Expand All @@ -73,31 +68,21 @@ public static void startMethod(
SessionInfo sessionInfo = criteriaVirtualField.get(criteria);

Context parentContext = Java8BytecodeBridge.currentContext();
hibernateOperation = new HibernateOperation("Criteria." + name, entityName, sessionInfo);
HibernateOperation hibernateOperation =
new HibernateOperation("Criteria." + name, entityName, sessionInfo);
if (!instrumenter().shouldStart(parentContext, hibernateOperation)) {
return;
return callDepth;
}

context = instrumenter().start(parentContext, hibernateOperation);
scope = context.makeCurrent();
return HibernateOperationScope.startNew(
callDepth, 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 Object enterScope) {

if (scope != null) {
scope.close();
instrumenter().end(context, hibernateOperation, null, throwable);
}
HibernateOperationScope.end(enterScope, instrumenter(), 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,13 @@
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 +50,33 @@ 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 Object startMethod(@Advice.This Query query) {

callDepth = CallDepth.forClass(HibernateOperation.class);
CallDepth callDepth = CallDepth.forClass(HibernateOperation.class);
if (callDepth.getAndIncrement() > 0) {
return;
return callDepth;
}

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;
return callDepth;
}

context = instrumenter().start(parentContext, hibernateOperation);
scope = context.makeCurrent();
return HibernateOperationScope.startNew(
callDepth, 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 Object enterScope) {

if (callDepth.decrementAndGet() > 0) {
return;
}

if (scope != null) {
scope.close();
instrumenter().end(context, hibernateOperation, null, throwable);
}
HibernateOperationScope.end(enterScope, instrumenter(), throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
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 javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -49,48 +50,33 @@ 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 Object startCommit(@Advice.This Transaction transaction) {

callDepth = CallDepth.forClass(HibernateOperation.class);
CallDepth callDepth = CallDepth.forClass(HibernateOperation.class);
if (callDepth.getAndIncrement() > 0) {
return;
return callDepth;
}

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);
HibernateOperation hibernateOperation =
new HibernateOperation("Transaction.commit", sessionInfo);
if (!instrumenter().shouldStart(parentContext, hibernateOperation)) {
return;
return callDepth;
}

context = instrumenter().start(parentContext, hibernateOperation);
scope = context.makeCurrent();
return HibernateOperationScope.startNew(
callDepth, 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) {
@Advice.Thrown Throwable throwable, @Advice.Enter @Nullable Object enterScope) {

if (callDepth.decrementAndGet() > 0) {
return;
}

if (scope != null) {
scope.close();
instrumenter().end(context, hibernateOperation, null, throwable);
}
HibernateOperationScope.end(enterScope, instrumenter(), throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
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 +50,12 @@ 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);
public static Object startMethod(
@Advice.This Criteria criteria, @Advice.Origin("#m") String name) {

CallDepth callDepth = CallDepth.forClass(HibernateOperation.class);
if (callDepth.getAndIncrement() > 0) {
return;
return callDepth;
}

String entityName = null;
Expand All @@ -73,31 +68,21 @@ public static void startMethod(
SessionInfo sessionInfo = criteriaVirtualField.get(criteria);

Context parentContext = Java8BytecodeBridge.currentContext();
hibernateOperation = new HibernateOperation("Criteria." + name, entityName, sessionInfo);
HibernateOperation hibernateOperation =
new HibernateOperation("Criteria." + name, entityName, sessionInfo);
if (!instrumenter().shouldStart(parentContext, hibernateOperation)) {
return;
return callDepth;
}

context = instrumenter().start(parentContext, hibernateOperation);
scope = context.makeCurrent();
return HibernateOperationScope.startNew(
callDepth, 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 Object enterState) {

if (scope != null) {
scope.close();
instrumenter().end(context, hibernateOperation, null, throwable);
}
HibernateOperationScope.end(enterState, instrumenter(), 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-4.0");
Expand All @@ -31,10 +33,8 @@ public ElementMatcher.Junction<ClassLoader> 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
Expand Down
Loading
Loading