Neaten decorator pattern a bit
This commit is contained in:
parent
079fcd6a56
commit
044ff75e07
|
@ -6,7 +6,7 @@ import java.util.List;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
|
||||||
public class HibernateDecorator extends OrmClientDecorator {
|
public class HibernateDecorator extends OrmClientDecorator {
|
||||||
public static final HibernateDecorator INSTANCE = new HibernateDecorator();
|
public static final HibernateDecorator DECORATOR = new HibernateDecorator();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String service() {
|
protected String service() {
|
||||||
|
@ -44,7 +44,7 @@ public class HibernateDecorator extends OrmClientDecorator {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <ENTITY> String entityName(final ENTITY entity) {
|
public String entityName(final Object entity) {
|
||||||
String name = null;
|
String name = null;
|
||||||
if (entity instanceof String) {
|
if (entity instanceof String) {
|
||||||
// We were given an entity name, not the entity itself.
|
// We were given an entity name, not the entity itself.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package datadog.trace.instrumentation.hibernate;
|
package datadog.trace.instrumentation.hibernate;
|
||||||
|
|
||||||
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
|
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.isInterface;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
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.
|
// Note: We don't know what the entity is until the method is returning.
|
||||||
final SessionState state =
|
final SessionState state =
|
||||||
SessionMethodUtils.startScopeFrom(contextStore, query, "hibernate.query." + name, null);
|
SessionMethodUtils.startScopeFrom(contextStore, query, "hibernate.query." + name, null);
|
||||||
HibernateDecorator.INSTANCE.onStatement(
|
DECORATOR.onStatement(state.getMethodScope().span(), query.getQueryString());
|
||||||
state.getMethodScope().span(), query.getQueryString());
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package datadog.trace.instrumentation.hibernate;
|
package datadog.trace.instrumentation.hibernate;
|
||||||
|
|
||||||
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
|
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.isInterface;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
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) {
|
public static void openSession(@Advice.Return final SharedSessionContract session) {
|
||||||
|
|
||||||
final Span span = GlobalTracer.get().buildSpan("hibernate.session").start();
|
final Span span = GlobalTracer.get().buildSpan("hibernate.session").start();
|
||||||
HibernateDecorator.INSTANCE.afterStart(span);
|
DECORATOR.afterStart(span);
|
||||||
HibernateDecorator.INSTANCE.onSession(span, session);
|
DECORATOR.onSession(span, session);
|
||||||
|
|
||||||
final ContextStore<SharedSessionContract, SessionState> contextStore =
|
final ContextStore<SharedSessionContract, SessionState> contextStore =
|
||||||
InstrumentationContext.get(SharedSessionContract.class, SessionState.class);
|
InstrumentationContext.get(SharedSessionContract.class, SessionState.class);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package datadog.trace.instrumentation.hibernate;
|
package datadog.trace.instrumentation.hibernate;
|
||||||
|
|
||||||
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
|
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.isInterface;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
import static net.bytebuddy.matcher.ElementMatchers.named;
|
||||||
|
@ -136,8 +137,8 @@ public class SessionInstrumentation extends Instrumenter.Default {
|
||||||
}
|
}
|
||||||
|
|
||||||
final Span span = state.getSessionSpan();
|
final Span span = state.getSessionSpan();
|
||||||
HibernateDecorator.INSTANCE.onError(span, throwable);
|
DECORATOR.onError(span, throwable);
|
||||||
HibernateDecorator.INSTANCE.beforeFinish(span);
|
DECORATOR.beforeFinish(span);
|
||||||
span.finish();
|
span.finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
package datadog.trace.instrumentation.hibernate;
|
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 datadog.trace.bootstrap.ContextStore;
|
||||||
import io.opentracing.Scope;
|
import io.opentracing.Scope;
|
||||||
import io.opentracing.Span;
|
import io.opentracing.Span;
|
||||||
import io.opentracing.tag.Tags;
|
|
||||||
import io.opentracing.util.GlobalTracer;
|
import io.opentracing.util.GlobalTracer;
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class SessionMethodUtils {
|
public class SessionMethodUtils {
|
||||||
|
|
||||||
|
@ -37,8 +35,8 @@ public class SessionMethodUtils {
|
||||||
.buildSpan(operationName)
|
.buildSpan(operationName)
|
||||||
.asChildOf(sessionState.getSessionSpan())
|
.asChildOf(sessionState.getSessionSpan())
|
||||||
.startActive(true);
|
.startActive(true);
|
||||||
HibernateDecorator.INSTANCE.afterStart(scope.span());
|
DECORATOR.afterStart(scope.span());
|
||||||
HibernateDecorator.INSTANCE.onOperation(scope.span(), entity);
|
DECORATOR.onOperation(scope.span(), entity);
|
||||||
|
|
||||||
sessionState.setMethodScope(scope);
|
sessionState.setMethodScope(scope);
|
||||||
return sessionState;
|
return sessionState;
|
||||||
|
@ -57,14 +55,11 @@ public class SessionMethodUtils {
|
||||||
final Scope scope = sessionState.getMethodScope();
|
final Scope scope = sessionState.getMethodScope();
|
||||||
final Span span = scope.span();
|
final Span span = scope.span();
|
||||||
if (span != null) {
|
if (span != null) {
|
||||||
if (throwable != null) {
|
DECORATOR.onError(span, throwable);
|
||||||
Tags.ERROR.set(span, true);
|
|
||||||
span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
|
|
||||||
}
|
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
HibernateDecorator.INSTANCE.onOperation(span, entity);
|
DECORATOR.onOperation(span, entity);
|
||||||
}
|
}
|
||||||
HibernateDecorator.INSTANCE.beforeFinish(span);
|
DECORATOR.beforeFinish(span);
|
||||||
span.finish();
|
span.finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue