[core] renaming decorators to AbstractDecorator
MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
package com.datadoghq.trace;
-import com.datadoghq.trace.integration.DDSpanContextDecorator;
+import com.datadoghq.trace.integration.AbstractDecorator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import io.opentracing.tag.Tags;
@@ -28,28 +28,27 @@ public class DDSpanContext implements io.opentracing.SpanContext {
private final long parentId;
private final String threadName = Thread.currentThread().getName();
private final long threadId = Thread.currentThread().getId();
- private Map<String, String> baggageItems;
+ /** The collection of all span related to this one */
+ private final Queue<DDBaseSpan<?>> trace;
// DD attributes
+ /** For technical reasons, the ref to the original tracer */
+ private final DDTracer tracer;
+
+ private Map<String, String> baggageItems;
/** The service name is required, otherwise the span are dropped by the agent */
private String serviceName;
/** The resource associated to the service (server_web, database, etc.) */
private String resourceName;
/** True indicates that the span reports an error */
private boolean errorFlag;
-
/** The type of the span. If null, the Datadog Agent will report as a custom */
private String spanType;
- /** The collection of all span related to this one */
- private final Queue<DDBaseSpan<?>> trace;
/** Each span have an operation name describing the current span */
private String operationName;
-
+ // Others attributes
/** Tags are associated to the current span, they will not propagate to the children span */
private Map<String, Object> tags;
- // Others attributes
- /** For technical reasons, the ref to the original tracer */
- private final DDTracer tracer;
public DDSpanContext(
final long traceId,
@@ -109,12 +108,20 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return serviceName;
}
+ public void setServiceName(final String serviceName) {
+ this.serviceName = serviceName;
+ }
+
public String getResourceName() {
return this.resourceName == null || this.resourceName.isEmpty()
? this.operationName
: this.resourceName;
}
+ public void setResourceName(final String resourceName) {
+ this.resourceName = resourceName;
+ }
+
public boolean getErrorFlag() {
return errorFlag;
}
@@ -127,6 +134,10 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return spanType;
}
+ public void setSpanType(final String spanType) {
+ this.spanType = spanType;
+ }
+
public void setBaggageItem(final String key, final String value) {
if (this.baggageItems.isEmpty()) {
this.baggageItems = new HashMap<String, String>();
@@ -184,9 +195,9 @@ public class DDSpanContext implements io.opentracing.SpanContext {
this.tags.put(tag, value);
//Call decorators
- final List<DDSpanContextDecorator> decorators = tracer.getSpanContextDecorators(tag);
+ final List<AbstractDecorator> decorators = tracer.getSpanContextDecorators(tag);
if (decorators != null) {
- for (final DDSpanContextDecorator decorator : decorators) {
+ for (final AbstractDecorator decorator : decorators) {
decorator.afterSetTag(this, tag, value);
}
}
@@ -223,23 +234,11 @@ public class DDSpanContext implements io.opentracing.SpanContext {
.toString();
}
- public void setOperationName(final String operationName) {
- this.operationName = operationName;
- }
-
public String getOperationName() {
return operationName;
}
- public void setServiceName(final String serviceName) {
- this.serviceName = serviceName;
- }
-
- public void setResourceName(final String resourceName) {
- this.resourceName = resourceName;
- }
-
- public void setSpanType(final String spanType) {
- this.spanType = spanType;
+ public void setOperationName(final String operationName) {
+ this.operationName = operationName;
}
}

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 com.datadoghq.trace.integration.DDSpanContextDecorator;
+import com.datadoghq.trace.integration.AbstractDecorator;
import com.datadoghq.trace.propagation.Codec;
import com.datadoghq.trace.propagation.HTTPCodec;
import com.datadoghq.trace.sampling.AllSampler;
@@ -39,7 +39,7 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
private final String defaultServiceName;
/** Span context decorators */
- private final Map<String, List<DDSpanContextDecorator>> spanContextDecorators = new HashMap<>();
+ private final Map<String, List<AbstractDecorator>> spanContextDecorators = new HashMap<>();
private final CodecRegistry registry;
@@ -70,18 +70,18 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
*
* @return the list of span context decorators
*/
- public List<DDSpanContextDecorator> getSpanContextDecorators(final String tag) {
+ public List<AbstractDecorator> getSpanContextDecorators(final String tag) {
return spanContextDecorators.get(tag);
}
/**
- * Add a new decorator in the list ({@link DDSpanContextDecorator})
+ * Add a new decorator in the list ({@link AbstractDecorator})
*
* @param decorator The decorator in the list
*/
- public void addDecorator(final DDSpanContextDecorator decorator) {
+ public void addDecorator(final AbstractDecorator decorator) {
- List<DDSpanContextDecorator> list = spanContextDecorators.get(decorator.getMatchingTag());
+ List<AbstractDecorator> list = spanContextDecorators.get(decorator.getMatchingTag());
if (list == null) {
list = new ArrayList<>();
}

ADD
dd-trace/src/main/java/com/datadoghq/trace/integration/AbstractDecorator.java
+package com.datadoghq.trace.integration;
+
+import com.datadoghq.trace.DDSpanContext;
+import com.datadoghq.trace.DDTags;
+
+/**
+ * Span decorators are called when new tags are written and proceed to various remappings and
+ * enrichments
+ */
+public abstract class AbstractDecorator {
+
+ private String matchingTag;
+
+ private String matchingValue;
+
+ private String setTag;
+
+ private String setValue;
+
+ public boolean afterSetTag(final DDSpanContext context, final String tag, final Object value) {
+ if ((this.getMatchingValue() == null || value.equals(this.getMatchingValue()))) {
+ final String targetTag = getSetTag() == null ? tag : getSetTag();
+ final String targetValue = getSetValue() == null ? String.valueOf(value) : getSetValue();
+
+ if (targetTag.equals(DDTags.SERVICE_NAME)) {
+ context.setServiceName(targetValue);
+ } else if (targetTag.equals(DDTags.RESOURCE_NAME)) {
+ context.setResourceName(targetValue);
+ } else if (targetTag.equals(DDTags.SPAN_TYPE)) {
+ context.setSpanType(targetValue);
+ } else {
+ context.setTag(targetTag, targetValue);
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public String getMatchingTag() {
+ return matchingTag;
+ }
+
+ public void setMatchingTag(final String tag) {
+ this.matchingTag = tag;
+ }
+
+ public String getMatchingValue() {
+ return matchingValue;
+ }
+
+ public void setMatchingValue(final String value) {
+ this.matchingValue = value;
+ }
+
+ public String getSetTag() {
+ return setTag;
+ }
+
+ public void setSetTag(final String targetTag) {
+ this.setTag = targetTag;
+ }
+
+ public String getSetValue() {
+ return setValue;
+ }
+
+ public void setSetValue(final String targetValue) {
+ this.setValue = targetValue;
+ }
+}

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java
dd-trace/src/main/java/com/datadoghq/trace/integration/DBStatementAsResourceName.java
@@ -3,7 +3,7 @@ package com.datadoghq.trace.integration;
import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags;
-public class DBStatementAsResourceName extends DDSpanContextDecorator {
+public class DBStatementAsResourceName extends AbstractDecorator {
public DBStatementAsResourceName() {
super();

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/integration/ErrorFlag.java
dd-trace/src/main/java/com/datadoghq/trace/integration/ErrorFlag.java
@@ -3,18 +3,18 @@ package com.datadoghq.trace.integration;
import com.datadoghq.trace.DDSpanContext;
import io.opentracing.tag.Tags;
-public class ErrorFlag extends DDSpanContextDecorator {
+public class ErrorFlag extends AbstractDecorator {
public ErrorFlag() {
super();
this.setMatchingTag(Tags.ERROR.getKey());
}
@Override
- public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
+ public boolean afterSetTag(final DDSpanContext context, final String tag, final Object value) {
//Assign resource name
try {
context.setErrorFlag(Boolean.parseBoolean(String.valueOf(value)));
- } catch (Throwable t) {
+ } catch (final Throwable t) {
//DO NOTHING
}
return true;

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/integration/HTTPComponent.java
dd-trace/src/main/java/com/datadoghq/trace/integration/HTTPComponent.java
@@ -8,7 +8,7 @@ import io.opentracing.tag.Tags;
* This span decorator leverages HTTP tags. It allows the dev to define a custom service name and
* retrieves some HTTP meta such as the request path
*/
-public class HTTPComponent extends DDSpanContextDecorator {
+public class HTTPComponent extends AbstractDecorator {
public HTTPComponent() {
super();
@@ -17,7 +17,7 @@ public class HTTPComponent extends DDSpanContextDecorator {
}
@Override
- public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
+ public boolean afterSetTag(final DDSpanContext context, final String tag, final Object value) {
//Assign service name
if (super.afterSetTag(context, tag, value)) {
//Assign span type to WEB

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/integration/URLAsResourceName.java
dd-trace/src/main/java/com/datadoghq/trace/integration/URLAsResourceName.java
@@ -5,7 +5,7 @@ import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags;
import java.net.MalformedURLException;
-public class URLAsResourceName extends DDSpanContextDecorator {
+public class URLAsResourceName extends AbstractDecorator {
public URLAsResourceName() {
super();
@@ -14,12 +14,12 @@ public class URLAsResourceName extends DDSpanContextDecorator {
}
@Override
- public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
+ public boolean afterSetTag(final DDSpanContext context, final String tag, final Object value) {
//Assign resource name
try {
- String path = new java.net.URL(String.valueOf(value)).getPath();
+ final String path = new java.net.URL(String.valueOf(value)).getPath();
context.setResourceName(path);
- } catch (MalformedURLException e) {
+ } catch (final MalformedURLException e) {
context.setResourceName(String.valueOf(value));
}
return true;

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDDecoratorsFactory.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDDecoratorsFactory.java
package com.datadoghq.trace.resolver;
-import com.datadoghq.trace.integration.DDSpanContextDecorator;
+import com.datadoghq.trace.integration.AbstractDecorator;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
@@ -9,9 +9,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DDDecoratorsFactory {
- public static String DECORATORS_PACKAGE = "com.datadoghq.trace.integration.";
-
public static final String CONFIG_PATH = "dd-trace-decorators";
+ public static String DECORATORS_PACKAGE = "com.datadoghq.trace.integration.";
/**
* Create decorators from configuration
@@ -19,9 +18,8 @@ public class DDDecoratorsFactory {
* @param decoratorsConfig
* @return the list of instanciated and configured decorators
*/
- public static List<DDSpanContextDecorator> create(
- final List<DDSpanDecoratorConfig> decoratorsConfig) {
- final List<DDSpanContextDecorator> decorators = new ArrayList<>();
+ public static List<AbstractDecorator> create(final List<DDSpanDecoratorConfig> decoratorsConfig) {
+ final List<AbstractDecorator> decorators = new ArrayList<>();
for (final DDSpanDecoratorConfig decoratorConfig : decoratorsConfig) {
if (decoratorConfig.getType() == null) {
log.warn("Cannot create decorator without type from configuration {}", decoratorConfig);
@@ -39,9 +37,9 @@ public class DDDecoratorsFactory {
continue;
}
- DDSpanContextDecorator decorator = null;
+ AbstractDecorator decorator = null;
try {
- decorator = (DDSpanContextDecorator) decoratorClass.getConstructor().newInstance();
+ decorator = (AbstractDecorator) decoratorClass.getConstructor().newInstance();
} catch (final Exception e) {
log.warn(
"Cannot create decorator as we could not invoke the default constructor. Provided configuration {}",
@@ -68,8 +66,8 @@ public class DDDecoratorsFactory {
return decorators;
}
- public static List<DDSpanContextDecorator> createFromResources() {
- List<DDSpanContextDecorator> result = new ArrayList<>();
+ public static List<AbstractDecorator> createFromResources() {
+ List<AbstractDecorator> result = new ArrayList<>();
final TracerConfig config =
FactoryUtils.loadConfigFromResource(CONFIG_PATH, TracerConfig.class);
if (config != null) {

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerFactory.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerFactory.java
package com.datadoghq.trace.resolver;
import com.datadoghq.trace.DDTracer;
-import com.datadoghq.trace.integration.DDSpanContextDecorator;
+import com.datadoghq.trace.integration.AbstractDecorator;
import com.datadoghq.trace.sampling.AbstractSampler;
import com.datadoghq.trace.sampling.AllSampler;
import com.datadoghq.trace.sampling.RateSampler;
@@ -102,8 +102,8 @@ public class DDTracerFactory {
}
//Create decorators from resource files
- final List<DDSpanContextDecorator> decorators = DDDecoratorsFactory.createFromResources();
- for (final DDSpanContextDecorator decorator : decorators) {
+ final List<AbstractDecorator> decorators = DDDecoratorsFactory.createFromResources();
+ for (final AbstractDecorator decorator : decorators) {
tracer.addDecorator(decorator);
}

MODIFY
dd-trace/src/test/java/com/datadoghq/trace/resolver/TracerResolverTest.java
dd-trace/src/test/java/com/datadoghq/trace/resolver/TracerResolverTest.java
@@ -3,7 +3,7 @@ package com.datadoghq.trace.resolver;
import static org.assertj.core.api.Assertions.assertThat;
import com.datadoghq.trace.DDTracer;
-import com.datadoghq.trace.integration.DDSpanContextDecorator;
+import com.datadoghq.trace.integration.AbstractDecorator;
import com.datadoghq.trace.integration.HTTPComponent;
import com.datadoghq.trace.integration.URLAsResourceName;
import io.opentracing.NoopTracerFactory;
@@ -19,17 +19,16 @@ public class TracerResolverTest {
@Test
public void testResolve() {
- DDTracerResolver tracerResolver = new DDTracerResolver();
- DDTracer tracer = (DDTracer) tracerResolver.resolve();
+ final DDTracerResolver tracerResolver = new DDTracerResolver();
+ final DDTracer tracer = (DDTracer) tracerResolver.resolve();
// for HTTP decorators
- List<DDSpanContextDecorator> decorators =
- tracer.getSpanContextDecorators(Tags.COMPONENT.getKey());
+ List<AbstractDecorator> decorators = tracer.getSpanContextDecorators(Tags.COMPONENT.getKey());
assertThat(decorators.size()).isEqualTo(2);
- DDSpanContextDecorator decorator = decorators.get(0);
+ AbstractDecorator decorator = decorators.get(0);
assertThat(decorator.getClass()).isEqualTo(HTTPComponent.class);
- HTTPComponent httpServiceDecorator = (HTTPComponent) decorator;
+ final HTTPComponent httpServiceDecorator = (HTTPComponent) decorator;
assertThat(httpServiceDecorator.getMatchingTag()).isEqualTo("component");
assertThat(httpServiceDecorator.getMatchingValue()).isEqualTo("hello");
assertThat(httpServiceDecorator.getSetValue()).isEqualTo("world");
@@ -44,13 +43,13 @@ public class TracerResolverTest {
@Test
public void testResolveTracer() throws Exception {
- Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
+ final Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
tracerField.setAccessible(true);
tracerField.set(null, NoopTracerFactory.create());
assertThat(GlobalTracer.isRegistered()).isFalse();
- Tracer tracer = TracerResolver.resolveTracer();
+ final Tracer tracer = TracerResolver.resolveTracer();
assertThat(GlobalTracer.isRegistered()).isFalse();
assertThat(tracer).isInstanceOf(DDTracer.class);
@@ -58,7 +57,7 @@ public class TracerResolverTest {
@Test
public void testRegisterTracer() throws Exception {
- Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
+ final Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
tracerField.setAccessible(true);
tracerField.set(null, NoopTracerFactory.create());

[core] renaming decorators to AbstractDecorator