Skip to content

Commit

Permalink
Remove hibernate session spans (open-telemetry#4538)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored and RashmiRam committed May 23, 2022
1 parent 4b8f589 commit 748f5af
Show file tree
Hide file tree
Showing 36 changed files with 945 additions and 1,113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ if (findProperty("testLatestDeps") as Boolean) {
}
}
}

tasks.withType<Test>().configureEach {
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.hibernate.experimental-span-attributes=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
Expand All @@ -17,7 +18,9 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -51,33 +54,39 @@ 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(SessionMethodUtils.class);
callDepth = CallDepth.forClass(HibernateOperation.class);
if (callDepth.getAndIncrement() > 0) {
return;
}

VirtualField<Criteria, Context> virtualField =
VirtualField.find(Criteria.class, Context.class);

String entityName = null;
if (criteria instanceof CriteriaImpl) {
entityName = ((CriteriaImpl) criteria).getEntityOrClassName();
}

context =
SessionMethodUtils.startSpanFrom(virtualField, criteria, "Criteria." + name, entityName);
if (context != null) {
scope = context.makeCurrent();
VirtualField<Criteria, SessionInfo> criteriaVirtualField =
VirtualField.find(Criteria.class, SessionInfo.class);
SessionInfo sessionInfo = criteriaVirtualField.get(criteria);

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

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.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Expand All @@ -87,7 +96,7 @@ public static void endMethod(

if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable);
instrumenter().end(context, hibernateOperation, null, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateSingletons.instrumenter;
import static io.opentelemetry.javaagent.instrumentation.hibernate.OperationNameUtil.getOperationNameForQuery;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
Expand All @@ -17,7 +19,9 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -49,26 +53,35 @@ public static class QueryMethodAdvice {
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.forClass(SessionMethodUtils.class);
callDepth = CallDepth.forClass(HibernateOperation.class);
if (callDepth.getAndIncrement() > 0) {
return;
}

VirtualField<Query, Context> virtualField = VirtualField.find(Query.class, Context.class);
VirtualField<Query, SessionInfo> criteriaVirtualField =
VirtualField.find(Query.class, SessionInfo.class);
SessionInfo sessionInfo = criteriaVirtualField.get(query);

context = SessionMethodUtils.startSpanFromQuery(virtualField, query, query.getQueryString());
if (context != null) {
scope = context.makeCurrent();
Context parentContext = Java8BytecodeBridge.currentContext();
hibernateOperation =
new HibernateOperation(getOperationNameForQuery(query.getQueryString()), sessionInfo);
if (!instrumenter().shouldStart(parentContext, hibernateOperation)) {
return;
}

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.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelHibernateOperation") HibernateOperation hibernateOperation,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Expand All @@ -78,7 +91,7 @@ public static void endMethod(

if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable);
instrumenter().end(context, hibernateOperation, null, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.field.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -56,17 +54,14 @@ public static class SessionFactoryAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void openSession(@Advice.Return Object session) {

Context parentContext = Java8BytecodeBridge.currentContext();
Context context = instrumenter().start(parentContext, "Session");

if (session instanceof Session) {
VirtualField<Session, Context> virtualField =
VirtualField.find(Session.class, Context.class);
virtualField.set((Session) session, context);
VirtualField<Session, SessionInfo> virtualField =
VirtualField.find(Session.class, SessionInfo.class);
virtualField.set((Session) session, new SessionInfo());
} else if (session instanceof StatelessSession) {
VirtualField<StatelessSession, Context> virtualField =
VirtualField.find(StatelessSession.class, Context.class);
virtualField.set((StatelessSession) session, context);
VirtualField<StatelessSession, SessionInfo> virtualField =
VirtualField.find(StatelessSession.class, SessionInfo.class);
virtualField.set((StatelessSession) session, new SessionInfo());
}
}
}
Expand Down
Loading

0 comments on commit 748f5af

Please sign in to comment.