Skip to content

Commit

Permalink
Hibernate: set span name only on method entry (#3603)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Jul 26, 2021
1 parent 26dc106 commit 3555c25
Show file tree
Hide file tree
Showing 18 changed files with 208 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.Criteria;
import org.hibernate.impl.CriteriaImpl;

public class CriteriaInstrumentation implements TypeInstrumentation {

Expand Down Expand Up @@ -63,7 +63,13 @@ public static void startMethod(
ContextStore<Criteria, Context> contextStore =
InstrumentationContext.get(Criteria.class, Context.class);

context = SessionMethodUtils.startSpanFrom(contextStore, criteria, "Criteria." + name, null);
String entityName = null;
if (criteria instanceof CriteriaImpl) {
entityName = ((CriteriaImpl) criteria).getEntityOrClassName();
}

context =
SessionMethodUtils.startSpanFrom(contextStore, criteria, "Criteria." + name, entityName);
if (context != null) {
scope = context.makeCurrent();
}
Expand All @@ -72,7 +78,6 @@ public static void startMethod(
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable,
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object entity,
@Advice.Origin("#m") String name,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
Expand All @@ -84,7 +89,7 @@ public static void endMethod(

if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, "Criteria." + name, entity);
SessionMethodUtils.end(context, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.hibernate.v3_3;

import java.util.function.Function;
import org.hibernate.impl.AbstractSessionImpl;

public final class EntityNameUtil {

private EntityNameUtil() {}

private static String bestGuessEntityName(Object session, Object entity) {
if (entity == null) {
return null;
}

if (session instanceof AbstractSessionImpl) {
return ((AbstractSessionImpl) session).bestGuessEntityName(entity);
}

return null;
}

public static Function<Object, String> bestGuessEntityName(Object session) {
return (entity) -> bestGuessEntityName(session, entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void endMethod(

if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, null, null);
SessionMethodUtils.end(context, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateTracer.tracer;
import static io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils.SCOPE_ONLY_METHODS;
import static io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils.getEntityName;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
Expand All @@ -26,7 +27,6 @@
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.Criteria;
import org.hibernate.Query;
Expand Down Expand Up @@ -131,7 +131,9 @@ public static class SessionMethodAdvice {
public static void startMethod(
@Advice.This Object session,
@Advice.Origin("#m") String name,
@Advice.Argument(0) Object entity,
@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("otelContext") Context spanContext,
@Advice.Local("otelScope") Scope scope) {
Expand All @@ -157,7 +159,9 @@ public static void startMethod(
}

if (!SCOPE_ONLY_METHODS.contains(name)) {
spanContext = tracer().startSpan(sessionContext, "Session." + name, entity);
String entityName =
getEntityName(descriptor, arg0, arg1, EntityNameUtil.bestGuessEntityName(session));
spanContext = tracer().startSpan(sessionContext, "Session." + name, entityName);
scope = spanContext.makeCurrent();
} else {
scope = sessionContext.makeCurrent();
Expand All @@ -167,8 +171,6 @@ public static void startMethod(
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable,
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object returned,
@Advice.Origin("#m") String name,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context spanContext,
@Advice.Local("otelScope") Scope scope) {
Expand All @@ -179,7 +181,7 @@ public static void endMethod(

if (scope != null) {
scope.close();
SessionMethodUtils.end(spanContext, throwable, "Session." + name, returned);
SessionMethodUtils.end(spanContext, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void endCommit(

if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, null, null);
SessionMethodUtils.end(context, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CriteriaTest extends AbstractHibernateTest {
}
}
span(1) {
name "Criteria.$methodName"
name "Criteria.$methodName Value"
kind INTERNAL
childOf span(0)
attributes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class SessionTest extends AbstractHibernateTest {
}
}
span(1) {
name "Session.replicate"
name "Session.replicate java.lang.Long"
kind INTERNAL
childOf span(0)
status ERROR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.Criteria;
import org.hibernate.internal.CriteriaImpl;

public class CriteriaInstrumentation implements TypeInstrumentation {

Expand Down Expand Up @@ -63,7 +63,13 @@ public static void startMethod(
ContextStore<Criteria, Context> contextStore =
InstrumentationContext.get(Criteria.class, Context.class);

context = SessionMethodUtils.startSpanFrom(contextStore, criteria, "Criteria." + name, null);
String entityName = null;
if (criteria instanceof CriteriaImpl) {
entityName = ((CriteriaImpl) criteria).getEntityOrClassName();
}

context =
SessionMethodUtils.startSpanFrom(contextStore, criteria, "Criteria." + name, entityName);
if (context != null) {
scope = context.makeCurrent();
}
Expand All @@ -72,8 +78,6 @@ public static void startMethod(
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable,
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object entity,
@Advice.Origin("#m") String name,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Expand All @@ -84,7 +88,7 @@ public static void endMethod(

if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, "Criteria." + name, entity);
SessionMethodUtils.end(context, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.hibernate.v4_0;

import java.util.function.Function;
import org.hibernate.SharedSessionContract;
import org.hibernate.internal.SessionImpl;
import org.hibernate.internal.StatelessSessionImpl;

public final class EntityNameUtil {

private EntityNameUtil() {}

private static String bestGuessEntityName(SharedSessionContract session, Object entity) {
if (entity == null) {
return null;
}

if (session instanceof SessionImpl) {
return ((SessionImpl) session).bestGuessEntityName(entity);
} else if (session instanceof StatelessSessionImpl) {
return ((StatelessSessionImpl) session).bestGuessEntityName(entity);
}

return null;
}

public static Function<Object, String> bestGuessEntityName(SharedSessionContract session) {
return (entity) -> bestGuessEntityName(session, entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void endMethod(

if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, null, null);
SessionMethodUtils.end(context, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateTracer.tracer;
import static io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils.SCOPE_ONLY_METHODS;
import static io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils.getEntityName;
import static io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils.getSessionMethodSpanName;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand All @@ -27,7 +28,6 @@
import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatcher;
import org.hibernate.Criteria;
import org.hibernate.Query;
Expand Down Expand Up @@ -125,7 +125,9 @@ public static class SessionMethodAdvice {
public static void startMethod(
@Advice.This SharedSessionContract session,
@Advice.Origin("#m") String name,
@Advice.Argument(0) Object entity,
@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("otelContext") Context spanContext,
@Advice.Local("otelScope") Scope scope) {
Expand All @@ -144,7 +146,10 @@ public static void startMethod(
}

if (!SCOPE_ONLY_METHODS.contains(name)) {
spanContext = tracer().startSpan(sessionContext, getSessionMethodSpanName(name), entity);
String entityName =
getEntityName(descriptor, arg0, arg1, EntityNameUtil.bestGuessEntityName(session));
spanContext =
tracer().startSpan(sessionContext, getSessionMethodSpanName(name), entityName);
scope = spanContext.makeCurrent();
} else {
scope = sessionContext.makeCurrent();
Expand All @@ -154,8 +159,6 @@ public static void startMethod(
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endMethod(
@Advice.Thrown Throwable throwable,
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object returned,
@Advice.Origin("#m") String name,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context spanContext,
@Advice.Local("otelScope") Scope scope) {
Expand All @@ -166,7 +169,7 @@ public static void endMethod(

if (scope != null) {
scope.close();
SessionMethodUtils.end(spanContext, throwable, getSessionMethodSpanName(name), returned);
SessionMethodUtils.end(spanContext, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void endCommit(

if (scope != null) {
scope.close();
SessionMethodUtils.end(context, throwable, null, null);
SessionMethodUtils.end(context, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class AbstractHibernateTest extends AgentInstrumentationSpecification {
Session writer = sessionFactory.openSession()
writer.beginTransaction()
prepopulated = new ArrayList<>()
for (int i = 0; i < 2; i++) {
for (int i = 0; i < 5; i++) {
prepopulated.add(new Value("Hello :) " + i))
writer.save(prepopulated.get(i))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CriteriaTest extends AbstractHibernateTest {
}
}
span(1) {
name "Criteria.$methodName"
name "Criteria.$methodName Value"
kind INTERNAL
childOf span(0)
attributes {
Expand Down
Loading

0 comments on commit 3555c25

Please sign in to comment.