Neaten decorator pattern a bit
MODIFY
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/HibernateDecorator.java
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/HibernateDecorator.java
@@ -6,7 +6,7 @@ import java.util.List;
import javax.persistence.Entity;
public class HibernateDecorator extends OrmClientDecorator {
- public static final HibernateDecorator INSTANCE = new HibernateDecorator();
+ public static final HibernateDecorator DECORATOR = new HibernateDecorator();
@Override
protected String service() {
@@ -44,7 +44,7 @@ public class HibernateDecorator extends OrmClientDecorator {
}
@Override
- public <ENTITY> String entityName(final ENTITY entity) {
+ public String entityName(final Object entity) {
String name = null;
if (entity instanceof String) {
// We were given an entity name, not the entity itself.

MODIFY
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/QueryInstrumentation.java
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/QueryInstrumentation.java
package datadog.trace.instrumentation.hibernate;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.hibernate.HibernateDecorator.DECORATOR;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -75,8 +76,7 @@ public class QueryInstrumentation extends Instrumenter.Default {
// Note: We don't know what the entity is until the method is returning.
final SessionState state =
SessionMethodUtils.startScopeFrom(contextStore, query, "hibernate.query." + name, null);
- HibernateDecorator.INSTANCE.onStatement(
- state.getMethodScope().span(), query.getQueryString());
+ DECORATOR.onStatement(state.getMethodScope().span(), query.getQueryString());
return state;
}

MODIFY
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/SessionFactoryInstrumentation.java
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/SessionFactoryInstrumentation.java
package datadog.trace.instrumentation.hibernate;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.hibernate.HibernateDecorator.DECORATOR;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -77,8 +78,8 @@ public class SessionFactoryInstrumentation extends Instrumenter.Default {
public static void openSession(@Advice.Return final SharedSessionContract session) {
final Span span = GlobalTracer.get().buildSpan("hibernate.session").start();
- HibernateDecorator.INSTANCE.afterStart(span);
- HibernateDecorator.INSTANCE.onSession(span, session);
+ DECORATOR.afterStart(span);
+ DECORATOR.onSession(span, session);
final ContextStore<SharedSessionContract, SessionState> contextStore =
InstrumentationContext.get(SharedSessionContract.class, SessionState.class);

MODIFY
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/SessionInstrumentation.java
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/SessionInstrumentation.java
package datadog.trace.instrumentation.hibernate;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.hibernate.HibernateDecorator.DECORATOR;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -136,8 +137,8 @@ public class SessionInstrumentation extends Instrumenter.Default {
}
final Span span = state.getSessionSpan();
- HibernateDecorator.INSTANCE.onError(span, throwable);
- HibernateDecorator.INSTANCE.beforeFinish(span);
+ DECORATOR.onError(span, throwable);
+ DECORATOR.beforeFinish(span);
span.finish();
}
}

MODIFY
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/SessionMethodUtils.java
dd-java-agent/instrumentation/hibernate/src/main/java/datadog/trace/instrumentation/hibernate/SessionMethodUtils.java
package datadog.trace.instrumentation.hibernate;
-import static io.opentracing.log.Fields.ERROR_OBJECT;
+import static datadog.trace.instrumentation.hibernate.HibernateDecorator.DECORATOR;
import datadog.trace.bootstrap.ContextStore;
import io.opentracing.Scope;
import io.opentracing.Span;
-import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
-import java.util.Collections;
public class SessionMethodUtils {
@@ -37,8 +35,8 @@ public class SessionMethodUtils {
.buildSpan(operationName)
.asChildOf(sessionState.getSessionSpan())
.startActive(true);
- HibernateDecorator.INSTANCE.afterStart(scope.span());
- HibernateDecorator.INSTANCE.onOperation(scope.span(), entity);
+ DECORATOR.afterStart(scope.span());
+ DECORATOR.onOperation(scope.span(), entity);
sessionState.setMethodScope(scope);
return sessionState;
@@ -57,14 +55,11 @@ public class SessionMethodUtils {
final Scope scope = sessionState.getMethodScope();
final Span span = scope.span();
if (span != null) {
- if (throwable != null) {
- Tags.ERROR.set(span, true);
- span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
- }
+ DECORATOR.onError(span, throwable);
if (entity != null) {
- HibernateDecorator.INSTANCE.onOperation(span, entity);
+ DECORATOR.onOperation(span, entity);
}
- HibernateDecorator.INSTANCE.beforeFinish(span);
+ DECORATOR.beforeFinish(span);
span.finish();
}

Neaten decorator pattern a bit