Skip to content

Commit

Permalink
move hibernate to open-telemetry#11553
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Jun 11, 2024
1 parent ac0e649 commit f48e203
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 211 deletions.
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,12 +50,17 @@ public void transform(TypeTransformer transformer) {
public static class CriteriaMethodAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static Object startMethod(
@Advice.This Criteria criteria, @Advice.Origin("#m") String name) {

CallDepth callDepth = CallDepth.forClass(HibernateOperation.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 callDepth;
return;
}

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

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

return HibernateOperationScope.startNew(
callDepth, hibernateOperation, parentContext, instrumenter());
context = instrumenter().start(parentContext, hibernateOperation);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable, @Advice.Enter Object enterScope) {
@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;
}

HibernateOperationScope.end(enterScope, instrumenter(), throwable);
if (scope != null) {
scope.close();
instrumenter().end(context, hibernateOperation, null, throwable);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
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
implements ExperimentalInstrumentationModule {
public class HibernateInstrumentationModule extends InstrumentationModule {

public HibernateInstrumentationModule() {
super("hibernate", "hibernate-3.3");
Expand All @@ -32,11 +30,6 @@ 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,33 +50,49 @@ public void transform(TypeTransformer transformer) {
public static class QueryMethodAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static Object startMethod(@Advice.This Query query) {
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) {

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

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

return HibernateOperationScope.startNew(
callDepth, hibernateOperation, parentContext, instrumenter());
context = instrumenter().start(parentContext, hibernateOperation);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable, @Advice.Enter Object enterScope) {
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

HibernateOperationScope.end(enterScope, instrumenter(), throwable);
if (callDepth.decrementAndGet() > 0) {
return;
}

if (scope != null) {
scope.close();
instrumenter().end(context, hibernateOperation, null, throwable);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
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 @@ -50,33 +49,48 @@ public void transform(TypeTransformer transformer) {
public static class TransactionCommitAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static Object startCommit(@Advice.This Transaction transaction) {
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) {

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

VirtualField<Transaction, SessionInfo> transactionVirtualField =
VirtualField.find(Transaction.class, SessionInfo.class);
SessionInfo sessionInfo = transactionVirtualField.get(transaction);

Context parentContext = Java8BytecodeBridge.currentContext();
HibernateOperation hibernateOperation =
new HibernateOperation("Transaction.commit", sessionInfo);
hibernateOperation = new HibernateOperation("Transaction.commit", sessionInfo);
if (!instrumenter().shouldStart(parentContext, hibernateOperation)) {
return callDepth;
return;
}

return HibernateOperationScope.startNew(
callDepth, hibernateOperation, parentContext, instrumenter());
context = instrumenter().start(parentContext, hibernateOperation);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endCommit(
@Advice.Thrown Throwable throwable, @Advice.Enter @Nullable Object enterScope) {
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

HibernateOperationScope.end(enterScope, instrumenter(), throwable);
if (callDepth.decrementAndGet() > 0) {
return;
}

if (scope != null) {
scope.close();
instrumenter().end(context, hibernateOperation, null, 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,12 +50,17 @@ public void transform(TypeTransformer transformer) {
public static class CriteriaMethodAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static Object startMethod(
@Advice.This Criteria criteria, @Advice.Origin("#m") String name) {

CallDepth callDepth = CallDepth.forClass(HibernateOperation.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 callDepth;
return;
}

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

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

return HibernateOperationScope.startNew(
callDepth, hibernateOperation, parentContext, instrumenter());
context = instrumenter().start(parentContext, hibernateOperation);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable, @Advice.Enter Object enterState) {
@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;
}

HibernateOperationScope.end(enterState, instrumenter(), throwable);
if (scope != null) {
scope.close();
instrumenter().end(context, hibernateOperation, null, throwable);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
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
implements ExperimentalInstrumentationModule {
public class HibernateInstrumentationModule extends InstrumentationModule {

public HibernateInstrumentationModule() {
super("hibernate", "hibernate-4.0");
Expand All @@ -33,8 +31,10 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
}

@Override
public String getModuleGroup() {
return "hibernate";
public boolean isIndyModule() {
// shares classes with hibernate-procedure-call-4.3, these classes should be in the same class
// loader
return false;
}

@Override
Expand Down
Loading

0 comments on commit f48e203

Please sign in to comment.