Remove decorators infrastructure (#39)
This commit is contained in:
parent
7a826d2f27
commit
e94573df4f
|
@ -1,10 +1,8 @@
|
||||||
package datadog.opentracing;
|
package datadog.opentracing;
|
||||||
|
|
||||||
import datadog.opentracing.decorators.AbstractDecorator;
|
|
||||||
import datadog.trace.api.DDTags;
|
import datadog.trace.api.DDTags;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
@ -163,26 +161,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean addTag = true;
|
tags.put(tag, value);
|
||||||
|
|
||||||
// Call decorators
|
|
||||||
final List<AbstractDecorator> decorators = tracer.getSpanContextDecorators(tag);
|
|
||||||
if (decorators != null) {
|
|
||||||
for (final AbstractDecorator decorator : decorators) {
|
|
||||||
try {
|
|
||||||
addTag &= decorator.shouldSetTag(this, tag, value);
|
|
||||||
} catch (final Throwable ex) {
|
|
||||||
log.debug(
|
|
||||||
"Could not decorate the span decorator={}: {}",
|
|
||||||
decorator.getClass().getSimpleName(),
|
|
||||||
ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (addTag) {
|
|
||||||
tags.put(tag, value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized Map<String, Object> getTags() {
|
public synchronized Map<String, Object> getTags() {
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package datadog.opentracing;
|
package datadog.opentracing;
|
||||||
|
|
||||||
import datadog.opentracing.decorators.AbstractDecorator;
|
|
||||||
import datadog.opentracing.decorators.DDDecoratorsFactory;
|
|
||||||
import datadog.opentracing.propagation.ExtractedContext;
|
import datadog.opentracing.propagation.ExtractedContext;
|
||||||
import datadog.opentracing.propagation.HttpCodec;
|
import datadog.opentracing.propagation.HttpCodec;
|
||||||
import datadog.opentracing.scopemanager.ContextualScopeManager;
|
import datadog.opentracing.scopemanager.ContextualScopeManager;
|
||||||
|
@ -24,10 +22,8 @@ import java.math.BigInteger;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -54,10 +50,6 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
|
||||||
*/
|
*/
|
||||||
private final Thread shutdownCallback;
|
private final Thread shutdownCallback;
|
||||||
|
|
||||||
/** Span context decorators */
|
|
||||||
private final Map<String, List<AbstractDecorator>> spanContextDecorators =
|
|
||||||
new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
private final HttpCodec.Injector injector;
|
private final HttpCodec.Injector injector;
|
||||||
private final HttpCodec.Extractor extractor;
|
private final HttpCodec.Extractor extractor;
|
||||||
|
|
||||||
|
@ -102,11 +94,6 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
|
||||||
|
|
||||||
log.info("New instance: {}", this);
|
log.info("New instance: {}", this);
|
||||||
|
|
||||||
final List<AbstractDecorator> decorators = DDDecoratorsFactory.createBuiltinDecorators();
|
|
||||||
for (final AbstractDecorator decorator : decorators) {
|
|
||||||
addDecorator(decorator);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that PendingTrace.SPAN_CLEANER is initialized in this thread:
|
// Ensure that PendingTrace.SPAN_CLEANER is initialized in this thread:
|
||||||
// FIXME: add test to verify the span cleaner thread is started with this call.
|
// FIXME: add test to verify the span cleaner thread is started with this call.
|
||||||
PendingTrace.initialize();
|
PendingTrace.initialize();
|
||||||
|
@ -122,33 +109,6 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the list of span context decorators
|
|
||||||
*
|
|
||||||
* @return the list of span context decorators
|
|
||||||
*/
|
|
||||||
public List<AbstractDecorator> getSpanContextDecorators(final String tag) {
|
|
||||||
return spanContextDecorators.get(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a new decorator in the list ({@link AbstractDecorator})
|
|
||||||
*
|
|
||||||
* @param decorator The decorator in the list
|
|
||||||
*/
|
|
||||||
public void addDecorator(final AbstractDecorator decorator) {
|
|
||||||
|
|
||||||
List<AbstractDecorator> list = spanContextDecorators.get(decorator.getMatchingTag());
|
|
||||||
if (list == null) {
|
|
||||||
list = new ArrayList<>();
|
|
||||||
}
|
|
||||||
list.add(decorator);
|
|
||||||
|
|
||||||
spanContextDecorators.put(decorator.getMatchingTag(), list);
|
|
||||||
log.debug(
|
|
||||||
"Decorator added: '{}' -> {}", decorator.getMatchingTag(), decorator.getClass().getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public void addScopeContext(final ScopeContext context) {
|
public void addScopeContext(final ScopeContext context) {
|
||||||
scopeManager.addScopeContext(context);
|
scopeManager.addScopeContext(context);
|
||||||
|
@ -391,7 +351,6 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
|
||||||
final BigInteger parentSpanId;
|
final BigInteger parentSpanId;
|
||||||
final PendingTrace parentTrace;
|
final PendingTrace parentTrace;
|
||||||
|
|
||||||
final DDSpanContext context;
|
|
||||||
SpanContext parentContext = parent;
|
SpanContext parentContext = parent;
|
||||||
if (parentContext == null && !ignoreScope) {
|
if (parentContext == null && !ignoreScope) {
|
||||||
// use the Scope as parent unless overridden or ignored.
|
// use the Scope as parent unless overridden or ignored.
|
||||||
|
@ -426,47 +385,15 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
|
||||||
}
|
}
|
||||||
|
|
||||||
// some attributes are inherited from the parent
|
// some attributes are inherited from the parent
|
||||||
context =
|
return new DDSpanContext(
|
||||||
new DDSpanContext(
|
traceId,
|
||||||
traceId,
|
spanId,
|
||||||
spanId,
|
parentSpanId,
|
||||||
parentSpanId,
|
operationName,
|
||||||
operationName,
|
errorFlag,
|
||||||
errorFlag,
|
tags,
|
||||||
tags,
|
parentTrace,
|
||||||
parentTrace,
|
DDTracer.this);
|
||||||
DDTracer.this);
|
|
||||||
|
|
||||||
// Apply Decorators to handle any tags that may have been set via the builder.
|
|
||||||
for (final Map.Entry<String, Object> tag : tags.entrySet()) {
|
|
||||||
if (tag.getValue() == null) {
|
|
||||||
context.setTag(tag.getKey(), null);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean addTag = true;
|
|
||||||
|
|
||||||
// Call decorators
|
|
||||||
final List<AbstractDecorator> decorators = getSpanContextDecorators(tag.getKey());
|
|
||||||
if (decorators != null) {
|
|
||||||
for (final AbstractDecorator decorator : decorators) {
|
|
||||||
try {
|
|
||||||
addTag &= decorator.shouldSetTag(context, tag.getKey(), tag.getValue());
|
|
||||||
} catch (final Throwable ex) {
|
|
||||||
log.debug(
|
|
||||||
"Could not decorate the span decorator={}: {}",
|
|
||||||
decorator.getClass().getSimpleName(),
|
|
||||||
ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!addTag) {
|
|
||||||
context.setTag(tag.getKey(), null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return context;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,63 +0,0 @@
|
||||||
package datadog.opentracing.decorators;
|
|
||||||
|
|
||||||
import datadog.opentracing.DDSpanContext;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Span decorators are called when new tags are written and proceed to various remappings and
|
|
||||||
* enrichments
|
|
||||||
*/
|
|
||||||
public abstract class AbstractDecorator {
|
|
||||||
|
|
||||||
private String matchingTag;
|
|
||||||
|
|
||||||
private Object matchingValue;
|
|
||||||
|
|
||||||
private String replacementTag;
|
|
||||||
|
|
||||||
private String replacementValue;
|
|
||||||
|
|
||||||
public boolean shouldSetTag(final DDSpanContext context, final String tag, final Object value) {
|
|
||||||
if (this.getMatchingValue() == null || this.getMatchingValue().equals(value)) {
|
|
||||||
final String targetTag = getReplacementTag() == null ? tag : getReplacementTag();
|
|
||||||
final String targetValue =
|
|
||||||
getReplacementValue() == null ? String.valueOf(value) : getReplacementValue();
|
|
||||||
|
|
||||||
context.setTag(targetTag, targetValue);
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMatchingTag() {
|
|
||||||
return matchingTag;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMatchingTag(final String tag) {
|
|
||||||
this.matchingTag = tag;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getMatchingValue() {
|
|
||||||
return matchingValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMatchingValue(final Object value) {
|
|
||||||
this.matchingValue = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getReplacementTag() {
|
|
||||||
return replacementTag;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setReplacementTag(final String targetTag) {
|
|
||||||
this.replacementTag = targetTag;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getReplacementValue() {
|
|
||||||
return replacementValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setReplacementValue(final String targetValue) {
|
|
||||||
this.replacementValue = targetValue;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
package datadog.opentracing.decorators;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/** Create DDSpanDecorators */
|
|
||||||
public class DDDecoratorsFactory {
|
|
||||||
public static List<AbstractDecorator> createBuiltinDecorators() {
|
|
||||||
|
|
||||||
return Arrays.asList();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
package datadog.opentracing.decorators
|
|
||||||
|
|
||||||
import datadog.opentracing.DDSpanContext
|
|
||||||
import datadog.opentracing.DDTracer
|
|
||||||
import datadog.opentracing.SpanFactory
|
|
||||||
import datadog.trace.common.writer.LoggingWriter
|
|
||||||
import datadog.trace.util.test.DDSpecification
|
|
||||||
import io.opentracing.tag.StringTag
|
|
||||||
|
|
||||||
class SpanDecoratorTest extends DDSpecification {
|
|
||||||
|
|
||||||
def tracer = new DDTracer(new LoggingWriter())
|
|
||||||
def span = SpanFactory.newSpanOf(tracer)
|
|
||||||
|
|
||||||
def "adding span personalisation using Decorators"() {
|
|
||||||
setup:
|
|
||||||
def decorator = new AbstractDecorator() {
|
|
||||||
boolean shouldSetTag(DDSpanContext context, String tag, Object value) {
|
|
||||||
return super.shouldSetTag(context, tag, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
decorator.setMatchingTag("foo")
|
|
||||||
decorator.setMatchingValue("bar")
|
|
||||||
decorator.setReplacementTag("newFoo")
|
|
||||||
decorator.setReplacementValue("newBar")
|
|
||||||
tracer.addDecorator(decorator)
|
|
||||||
|
|
||||||
new StringTag("foo").set(span, "bar")
|
|
||||||
|
|
||||||
expect:
|
|
||||||
span.getTags().containsKey("newFoo")
|
|
||||||
span.getTags().get("newFoo") == "newBar"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -35,8 +35,6 @@ class DDTracerTest extends DDSpecification {
|
||||||
def tracer = new DDTracer()
|
def tracer = new DDTracer()
|
||||||
|
|
||||||
then:
|
then:
|
||||||
tracer.spanContextDecorators.size() == 0
|
|
||||||
|
|
||||||
tracer.injector instanceof DatadogHttpCodec.Injector
|
tracer.injector instanceof DatadogHttpCodec.Injector
|
||||||
tracer.extractor instanceof DatadogHttpCodec.Extractor
|
tracer.extractor instanceof DatadogHttpCodec.Extractor
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue