Neaten decorator pattern a bit

This commit is contained in:
Will Gittoes 2019-02-27 09:35:10 +11:00
parent 079fcd6a56
commit 044ff75e07
No known key found for this signature in database
GPG Key ID: 521026A02DB0BB42
5 changed files with 16 additions and 19 deletions

View File

@ -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.

View File

@ -1,6 +1,7 @@
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;
}

View File

@ -1,6 +1,7 @@
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);

View File

@ -1,6 +1,7 @@
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();
}
}

View File

@ -1,13 +1,11 @@
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();
}