From de5d108afc06bbf9899259dd7ac87302714c72bd Mon Sep 17 00:00:00 2001 From: Tyler Benson Date: Mon, 23 Jul 2018 12:44:02 +1000 Subject: [PATCH] Allow service name mapping to apply without setting the tag. Previously the mapping only applied when the `service.name` tag was set, not on the default or configured service name. --- .../java/datadog/opentracing/DDTracer.java | 7 +++++++ .../decorators/SpanDecoratorTest.groovy | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java b/dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java index 26af9f2629..c9002bf085 100644 --- a/dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java +++ b/dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java @@ -57,6 +57,8 @@ public class DDTracer implements io.opentracing.Tracer { /** A set of tags that are added to every span */ private final Map spanTags; + /** A configured mapping of service names to update with new values */ + private final Map serviceNameMappings; /** Span context decorators */ private final Map> spanContextDecorators = @@ -116,6 +118,7 @@ public class DDTracer implements io.opentracing.Tracer { this.writer.start(); this.sampler = sampler; this.spanTags = defaultSpanTags; + this.serviceNameMappings = serviceNameMappings; try { Runtime.getRuntime() @@ -522,6 +525,10 @@ public class DDTracer implements io.opentracing.Tracer { serviceName = DDTracer.this.serviceName; } + if (serviceNameMappings.containsKey(serviceName)) { + serviceName = serviceNameMappings.get(serviceName); + } + final String operationName = this.operationName != null ? this.operationName : this.resourceName; diff --git a/dd-trace-ot/src/test/groovy/datadog/opentracing/decorators/SpanDecoratorTest.groovy b/dd-trace-ot/src/test/groovy/datadog/opentracing/decorators/SpanDecoratorTest.groovy index 520567c03a..ed558ece95 100644 --- a/dd-trace-ot/src/test/groovy/datadog/opentracing/decorators/SpanDecoratorTest.groovy +++ b/dd-trace-ot/src/test/groovy/datadog/opentracing/decorators/SpanDecoratorTest.groovy @@ -5,12 +5,14 @@ import datadog.opentracing.DDTracer import datadog.opentracing.SpanFactory import datadog.trace.api.DDSpanTypes import datadog.trace.api.DDTags +import datadog.trace.common.sampling.AllSampler import datadog.trace.common.writer.LoggingWriter import io.opentracing.tag.StringTag import io.opentracing.tag.Tags import spock.lang.Specification import static datadog.opentracing.DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME +import static java.util.Collections.emptyMap class SpanDecoratorTest extends Specification { def tracer = new DDTracer(new LoggingWriter()) @@ -74,6 +76,23 @@ class SpanDecoratorTest extends Specification { "other-context" | "my-service" | "my-service" } + def "default or configured service name can be remapped without setting tag"() { + setup: + tracer = new DDTracer(serviceName, new LoggingWriter(), new AllSampler(), emptyMap(), mapping, emptyMap()) + + when: + def span = tracer.buildSpan("some span").start() + + then: + span.serviceName == expected + + where: + serviceName | expected | mapping + UNASSIGNED_DEFAULT_SERVICE_NAME | UNASSIGNED_DEFAULT_SERVICE_NAME | ["other-service-name": "other-service"] + UNASSIGNED_DEFAULT_SERVICE_NAME | "new-service" | [(UNASSIGNED_DEFAULT_SERVICE_NAME): "new-service"] + "other-service-name" | "other-service" | ["other-service-name": "other-service"] + } + def "set operation name"() { when: Tags.COMPONENT.set(span, component)