Remove decorators infrastructure (#39)

This commit is contained in:
Trask Stalnaker 2019-12-17 10:58:31 -08:00 committed by GitHub
parent 7a826d2f27
commit e94573df4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 215 deletions

View File

@ -1,10 +1,8 @@
package datadog.opentracing;
import datadog.opentracing.decorators.AbstractDecorator;
import datadog.trace.api.DDTags;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
@ -163,27 +161,8 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return;
}
boolean addTag = true;
// 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() {
return Collections.unmodifiableMap(tags);

View File

@ -1,7 +1,5 @@
package datadog.opentracing;
import datadog.opentracing.decorators.AbstractDecorator;
import datadog.opentracing.decorators.DDDecoratorsFactory;
import datadog.opentracing.propagation.ExtractedContext;
import datadog.opentracing.propagation.HttpCodec;
import datadog.opentracing.scopemanager.ContextualScopeManager;
@ -24,10 +22,8 @@ import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@ -54,10 +50,6 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
*/
private final Thread shutdownCallback;
/** Span context decorators */
private final Map<String, List<AbstractDecorator>> spanContextDecorators =
new ConcurrentHashMap<>();
private final HttpCodec.Injector injector;
private final HttpCodec.Extractor extractor;
@ -102,11 +94,6 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
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:
// FIXME: add test to verify the span cleaner thread is started with this call.
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
public void addScopeContext(final ScopeContext context) {
scopeManager.addScopeContext(context);
@ -391,7 +351,6 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
final BigInteger parentSpanId;
final PendingTrace parentTrace;
final DDSpanContext context;
SpanContext parentContext = parent;
if (parentContext == null && !ignoreScope) {
// use the Scope as parent unless overridden or ignored.
@ -426,8 +385,7 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
}
// some attributes are inherited from the parent
context =
new DDSpanContext(
return new DDSpanContext(
traceId,
spanId,
parentSpanId,
@ -436,37 +394,6 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
tags,
parentTrace,
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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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"
}
}

View File

@ -35,8 +35,6 @@ class DDTracerTest extends DDSpecification {
def tracer = new DDTracer()
then:
tracer.spanContextDecorators.size() == 0
tracer.injector instanceof DatadogHttpCodec.Injector
tracer.extractor instanceof DatadogHttpCodec.Extractor
}