Convert HibernateTracer to Instrumenter
MODIFY
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionFactoryInstrumentation.java
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionFactoryInstrumentation.java
@@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.hibernate.v3_3;
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.HibernateTracer.tracer;
+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;
@@ -57,7 +57,7 @@ public class SessionFactoryInstrumentation implements TypeInstrumentation {
public static void openSession(@Advice.Return Object session) {
Context parentContext = Java8BytecodeBridge.currentContext();
- Context context = tracer().startSpan(parentContext, "Session");
+ Context context = instrumenter().start(parentContext, "Session");
if (session instanceof Session) {
VirtualField<Session, Context> virtualField =

MODIFY
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java
instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java
@@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.hibernate.v3_3;
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.HibernateTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateSingletons.instrumenter;
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;
@@ -115,11 +115,7 @@ public class SessionInstrumentation implements TypeInstrumentation {
if (sessionContext == null) {
return;
}
- if (throwable != null) {
- tracer().endExceptionally(sessionContext, throwable);
- } else {
- tracer().end(sessionContext);
- }
+ instrumenter().end(sessionContext, null, null, throwable);
}
}
@@ -160,7 +156,8 @@ public class SessionInstrumentation implements TypeInstrumentation {
if (!SCOPE_ONLY_METHODS.contains(name)) {
String entityName =
getEntityName(descriptor, arg0, arg1, EntityNameUtil.bestGuessEntityName(session));
- spanContext = tracer().startSpan(sessionContext, "Session." + name, entityName);
+ spanContext =
+ SessionMethodUtils.startSpanFrom(sessionContext, "Session." + name, entityName);
scope = spanContext.makeCurrent();
} else {
scope = sessionContext.makeCurrent();

MODIFY
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionFactoryInstrumentation.java
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionFactoryInstrumentation.java
@@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.hibernate.v4_0;
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.HibernateTracer.tracer;
+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;
@@ -54,7 +54,7 @@ public class SessionFactoryInstrumentation implements TypeInstrumentation {
public static void openSession(@Advice.Return SharedSessionContract session) {
Context parentContext = Java8BytecodeBridge.currentContext();
- Context context = tracer().startSpan(parentContext, "Session");
+ Context context = instrumenter().start(parentContext, "Session");
VirtualField<SharedSessionContract, Context> virtualField =
VirtualField.find(SharedSessionContract.class, Context.class);

MODIFY
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java
instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java
@@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.hibernate.v4_0;
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.HibernateTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateSingletons.instrumenter;
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;
@@ -109,11 +109,7 @@ public class SessionInstrumentation implements TypeInstrumentation {
if (sessionContext == null) {
return;
}
- if (throwable != null) {
- tracer().endExceptionally(sessionContext, throwable);
- } else {
- tracer().end(sessionContext);
- }
+ instrumenter().end(sessionContext, null, null, throwable);
}
}
@@ -148,7 +144,8 @@ public class SessionInstrumentation implements TypeInstrumentation {
String entityName =
getEntityName(descriptor, arg0, arg1, EntityNameUtil.bestGuessEntityName(session));
spanContext =
- tracer().startSpan(sessionContext, getSessionMethodSpanName(name), entityName);
+ SessionMethodUtils.startSpanFrom(
+ sessionContext, getSessionMethodSpanName(name), entityName);
scope = spanContext.makeCurrent();
} else {
scope = sessionContext.makeCurrent();

ADD
instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/HibernateSingletons.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.hibernate;
+
+import io.opentelemetry.api.GlobalOpenTelemetry;
+import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
+
+public class HibernateSingletons {
+
+ private static final Instrumenter<String, Void> INSTANCE;
+
+ static {
+ INSTANCE =
+ Instrumenter.<String, Void>newBuilder(
+ GlobalOpenTelemetry.get(), "io.opentelemetry.hibernate-common", s -> s)
+ .newInstrumenter();
+ }
+
+ public static Instrumenter<String, Void> instrumenter() {
+ return INSTANCE;
+ }
+}

DELETE
instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/HibernateTracer.java
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.javaagent.instrumentation.hibernate;
-
-import io.opentelemetry.api.trace.SpanKind;
-import io.opentelemetry.context.Context;
-import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
-
-public class HibernateTracer extends BaseTracer {
- private static final HibernateTracer TRACER = new HibernateTracer();
-
- public static HibernateTracer tracer() {
- return TRACER;
- }
-
- public Context startSpan(Context parentContext, String operationName, String entityName) {
- return startSpan(parentContext, spanNameForOperation(operationName, entityName));
- }
-
- public Context startSpan(Context parentContext, String spanName) {
- return startSpan(parentContext, spanName, SpanKind.INTERNAL);
- }
-
- private static String spanNameForOperation(String operationName, String entityName) {
- if (entityName != null) {
- return operationName + " " + entityName;
- }
- return operationName;
- }
-
- @Override
- protected String getInstrumentationName() {
- return "io.opentelemetry.hibernate-common";
- }
-}

MODIFY
instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/SessionMethodUtils.java
instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/SessionMethodUtils.java
package io.opentelemetry.javaagent.instrumentation.hibernate;
-import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.hibernate.HibernateSingletons.instrumenter;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.db.SqlStatementInfo;
@@ -23,7 +23,7 @@ public final class SessionMethodUtils {
public static final Set<String> SCOPE_ONLY_METHODS =
new HashSet<>(Arrays.asList("immediateLoad", "internalLoad"));
- public static <TARGET, ENTITY> Context startSpanFrom(
+ public static <TARGET> Context startSpanFrom(
VirtualField<TARGET, Context> virtualField,
TARGET spanKey,
String operationName,
@@ -31,7 +31,7 @@ public final class SessionMethodUtils {
return startSpanFrom(virtualField, spanKey, () -> operationName, entityName);
}
- private static <TARGET, ENTITY> Context startSpanFrom(
+ private static <TARGET> Context startSpanFrom(
VirtualField<TARGET, Context> virtualField,
TARGET spanKey,
Supplier<String> operationNameSupplier,
@@ -42,7 +42,19 @@ public final class SessionMethodUtils {
return null; // No state found. We aren't in a Session.
}
- return tracer().startSpan(sessionContext, operationNameSupplier.get(), entityName);
+ return startSpanFrom(sessionContext, operationNameSupplier.get(), entityName);
+ }
+
+ public static Context startSpanFrom(
+ Context sessionContext, String operationName, String entityName) {
+ return instrumenter().start(sessionContext, spanNameForOperation(operationName, entityName));
+ }
+
+ private static String spanNameForOperation(String operationName, String entityName) {
+ if (entityName != null) {
+ return operationName + " " + entityName;
+ }
+ return operationName;
}
public static <TARGET> Context startSpanFromQuery(
@@ -65,16 +77,11 @@ public final class SessionMethodUtils {
}
public static void end(@Nullable Context context, Throwable throwable) {
-
if (context == null) {
return;
}
- if (throwable != null) {
- tracer().endExceptionally(context, throwable);
- } else {
- tracer().end(context);
- }
+ instrumenter().end(context, null, null, throwable);
}
// Copies a span from the given Session VirtualField into the targetVirtualField. Used to

Convert HibernateTracer to Instrumenter (#4369)