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.
This commit is contained in:
Tyler Benson 2018-07-23 12:44:02 +10:00
parent 1aaa3cafeb
commit de5d108afc
2 changed files with 26 additions and 0 deletions

View File

@ -57,6 +57,8 @@ public class DDTracer implements io.opentracing.Tracer {
/** A set of tags that are added to every span */ /** A set of tags that are added to every span */
private final Map<String, String> spanTags; private final Map<String, String> spanTags;
/** A configured mapping of service names to update with new values */
private final Map<String, String> serviceNameMappings;
/** Span context decorators */ /** Span context decorators */
private final Map<String, List<AbstractDecorator>> spanContextDecorators = private final Map<String, List<AbstractDecorator>> spanContextDecorators =
@ -116,6 +118,7 @@ public class DDTracer implements io.opentracing.Tracer {
this.writer.start(); this.writer.start();
this.sampler = sampler; this.sampler = sampler;
this.spanTags = defaultSpanTags; this.spanTags = defaultSpanTags;
this.serviceNameMappings = serviceNameMappings;
try { try {
Runtime.getRuntime() Runtime.getRuntime()
@ -522,6 +525,10 @@ public class DDTracer implements io.opentracing.Tracer {
serviceName = DDTracer.this.serviceName; serviceName = DDTracer.this.serviceName;
} }
if (serviceNameMappings.containsKey(serviceName)) {
serviceName = serviceNameMappings.get(serviceName);
}
final String operationName = final String operationName =
this.operationName != null ? this.operationName : this.resourceName; this.operationName != null ? this.operationName : this.resourceName;

View File

@ -5,12 +5,14 @@ import datadog.opentracing.DDTracer
import datadog.opentracing.SpanFactory import datadog.opentracing.SpanFactory
import datadog.trace.api.DDSpanTypes import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags import datadog.trace.api.DDTags
import datadog.trace.common.sampling.AllSampler
import datadog.trace.common.writer.LoggingWriter import datadog.trace.common.writer.LoggingWriter
import io.opentracing.tag.StringTag import io.opentracing.tag.StringTag
import io.opentracing.tag.Tags import io.opentracing.tag.Tags
import spock.lang.Specification import spock.lang.Specification
import static datadog.opentracing.DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME import static datadog.opentracing.DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME
import static java.util.Collections.emptyMap
class SpanDecoratorTest extends Specification { class SpanDecoratorTest extends Specification {
def tracer = new DDTracer(new LoggingWriter()) def tracer = new DDTracer(new LoggingWriter())
@ -74,6 +76,23 @@ class SpanDecoratorTest extends Specification {
"other-context" | "my-service" | "my-service" "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"() { def "set operation name"() {
when: when:
Tags.COMPONENT.set(span, component) Tags.COMPONENT.set(span, component)