Use lombok builder for Tracer construction.
This will work better for long-term support without breaking compatibility for adding new fields.
This commit is contained in:
parent
bd6f732400
commit
b7ec90a846
|
@ -12,7 +12,7 @@ class Examples {
|
||||||
public static void test() {
|
public static void test() {
|
||||||
final Throwable someThrowable = null;
|
final Throwable someThrowable = null;
|
||||||
// registration
|
// registration
|
||||||
TracerContext.registerGlobalContext(new TracerContext(new Tracer()), false);
|
TracerContext.registerGlobalContext(new TracerContext(Tracer.builder().build()), false);
|
||||||
|
|
||||||
// scope
|
// scope
|
||||||
final TracerContext ctx = TracerContext.getGlobalContext();
|
final TracerContext ctx = TracerContext.getGlobalContext();
|
||||||
|
|
|
@ -4,56 +4,52 @@ import datadog.trace.api.Config;
|
||||||
import datadog.trace.tracer.sampling.AllSampler;
|
import datadog.trace.tracer.sampling.AllSampler;
|
||||||
import datadog.trace.tracer.sampling.Sampler;
|
import datadog.trace.tracer.sampling.Sampler;
|
||||||
import datadog.trace.tracer.writer.Writer;
|
import datadog.trace.tracer.writer.Writer;
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
/** A Tracer creates {@link Trace}s and holds common settings across traces. */
|
/** A Tracer creates {@link Trace}s and holds common settings across traces. */
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Tracer {
|
public class Tracer implements Closeable {
|
||||||
|
|
||||||
/** Writer is an charge of reporting traces and spans to the desired endpoint */
|
/** Writer is an charge of reporting traces and spans to the desired endpoint */
|
||||||
private final Writer writer;
|
private final Writer writer;
|
||||||
|
|
||||||
/** Sampler defines the sampling policy in order to reduce the number of traces for instance */
|
/** Sampler defines the sampling policy in order to reduce the number of traces for instance */
|
||||||
private final Sampler sampler;
|
private final Sampler sampler;
|
||||||
|
|
||||||
/** Settings for this tracer. */
|
/** Settings for this tracer. */
|
||||||
private final Config config;
|
private final Config config;
|
||||||
|
|
||||||
/** Interceptors to be called on certain trace and span events */
|
/** Interceptors to be called on certain trace and span events */
|
||||||
private final List<Interceptor> interceptors;
|
private final List<Interceptor> interceptors;
|
||||||
|
|
||||||
public Tracer() {
|
@Builder
|
||||||
this(Config.get());
|
private Tracer(
|
||||||
}
|
final String defaultServiceName,
|
||||||
|
|
||||||
public Tracer(final List<Interceptor> interceptors) {
|
|
||||||
this(Config.get(), interceptors);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Tracer(final Config config) {
|
|
||||||
this(config, Collections.<Interceptor>emptyList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Tracer(final Config config, final List<Interceptor> interceptors) {
|
|
||||||
// TODO: implement and include "standard" interceptors
|
|
||||||
this(
|
|
||||||
config,
|
|
||||||
Writer.Builder.forConfig(config),
|
|
||||||
new AllSampler(),
|
|
||||||
Collections.unmodifiableList(new ArrayList<>(interceptors)));
|
|
||||||
}
|
|
||||||
|
|
||||||
Tracer(
|
|
||||||
final Config config,
|
final Config config,
|
||||||
final Writer writer,
|
final Writer writer,
|
||||||
final Sampler sampler,
|
final Sampler sampler,
|
||||||
final List<Interceptor> interceptors) {
|
final List<Interceptor> interceptors) {
|
||||||
this.config = config;
|
// Apply defaults:
|
||||||
// TODO: we probably need to implement some sort of 'close' method for Tracer
|
final Properties serviceNameConfig = new Properties();
|
||||||
this.writer = writer;
|
if (defaultServiceName != null && !defaultServiceName.trim().isEmpty()) {
|
||||||
this.sampler = sampler;
|
serviceNameConfig.setProperty(Config.SERVICE_NAME, defaultServiceName);
|
||||||
this.interceptors = Collections.unmodifiableList(new ArrayList<>(interceptors));
|
}
|
||||||
|
this.config = config != null ? config : Config.get(serviceNameConfig);
|
||||||
|
this.writer = writer != null ? writer : Writer.Builder.forConfig(this.config);
|
||||||
|
this.sampler = sampler != null ? sampler : new AllSampler();
|
||||||
|
|
||||||
|
// TODO: implement and include "standard" interceptors
|
||||||
|
this.interceptors =
|
||||||
|
interceptors != null
|
||||||
|
? Collections.unmodifiableList(new ArrayList<>(interceptors))
|
||||||
|
: Collections.<Interceptor>emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return {@link Writer} used by this tracer */
|
/** @return {@link Writer} used by this tracer */
|
||||||
|
@ -123,4 +119,9 @@ public class Tracer {
|
||||||
log.debug(completeMessage);
|
log.debug(completeMessage);
|
||||||
throw new TraceException(completeMessage);
|
throw new TraceException(completeMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ class TracerTest extends Specification {
|
||||||
// TODO: add more tests for different config options and interceptors
|
// TODO: add more tests for different config options and interceptors
|
||||||
def "test getters"() {
|
def "test getters"() {
|
||||||
when:
|
when:
|
||||||
def tracer = new Tracer(config)
|
def tracer = Tracer.builder().config(config).build()
|
||||||
|
|
||||||
then:
|
then:
|
||||||
tracer.getWriter() instanceof AgentWriter
|
tracer.getWriter() instanceof AgentWriter
|
||||||
|
@ -32,9 +32,23 @@ class TracerTest extends Specification {
|
||||||
tracer.getDefaultServiceName() == config.getServiceName()
|
tracer.getDefaultServiceName() == config.getServiceName()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def "test defaultServiceName=#serviceName"() {
|
||||||
|
when:
|
||||||
|
def tracer = Tracer.builder().defaultServiceName(serviceName).build()
|
||||||
|
|
||||||
|
then:
|
||||||
|
tracer.config.serviceName == expected
|
||||||
|
|
||||||
|
where:
|
||||||
|
serviceName | expected
|
||||||
|
null | Config.DEFAULT_SERVICE_NAME
|
||||||
|
"" | Config.DEFAULT_SERVICE_NAME
|
||||||
|
"some-service" | "some-service"
|
||||||
|
}
|
||||||
|
|
||||||
def "test create current timestamp"() {
|
def "test create current timestamp"() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new Tracer(config)
|
def tracer = Tracer.builder().config(config).build()
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def timestamp = tracer.createCurrentTimestamp()
|
def timestamp = tracer.createCurrentTimestamp()
|
||||||
|
@ -45,7 +59,7 @@ class TracerTest extends Specification {
|
||||||
|
|
||||||
def "test trace happy path"() {
|
def "test trace happy path"() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new Tracer(config, testWriter, new AllSampler(), [])
|
def tracer = Tracer.builder().config(config).writer(testWriter).build()
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def rootSpan = tracer.buildTrace(null)
|
def rootSpan = tracer.buildTrace(null)
|
||||||
|
@ -76,7 +90,7 @@ class TracerTest extends Specification {
|
||||||
//TODO implement this test properly
|
//TODO implement this test properly
|
||||||
setup:
|
setup:
|
||||||
def context = Mock(SpanContext)
|
def context = Mock(SpanContext)
|
||||||
def tracer = new Tracer(config, testWriter, new AllSampler(), [])
|
def tracer = Tracer.builder().config(config).writer(testWriter).build()
|
||||||
|
|
||||||
when:
|
when:
|
||||||
tracer.inject(context, null, null)
|
tracer.inject(context, null, null)
|
||||||
|
@ -88,7 +102,7 @@ class TracerTest extends Specification {
|
||||||
def "test extract"() {
|
def "test extract"() {
|
||||||
//TODO implement this test properly
|
//TODO implement this test properly
|
||||||
setup:
|
setup:
|
||||||
def tracer = new Tracer(config, testWriter, new AllSampler(), [])
|
def tracer = Tracer.builder().config(config).writer(testWriter).build()
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def context = tracer.extract(null, null)
|
def context = tracer.extract(null, null)
|
||||||
|
@ -100,7 +114,7 @@ class TracerTest extends Specification {
|
||||||
def testReportError() {
|
def testReportError() {
|
||||||
//TODO implement this test properly
|
//TODO implement this test properly
|
||||||
setup:
|
setup:
|
||||||
def tracer = new Tracer(config, testWriter, new AllSampler(), [])
|
def tracer = Tracer.builder().config(config).writer(testWriter).build()
|
||||||
|
|
||||||
when:
|
when:
|
||||||
tracer.reportError("message %s", 123)
|
tracer.reportError("message %s", 123)
|
||||||
|
@ -111,7 +125,7 @@ class TracerTest extends Specification {
|
||||||
|
|
||||||
def "test trace that had a GCed span"() {
|
def "test trace that had a GCed span"() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new Tracer(config, testWriter, new AllSampler(), [])
|
def tracer = Tracer.builder().config(config).writer(testWriter).build()
|
||||||
|
|
||||||
when: "trace and spans get created"
|
when: "trace and spans get created"
|
||||||
def rootSpan = tracer.buildTrace(null)
|
def rootSpan = tracer.buildTrace(null)
|
||||||
|
@ -137,7 +151,7 @@ class TracerTest extends Specification {
|
||||||
|
|
||||||
def "test trace that had a GCed continuation"() {
|
def "test trace that had a GCed continuation"() {
|
||||||
setup:
|
setup:
|
||||||
def tracer = new Tracer(config, testWriter, new AllSampler(), [])
|
def tracer = Tracer.builder().config(config).writer(testWriter).build()
|
||||||
|
|
||||||
when: "trace and spans get created"
|
when: "trace and spans get created"
|
||||||
def rootSpan = tracer.buildTrace(null)
|
def rootSpan = tracer.buildTrace(null)
|
||||||
|
|
Loading…
Reference in New Issue