Rename TracerBridge and Provider
This commit is contained in:
parent
9f1d22ea6b
commit
7154b54e10
|
@ -1,19 +1,17 @@
|
|||
package datadog.trace.agent.tooling;
|
||||
|
||||
import datadog.opentracing.DDTracer;
|
||||
import datadog.trace.context.TracerBridge;
|
||||
import io.opentracing.util.GlobalTracer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class TracerInstaller {
|
||||
/** Register a global tracer if no global tracer is already registered. */
|
||||
public static synchronized void installGlobalTracer() {
|
||||
if (!GlobalTracer.isRegistered()) {
|
||||
if (!io.opentracing.util.GlobalTracer.isRegistered()) {
|
||||
final DDTracer tracer = new DDTracer();
|
||||
try {
|
||||
GlobalTracer.register(tracer);
|
||||
TracerBridge.registerIfAbsent(tracer);
|
||||
io.opentracing.util.GlobalTracer.register(tracer);
|
||||
datadog.trace.api.GlobalTracer.registerIfAbsent(tracer);
|
||||
} catch (final RuntimeException re) {
|
||||
log.warn("Failed to register tracer '" + tracer + "'", re);
|
||||
}
|
||||
|
@ -24,7 +22,10 @@ public class TracerInstaller {
|
|||
|
||||
public static void logVersionInfo() {
|
||||
VersionLogger.logAllVersions();
|
||||
log.debug(GlobalTracer.class.getName() + " loaded on " + GlobalTracer.class.getClassLoader());
|
||||
log.debug(
|
||||
io.opentracing.util.GlobalTracer.class.getName()
|
||||
+ " loaded on "
|
||||
+ io.opentracing.util.GlobalTracer.class.getClassLoader());
|
||||
log.debug(
|
||||
AgentInstaller.class.getName() + " loaded on " + AgentInstaller.class.getClassLoader());
|
||||
}
|
||||
|
|
|
@ -7,9 +7,9 @@ import datadog.opentracing.DDSpan;
|
|||
import datadog.opentracing.DDTracer;
|
||||
import datadog.trace.agent.tooling.AgentInstaller;
|
||||
import datadog.trace.agent.tooling.Instrumenter;
|
||||
import datadog.trace.api.GlobalTracer;
|
||||
import datadog.trace.common.writer.ListWriter;
|
||||
import datadog.trace.common.writer.Writer;
|
||||
import datadog.trace.context.TracerBridge;
|
||||
import io.opentracing.Tracer;
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
@ -85,7 +85,7 @@ public abstract class AgentTestRunner extends Specification {
|
|||
};
|
||||
TEST_TRACER = new DDTracer(TEST_WRITER);
|
||||
TestUtils.registerOrReplaceGlobalTracer((Tracer) TEST_TRACER);
|
||||
TracerBridge.registerIfAbsent((DDTracer) TEST_TRACER);
|
||||
GlobalTracer.registerIfAbsent((DDTracer) TEST_TRACER);
|
||||
}
|
||||
|
||||
protected static Tracer getTestTracer() {
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package datadog.trace.api;
|
||||
|
||||
import datadog.trace.context.TracerBridge;
|
||||
|
||||
/**
|
||||
* Utility class to access the active trace and span ids.
|
||||
*
|
||||
|
@ -9,29 +7,10 @@ import datadog.trace.context.TracerBridge;
|
|||
*/
|
||||
public class CorrelationIdentifier {
|
||||
public static String getTraceId() {
|
||||
return TracerBridge.get().getTraceId();
|
||||
return GlobalTracer.get().getTraceId();
|
||||
}
|
||||
|
||||
public static String getSpanId() {
|
||||
return TracerBridge.get().getSpanId();
|
||||
}
|
||||
|
||||
public interface Provider {
|
||||
String getTraceId();
|
||||
|
||||
String getSpanId();
|
||||
|
||||
Provider NO_OP =
|
||||
new Provider() {
|
||||
@Override
|
||||
public String getTraceId() {
|
||||
return "0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSpanId() {
|
||||
return "0";
|
||||
}
|
||||
};
|
||||
return GlobalTracer.get().getSpanId();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package datadog.trace.api;
|
||||
|
||||
import datadog.trace.api.interceptor.TraceInterceptor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* A global reference to the registered Datadog tracer.
|
||||
*
|
||||
* <p>OpenTracing's GlobalTracer cannot be cast to its DDTracer implementation, so this class exists
|
||||
* to provide a global window to datadog-specific features.
|
||||
*/
|
||||
public class GlobalTracer {
|
||||
private static final Tracer NO_OP =
|
||||
new Tracer() {
|
||||
@Override
|
||||
public String getTraceId() {
|
||||
return "0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSpanId() {
|
||||
return "0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addTraceInterceptor(TraceInterceptor traceInterceptor) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
private static final AtomicReference<Tracer> provider = new AtomicReference<>(NO_OP);
|
||||
|
||||
public static void registerIfAbsent(Tracer p) {
|
||||
if (p != null && p != NO_OP) {
|
||||
provider.compareAndSet(NO_OP, p);
|
||||
}
|
||||
}
|
||||
|
||||
public static Tracer get() {
|
||||
return provider.get();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package datadog.trace.api;
|
||||
|
||||
import datadog.trace.api.interceptor.TraceInterceptor;
|
||||
|
||||
/** A class with Datadog tracer features. */
|
||||
public interface Tracer {
|
||||
|
||||
/** Get the trace id of the active trace. Returns 0 if there is no active trace. */
|
||||
String getTraceId();
|
||||
|
||||
/**
|
||||
* Get the span id of the active span of the active trace. Returns 0 if there is no active trace.
|
||||
*/
|
||||
String getSpanId();
|
||||
|
||||
/**
|
||||
* Add a new interceptor to the tracer. Interceptors with duplicate priority to existing ones are
|
||||
* ignored.
|
||||
*
|
||||
* @param traceInterceptor
|
||||
* @return false if an interceptor with same priority exists.
|
||||
*/
|
||||
boolean addTraceInterceptor(TraceInterceptor traceInterceptor);
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
package datadog.trace.context;
|
||||
|
||||
import datadog.trace.api.interceptor.TraceInterceptor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/** A global reference to the registered Datadog tracer. */
|
||||
public class TracerBridge {
|
||||
private static final AtomicReference<Provider> provider = new AtomicReference<>(Provider.NO_OP);
|
||||
|
||||
public static void registerIfAbsent(Provider p) {
|
||||
if (p != null && p != Provider.NO_OP) {
|
||||
provider.compareAndSet(Provider.NO_OP, p);
|
||||
}
|
||||
}
|
||||
|
||||
public static Provider get() {
|
||||
return provider.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new interceptor to the tracer. Interceptors with duplicate priority to existing ones are
|
||||
* ignored.
|
||||
*
|
||||
* @param traceInterceptor
|
||||
* @return false if an interceptor with same priority exists.
|
||||
*/
|
||||
public static boolean addTraceInterceptor(TraceInterceptor traceInterceptor) {
|
||||
return get().addTraceInterceptor(traceInterceptor);
|
||||
}
|
||||
|
||||
public interface Provider {
|
||||
String getTraceId();
|
||||
|
||||
String getSpanId();
|
||||
|
||||
/**
|
||||
* Add a new interceptor to the tracer. Interceptors with duplicate priority to existing ones
|
||||
* are ignored.
|
||||
*
|
||||
* @param traceInterceptor
|
||||
* @return false if an interceptor with same priority exists.
|
||||
*/
|
||||
boolean addTraceInterceptor(TraceInterceptor traceInterceptor);
|
||||
|
||||
Provider NO_OP =
|
||||
new Provider() {
|
||||
@Override
|
||||
public String getTraceId() {
|
||||
return "0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSpanId() {
|
||||
return "0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addTraceInterceptor(TraceInterceptor traceInterceptor) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -23,7 +23,6 @@ import datadog.trace.common.sampling.Sampler;
|
|||
import datadog.trace.common.writer.DDAgentWriter;
|
||||
import datadog.trace.common.writer.DDApi;
|
||||
import datadog.trace.common.writer.Writer;
|
||||
import datadog.trace.context.TracerBridge;
|
||||
import io.opentracing.Scope;
|
||||
import io.opentracing.ScopeManager;
|
||||
import io.opentracing.Span;
|
||||
|
@ -49,7 +48,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||
|
||||
/** DDTracer makes it easy to send traces and span to DD using the OpenTracing API. */
|
||||
@Slf4j
|
||||
public class DDTracer implements io.opentracing.Tracer, Closeable, TracerBridge.Provider {
|
||||
public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace.api.Tracer {
|
||||
|
||||
public static final String UNASSIGNED_DEFAULT_SERVICE_NAME = "unnamed-java-app";
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ package datadog.opentracing
|
|||
import datadog.trace.api.interceptor.MutableSpan
|
||||
import datadog.trace.api.interceptor.TraceInterceptor
|
||||
import datadog.trace.common.writer.ListWriter
|
||||
import datadog.trace.context.TracerBridge
|
||||
import datadog.trace.api.GlobalTracer
|
||||
import spock.lang.Specification
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
@ -147,7 +147,7 @@ class TraceInterceptorTest extends Specification {
|
|||
|
||||
def "register interceptor through bridge" () {
|
||||
setup:
|
||||
TracerBridge.registerIfAbsent(tracer)
|
||||
GlobalTracer.registerIfAbsent(tracer)
|
||||
def interceptor = new TraceInterceptor() {
|
||||
@Override
|
||||
Collection<? extends MutableSpan> onTraceComplete(Collection<? extends MutableSpan> trace) {
|
||||
|
@ -161,7 +161,7 @@ class TraceInterceptorTest extends Specification {
|
|||
}
|
||||
|
||||
expect:
|
||||
TracerBridge.addTraceInterceptor(interceptor)
|
||||
GlobalTracer.get().addTraceInterceptor(interceptor)
|
||||
tracer.interceptors.contains(interceptor)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue