Backing decorators with a hash
MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
@@ -175,7 +175,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
this.tags.put(tag, value);
//Call decorators
- for (DDSpanContextDecorator decorator : tracer.getSpanContextDecorators()) {
+ for (DDSpanContextDecorator decorator : tracer.getSpanContextDecorators(tag)) {
decorator.afterSetTag(this, tag, value);
}
//Error management

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
package com.datadoghq.trace;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.datadoghq.trace.integration.DDSpanContextDecorator;
import com.datadoghq.trace.propagation.Codec;
import com.datadoghq.trace.propagation.HTTPCodec;
@@ -16,10 +7,13 @@ import com.datadoghq.trace.sampling.AllSampler;
import com.datadoghq.trace.sampling.Sampler;
import com.datadoghq.trace.writer.DDAgentWriter;
import com.datadoghq.trace.writer.Writer;
-
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.propagation.Format;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
/**
@@ -44,7 +38,7 @@ public class DDTracer implements io.opentracing.Tracer {
/**
* Span context decorators
*/
- private final List<DDSpanContextDecorator> spanContextDecorators = new ArrayList<DDSpanContextDecorator>();
+ private final Map<String, List<DDSpanContextDecorator>> spanContextDecorators = new HashMap<>();
private final static Logger logger = LoggerFactory.getLogger(DDTracer.class);
@@ -83,8 +77,15 @@ public class DDTracer implements io.opentracing.Tracer {
*
* @return the list of span context decorators
*/
- public List<DDSpanContextDecorator> getSpanContextDecorators() {
- return Collections.unmodifiableList(spanContextDecorators);
+ public List<DDSpanContextDecorator> getSpanContextDecorators(String tag) {
+ List<DDSpanContextDecorator> decorators = Collections.emptyList();
+ String key = getHashKey(tag);
+
+ if (spanContextDecorators.containsKey(key)) {
+ decorators = Collections.unmodifiableList(spanContextDecorators.get(key));
+ }
+
+ return decorators;
}
/**
@@ -93,9 +94,18 @@ public class DDTracer implements io.opentracing.Tracer {
* @param decorator The decorator in the list
*/
public void addDecorator(DDSpanContextDecorator decorator) {
- spanContextDecorators.add(decorator);
+ String key = getHashKey(decorator.getMatchingTag());
+
+ List<DDSpanContextDecorator> list = spanContextDecorators.get(key);
+ if (list == null) {
+ list = new ArrayList<>();
+ }
+ list.add(decorator);
+
+ spanContextDecorators.put(key, list);
}
+
public DDSpanBuilder buildSpan(String operationName) {
return new DDSpanBuilder(operationName);
}
@@ -317,6 +327,10 @@ public class DDTracer implements io.opentracing.Tracer {
}
+ private String getHashKey(String tag) {
+ return tag;
+ }
+
private static class CodecRegistry {
private final Map<Format<?>, Codec<?>> codecs = new HashMap<Format<?>, Codec<?>>();

MODIFY
dd-trace/src/test/java/com/datadoghq/trace/resolver/TracerResolverTest.java
dd-trace/src/test/java/com/datadoghq/trace/resolver/TracerResolverTest.java
@@ -4,6 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
+import io.opentracing.tag.Tags;
import org.junit.Test;
import com.datadoghq.trace.DDTracer;
@@ -18,18 +19,22 @@ public class TracerResolverTest {
DDTracerResolver tracerResolver = new DDTracerResolver();
DDTracer tracer = (DDTracer) tracerResolver.resolve();
- List<DDSpanContextDecorator> decorators = tracer.getSpanContextDecorators();
+ // for HTTP decorators
+ List<DDSpanContextDecorator> decorators = tracer.getSpanContextDecorators(Tags.COMPONENT.getKey());
assertThat(decorators.size()).isEqualTo(2);
DDSpanContextDecorator decorator = decorators.get(0);
assertThat(decorator.getClass()).isEqualTo(HTTPComponent.class);
HTTPComponent httpServiceDecorator = (HTTPComponent) decorator;
-
assertThat(httpServiceDecorator.getMatchingTag()).isEqualTo("component");
assertThat(httpServiceDecorator.getMatchingValue()).isEqualTo("hello");
assertThat(httpServiceDecorator.getSetValue()).isEqualTo("world");
- decorator = decorators.get(1);
+ // for URL decorators
+ decorators = tracer.getSpanContextDecorators(Tags.HTTP_URL.getKey());
+ assertThat(decorators.size()).isEqualTo(1);
+
+ decorator = decorators.get(0);
assertThat(decorator.getClass()).isEqualTo(URLAsResourceName.class);
}

MODIFY
dd-trace/src/test/resources/dd-trace-decorators.yaml
dd-trace/src/test/resources/dd-trace-decorators.yaml
@@ -2,4 +2,7 @@ decorators:
- type: HTTPComponent
matchingValue: hello
setValue: world
+ - type: HTTPComponent
+ matchingValue: foo
+ setValue: bar
- type: URLAsResourceName
\ No newline at end of file

Backing decorators with a hash