Merge branch 'dev' of github.com:DataDog/dd-trace-java into dev

This commit is contained in:
renaudboutet 2017-05-29 17:33:13 +02:00
commit 8e14d51d58
8 changed files with 324 additions and 310 deletions

View File

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

View File

@ -2,4 +2,7 @@ decorators:
- type: HTTPComponent
matchingValue: hello
setValue: world
- type: HTTPComponent
matchingValue: foo
setValue: bar
- type: URLAsResourceName

View File

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

View File

@ -1,14 +1,5 @@
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,8 +94,17 @@ 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<?>>();

View File

@ -2,7 +2,6 @@ package com.datadoghq.trace.integration;
import com.datadoghq.trace.DDSpanContext;
import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags;
/**
@ -25,9 +24,7 @@ public class DBComponent extends DDSpanContextDecorator {
context.setSpanType("db");
//Assign resource name
if(tag.equals(Tags.DB_STATEMENT.getKey())){
context.setResourceName(String.valueOf(value));
}
return true;
}
return false;

View File

@ -16,7 +16,7 @@ public abstract class DDSpanContextDecorator {
private String setValue;
public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
if(tag.equals(this.getMatchingTag()) && (this.getMatchingValue()==null || value.equals(this.getMatchingValue()))){
if ((this.getMatchingValue() == null || value.equals(this.getMatchingValue()))) {
String targetTag = getSetTag() == null ? tag : getSetTag();
String targetValue = getSetValue() == null ? String.valueOf(value) : getSetTag();
context.setTag(targetTag, targetValue);

View File

@ -2,7 +2,6 @@ package com.datadoghq.trace.integration;
import com.datadoghq.trace.DDSpanContext;
import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags;
@ -22,6 +21,7 @@ public class HTTPComponent extends DDSpanContextDecorator {
public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
//Assign service name
if (super.afterSetTag(context, tag, value)) {
//Assign span type to WEB
context.setSpanType("web");
return true;

View File

@ -1,12 +1,11 @@
package com.datadoghq.trace.integration;
import java.net.MalformedURLException;
import com.datadoghq.trace.DDSpanContext;
import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags;
import java.net.MalformedURLException;
public class URLAsResourceName extends DDSpanContextDecorator {
public URLAsResourceName() {
@ -18,7 +17,6 @@ public class URLAsResourceName extends DDSpanContextDecorator {
@Override
public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
//Assign resource name
if(tag.equals(Tags.HTTP_URL.getKey())){
try {
String path = new java.net.URL(String.valueOf(value)).getPath();
context.setTag(this.getSetTag(), path);
@ -26,9 +24,6 @@ public class URLAsResourceName extends DDSpanContextDecorator {
context.setResourceName(String.valueOf(value));
}
return true;
}else{
return false;
}
}
}