Lazy init Writer from config to avoid premature resource allocation.
MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
@@ -97,27 +97,20 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
private final HttpCodec.Extractor extractor;
public static class Builder {
+
public Builder() {
// Apply the default values from config.
- withConfig(Config.get());
- }
-
- public Builder writer(final Writer writer) {
- if (this.writer != null && this.writer != writer) {
- // Try to avoid leaking resources
- this.writer.close();
- }
- this.writer = writer;
- return this;
+ config(Config.get());
}
public Builder withProperties(final Properties properties) {
- return withConfig(Config.get(properties));
+ return config(Config.get(properties));
}
- public Builder withConfig(final Config config) {
+ public Builder config(final Config config) {
+ this.config = config;
serviceName(config.getServiceName());
- writer(Writer.Builder.forConfig(config));
+ // Explicitly skip setting writer to avoid allocating resources prematurely.
sampler(Sampler.Builder.forConfig(config));
injector(HttpCodec.createInjector(config));
extractor(HttpCodec.createExtractor(config, config.getHeaderTags()));
@@ -259,6 +252,7 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
final Map<String, String> taggedHeaders,
final int partialFlushMinSpans) {
this(
+ Config.get(),
serviceName,
writer,
sampler,
@@ -274,6 +268,7 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
@lombok.Builder(builderClassName = "Builder")
// These field names must be stable to ensure the builder api is stable.
private DDTracer(
+ final Config config,
final String serviceName,
final Writer writer,
final Sampler sampler,
@@ -285,14 +280,17 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
final Map<String, String> taggedHeaders,
final int partialFlushMinSpans) {
- assert writer != null;
assert localRootSpanTags != null;
assert defaultSpanTags != null;
assert serviceNameMappings != null;
assert taggedHeaders != null;
this.serviceName = serviceName;
+ if (writer == null) {
+ this.writer = Writer.Builder.forConfig(config);
+ } else {
this.writer = writer;
+ }
this.sampler = sampler;
this.injector = injector;
this.extractor = extractor;

MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/resolver/DDTracerResolver.java
dd-trace-ot/src/main/java/datadog/opentracing/resolver/DDTracerResolver.java
@@ -16,7 +16,7 @@ public class DDTracerResolver extends TracerResolver {
Tracer resolve(final Config config) {
if (config.isTraceResolverEnabled()) {
log.info("Creating DDTracer with DDTracerResolver");
- return DDTracer.builder().withConfig(config).build();
+ return DDTracer.builder().config(config).build();
} else {
log.info("DDTracerResolver disabled");
return null;

MODIFY
dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanBuilderTest.groovy
dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanBuilderTest.groovy
@@ -458,7 +458,7 @@ class DDSpanBuilderTest extends DDSpecification {
setup:
System.setProperty("dd.trace.span.tags", tagString)
def config = new Config()
- tracer = DDTracer.builder().withConfig(config).writer(writer).build()
+ tracer = DDTracer.builder().config(config).writer(writer).build()
def span = tracer.buildSpan("op name").withServiceName("foo").start()
expect:

MODIFY
dd-trace-ot/src/test/groovy/datadog/opentracing/PendingTraceTest.groovy
dd-trace-ot/src/test/groovy/datadog/opentracing/PendingTraceTest.groovy
@@ -195,7 +195,7 @@ class PendingTraceTest extends DDSpecification {
def properties = new Properties()
properties.setProperty(PARTIAL_FLUSH_MIN_SPANS, "1")
def config = Config.get(properties)
- def tracer = DDTracer.builder().withConfig(config).writer(writer).build()
+ def tracer = DDTracer.builder().config(config).writer(writer).build()
def trace = new PendingTrace(tracer, traceId, [:])
def rootSpan = SpanFactory.newSpanOf(trace)
def child1 = tracer.buildSpan("child1").asChildOf(rootSpan).start()
@@ -241,7 +241,7 @@ class PendingTraceTest extends DDSpecification {
def properties = new Properties()
properties.setProperty(PARTIAL_FLUSH_MIN_SPANS, "1")
def config = Config.get(properties)
- def tracer = DDTracer.builder().withConfig(config).writer(writer).build()
+ def tracer = DDTracer.builder().config(config).writer(writer).build()
def trace = new PendingTrace(tracer, traceId, [:])
def rootSpan = SpanFactory.newSpanOf(trace)
def child1 = tracer.buildSpan("child1").asChildOf(rootSpan).start()

MODIFY
dd-trace-ot/src/test/groovy/datadog/trace/DDTracerTest.groovy
dd-trace-ot/src/test/groovy/datadog/trace/DDTracerTest.groovy
@@ -63,7 +63,7 @@ class DDTracerTest extends DDSpecification {
System.setProperty(PREFIX + HEALTH_METRICS_ENABLED, "true")
when:
- def tracer = DDTracer.builder().withConfig(new Config()).build()
+ def tracer = DDTracer.builder().config(new Config()).build()
then:
tracer.writer.monitor instanceof Monitor.StatsD
@@ -75,7 +75,7 @@ class DDTracerTest extends DDSpecification {
setup:
System.setProperty(PREFIX + PRIORITY_SAMPLING, "false")
when:
- def tracer = DDTracer.builder().withConfig(new Config()).build()
+ def tracer = DDTracer.builder().config(new Config()).build()
then:
tracer.sampler instanceof AllSampler
}
@@ -85,7 +85,7 @@ class DDTracerTest extends DDSpecification {
System.setProperty(PREFIX + WRITER_TYPE, "LoggingWriter")
when:
- def tracer = DDTracer.builder().withConfig(new Config()).build()
+ def tracer = DDTracer.builder().config(new Config()).build()
then:
tracer.writer instanceof LoggingWriter
@@ -98,7 +98,7 @@ class DDTracerTest extends DDSpecification {
System.setProperty(PREFIX + HEADER_TAGS, mapString)
when:
- def tracer = DDTracer.builder().withConfig(new Config()).build()
+ def tracer = DDTracer.builder().config(new Config()).build()
// Datadog extractor gets placed first
def taggedHeaders = tracer.extractor.extractors[0].taggedHeaders
@@ -116,7 +116,7 @@ class DDTracerTest extends DDSpecification {
def "verify overriding host"() {
when:
System.setProperty(PREFIX + key, value)
- def tracer = DDTracer.builder().withConfig(new Config()).build()
+ def tracer = DDTracer.builder().config(new Config()).build()
then:
tracer.writer instanceof DDAgentWriter
@@ -131,7 +131,7 @@ class DDTracerTest extends DDSpecification {
def "verify overriding port"() {
when:
System.setProperty(PREFIX + key, value)
- def tracer = DDTracer.builder().withConfig(new Config()).build()
+ def tracer = DDTracer.builder().config(new Config()).build()
then:
tracer.writer instanceof DDAgentWriter
@@ -147,7 +147,7 @@ class DDTracerTest extends DDSpecification {
def "Writer is instance of LoggingWriter when property set"() {
when:
System.setProperty(PREFIX + "writer.type", "LoggingWriter")
- def tracer = DDTracer.builder().withConfig(new Config()).build()
+ def tracer = DDTracer.builder().config(new Config()).build()
then:
tracer.writer instanceof LoggingWriter

Lazy init Writer from config to avoid premature resource allocation.