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 java.util.List;
import io.opentracing.tag.Tags;
import org.junit.Test; import org.junit.Test;
import com.datadoghq.trace.DDTracer; import com.datadoghq.trace.DDTracer;
@ -18,18 +19,22 @@ public class TracerResolverTest {
DDTracerResolver tracerResolver = new DDTracerResolver(); DDTracerResolver tracerResolver = new DDTracerResolver();
DDTracer tracer = (DDTracer) tracerResolver.resolve(); 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); assertThat(decorators.size()).isEqualTo(2);
DDSpanContextDecorator decorator = decorators.get(0); DDSpanContextDecorator decorator = decorators.get(0);
assertThat(decorator.getClass()).isEqualTo(HTTPComponent.class); assertThat(decorator.getClass()).isEqualTo(HTTPComponent.class);
HTTPComponent httpServiceDecorator = (HTTPComponent) decorator; HTTPComponent httpServiceDecorator = (HTTPComponent) decorator;
assertThat(httpServiceDecorator.getMatchingTag()).isEqualTo("component"); assertThat(httpServiceDecorator.getMatchingTag()).isEqualTo("component");
assertThat(httpServiceDecorator.getMatchingValue()).isEqualTo("hello"); assertThat(httpServiceDecorator.getMatchingValue()).isEqualTo("hello");
assertThat(httpServiceDecorator.getSetValue()).isEqualTo("world"); 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); assertThat(decorator.getClass()).isEqualTo(URLAsResourceName.class);
} }

View File

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

View File

@ -175,7 +175,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
this.tags.put(tag, value); this.tags.put(tag, value);
//Call decorators //Call decorators
for (DDSpanContextDecorator decorator : tracer.getSpanContextDecorators()) { for (DDSpanContextDecorator decorator : tracer.getSpanContextDecorators(tag)) {
decorator.afterSetTag(this, tag, value); decorator.afterSetTag(this, tag, value);
} }
//Error management //Error management

View File

@ -1,14 +1,5 @@
package com.datadoghq.trace; 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.integration.DDSpanContextDecorator;
import com.datadoghq.trace.propagation.Codec; import com.datadoghq.trace.propagation.Codec;
import com.datadoghq.trace.propagation.HTTPCodec; 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.sampling.Sampler;
import com.datadoghq.trace.writer.DDAgentWriter; import com.datadoghq.trace.writer.DDAgentWriter;
import com.datadoghq.trace.writer.Writer; import com.datadoghq.trace.writer.Writer;
import io.opentracing.Span; import io.opentracing.Span;
import io.opentracing.SpanContext; import io.opentracing.SpanContext;
import io.opentracing.propagation.Format; 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 * 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); 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 * @return the list of span context decorators
*/ */
public List<DDSpanContextDecorator> getSpanContextDecorators() { public List<DDSpanContextDecorator> getSpanContextDecorators(String tag) {
return Collections.unmodifiableList(spanContextDecorators); 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 * @param decorator The decorator in the list
*/ */
public void addDecorator(DDSpanContextDecorator decorator) { 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) { public DDSpanBuilder buildSpan(String operationName) {
return new DDSpanBuilder(operationName); return new DDSpanBuilder(operationName);
@ -238,7 +248,7 @@ public class DDTracer implements io.opentracing.Tracer {
} }
public DDTracer.DDSpanBuilder asChildOf(Span span) { public DDTracer.DDSpanBuilder asChildOf(Span span) {
return asChildOf(span==null? null : span.context()); return asChildOf(span == null ? null : span.context());
} }
public DDTracer.DDSpanBuilder asChildOf(SpanContext spanContext) { public DDTracer.DDSpanBuilder asChildOf(SpanContext spanContext) {
@ -317,6 +327,10 @@ public class DDTracer implements io.opentracing.Tracer {
} }
private String getHashKey(String tag) {
return tag;
}
private static class CodecRegistry { private static class CodecRegistry {
private final Map<Format<?>, Codec<?>> codecs = new HashMap<Format<?>, Codec<?>>(); 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.DDSpanContext;
import com.datadoghq.trace.DDTags; import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags; import io.opentracing.tag.Tags;
/** /**
@ -20,14 +19,12 @@ public class DBComponent extends DDSpanContextDecorator {
@Override @Override
public boolean afterSetTag(DDSpanContext context, String tag, Object value) { public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
//Assign service name //Assign service name
if(super.afterSetTag(context, tag, value)){ if (super.afterSetTag(context, tag, value)) {
//Assign span type to DB //Assign span type to DB
context.setSpanType("db"); context.setSpanType("db");
//Assign resource name //Assign resource name
if(tag.equals(Tags.DB_STATEMENT.getKey())){
context.setResourceName(String.valueOf(value)); context.setResourceName(String.valueOf(value));
}
return true; return true;
} }
return false; return false;

View File

@ -15,13 +15,13 @@ public abstract class DDSpanContextDecorator {
private String setValue; private String setValue;
public boolean afterSetTag(DDSpanContext context, String tag, Object value){ 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 targetTag = getSetTag() == null ? tag : getSetTag();
String targetValue = getSetValue()==null?String.valueOf(value):getSetTag(); String targetValue = getSetValue() == null ? String.valueOf(value) : getSetTag();
context.setTag(targetTag, targetValue); context.setTag(targetTag, targetValue);
return true; return true;
}else{ } else {
return false; return false;
} }
} }

View File

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

View File

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