Add tests for hostname detection
MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
@@ -81,6 +81,12 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
private final HttpCodec.Injector injector;
private final HttpCodec.Extractor extractor;
+ /**
+ * hostname is expensive to calculate so we detect it only when creating the tracer and we cache
+ * it for future reuse.
+ */
+ private String hostname;
+
/** By default, report to local agent and collect all traces. */
public DDTracer() {
this(Config.get());
@@ -248,6 +254,8 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
// Ensure that PendingTrace.SPAN_CLEANER is initialized in this thread:
// FIXME: add test to verify the span cleaner thread is started with this call.
PendingTrace.initialize();
+
+ cacheHostName();
}
@Override
@@ -736,24 +744,34 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
}
/**
- * Sets the internal hostname tag on a root span if appropriate. The following acceptance criteria
- * apply: 1) Users should not be able to overwrite this value, 2) It has to be done only on the
- * root span, 3) It is not guaranteed that the first span in the list is the root span.
- *
- * @param spans
+ * Hostname is expensive to retrieve so we provide a way to calculate it and cache it in the
+ * tracer itself for further reuse.
*/
- private void applyHostNameDetection(List<DDSpan> spans) {
+ private void cacheHostName() {
// Host name detection can be disabled via configuration
if (!Config.get().isReportHostName()) {
return;
}
- String hostname;
try {
- hostname = InetAddress.getLocalHost().getHostName();
+ this.hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
// If we are not able to detect the hostname we do not throw an exception.
+ }
+ }
+
+ /**
+ * Sets the internal hostname tag on a root span if appropriate. The following acceptance criteria
+ * apply: 1) Users should not be able to overwrite this value, 2) It has to be done only on the
+ * root span, 3) It is not guaranteed that the first span in the list is the root span.
+ *
+ * @param spans
+ */
+ private void applyHostNameDetection(List<DDSpan> spans) {
+
+ // Host name detection can be disabled via configuration
+ if (!Config.get().isReportHostName()) {
return;
}

MODIFY
dd-trace-ot/src/test/groovy/datadog/trace/DDTracerTest.groovy
dd-trace-ot/src/test/groovy/datadog/trace/DDTracerTest.groovy
package datadog.trace
+import datadog.opentracing.DDSpan
import datadog.opentracing.DDTracer
import datadog.opentracing.propagation.HttpCodec
import datadog.trace.api.Config
+import datadog.trace.api.DDTags
import datadog.trace.common.sampling.AllSampler
import datadog.trace.common.sampling.RateByServiceSampler
import datadog.trace.common.writer.DDAgentWriter
@@ -31,7 +33,7 @@ class DDTracerTest extends Specification {
// assert that a trace agent isn't running locally as that messes up the test.
try {
(new Socket("localhost", 8126)).close()
- throw new IllegalStateException("Trace Agent unexpectedly running locally.")
+ throw new IllegalStateException("An agent is already running locally on port 8126. Please stop it if you want to run tests locally.")
} catch (final ConnectException ioe) {
// trace agent is not running locally.
}
@@ -144,4 +146,35 @@ class DDTracerTest extends Specification {
PRIORITY_SAMPLING | "true"
PRIORITY_SAMPLING | "false"
}
+
+ def "tracer does not set the host name by default"() {
+ setup:
+ def tracer = new DDTracer('my_service', new ListWriter(), new AllSampler())
+
+ when:
+ DDSpan root = tracer.buildSpan('my_root').start()
+ DDSpan child = tracer.buildSpan('my_child').asChildOf(root).start()
+ child.finish()
+ root.finish()
+
+ then:
+ !root.context().tags.containsKey(DDTags.INTERNAL_HOST_NAME)
+ !child.context().tags.containsKey(DDTags.INTERNAL_HOST_NAME)
+ }
+
+ def "tracer sets the host name if activated only on root span"() {
+ setup:
+ System.setProperty('dd.trace.report-hostname', 'true')
+ def tracer = new DDTracer('my_service', new ListWriter(), new AllSampler())
+
+ when:
+ DDSpan root = tracer.buildSpan('my_root').start()
+ DDSpan child = tracer.buildSpan('my_child').asChildOf(root).start()
+ child.finish()
+ root.finish()
+
+ then:
+ root.context().tags.get(DDTags.INTERNAL_HOST_NAME) == InetAddress.localHost.hostName
+ !child.context().tags.containsKey(DDTags.INTERNAL_HOST_NAME)
+ }
}

Add tests for hostname detection