Merge remote-tracking branch 'origin/dev' into dev

# Conflicts:
#	src/main/java/com/datadoghq/trace/impl/DDSpan.java
This commit is contained in:
Guillaume Polaert 2017-04-26 15:16:30 +02:00
commit ee9f91219b
7 changed files with 123 additions and 41 deletions

View File

@ -1,11 +1,15 @@
package com.datadoghq.trace; package com.datadoghq.trace;
import java.util.List;
import io.opentracing.Span; import io.opentracing.Span;
public interface SpanSerializer { public interface SpanSerializer {
public String serialize(Span t) throws Exception; public String serialize(Span t) throws Exception;
public String serialize(List<Span> t) throws Exception;
public Span deserialize(String str) throws Exception; public Span deserialize(String str) throws Exception;
} }

View File

@ -5,6 +5,11 @@ import io.opentracing.SpanContext;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonGetter;
import io.opentracing.Span;
import io.opentracing.SpanContext;
public class DDSpan implements io.opentracing.Span { public class DDSpan implements io.opentracing.Span {
@ -48,36 +53,36 @@ public class DDSpan implements io.opentracing.Span {
return this.setTag(tag, value); return this.setTag(tag, value);
} }
public io.opentracing.Span setTag(String tag, boolean value) { public Span setTag(String tag, boolean value) {
return this.setTag(tag, value); return this.setTag(tag, value);
} }
public io.opentracing.Span setTag(String tag, Number value) { public Span setTag(String tag, Number value) {
return this.setTag(tag, (Object) value); return this.setTag(tag, (Object) value);
} }
private io.opentracing.Span setTag(String tag, Object value) { private Span setTag(String tag, Object value) {
this.tags.put(tag, value); this.tags.put(tag, value);
return this; return this;
} }
public io.opentracing.Span log(Map<String, ?> map) { public Span log(Map<String, ?> map) {
return null; return null;
} }
public io.opentracing.Span log(long l, Map<String, ?> map) { public Span log(long l, Map<String, ?> map) {
return null; return null;
} }
public io.opentracing.Span log(String s) { public Span log(String s) {
return null; return null;
} }
public io.opentracing.Span log(long l, String s) { public Span log(long l, String s) {
return null; return null;
} }
public io.opentracing.Span setBaggageItem(String key, String value) { public Span setBaggageItem(String key, String value) {
this.context.setBaggageItem(key, value); this.context.setBaggageItem(key, value);
return this; return this;
} }
@ -86,36 +91,70 @@ public class DDSpan implements io.opentracing.Span {
return this.context.getBaggageItem(key); return this.context.getBaggageItem(key);
} }
public io.opentracing.Span setOperationName(String operationName) { public Span setOperationName(String operationName) {
this.operationName = operationName; this.operationName = operationName;
return this; return this;
} }
public io.opentracing.Span log(String s, Object o) { public Span log(String s, Object o) {
return null; return null;
} }
public io.opentracing.Span log(long l, String s, Object o) { public Span log(long l, String s, Object o) {
return null; return null;
} }
//Getters and JSON serialisation instructions
@JsonGetter(value="name")
public String getOperationName() { public String getOperationName() {
return operationName; return operationName;
} }
@JsonGetter(value="meta")
public Map<String, Object> getTags() { public Map<String, Object> getTags() {
return this.tags; return this.tags;
} }
@JsonGetter(value="start")
public long getStartTime() { public long getStartTime() {
return startTime; return startTime * 1000000;
} }
public DDSpanContext getContext() { @JsonGetter(value="duration")
return context; public long getDurationNano(){
return durationNano;
} }
public DDSpanContext DDContext() { public String getService(){
return this.context; return context.getServiceName();
}
@JsonGetter(value="trace_id")
public long getTraceId(){
return context.getTraceId();
}
@JsonGetter(value="span_id")
public long getSpanId(){
return context.getSpanId();
}
@JsonGetter(value="parent_id")
public long getParentId(){
return context.getParentId();
}
@JsonGetter(value="resource")
public String getResourceName(){
return context.getResourceName()==null?getOperationName():context.getResourceName();
}
public String getType(){
return context.getSpanType();
}
public int getError(){
return context.getErrorFlag()?1:0;
} }
} }

View File

@ -89,8 +89,8 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return resourceName; return resourceName;
} }
public boolean isErrorFlag() { public boolean getErrorFlag() {
return this.errorFlag; return errorFlag;
} }
public Map<String, Object> getMetrics() { public Map<String, Object> getMetrics() {
@ -101,7 +101,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return spanType; return spanType;
} }
public boolean isSampled() { public boolean getSampled() {
return sampled; return sampled;
} }

View File

@ -1,29 +1,33 @@
package com.datadoghq.trace.impl; package com.datadoghq.trace.impl;
import java.io.IOException; import java.util.List;
import com.datadoghq.trace.SpanSerializer; import com.datadoghq.trace.SpanSerializer;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.opentracing.Span;
public class DDSpanSerializer implements SpanSerializer { public class DDSpanSerializer implements SpanSerializer {
protected final ObjectMapper objectMapper = new ObjectMapper(); protected final ObjectMapper objectMapper = new ObjectMapper();
public String serialize(io.opentracing.Span t) throws JsonProcessingException { public String serialize(Span span) throws JsonProcessingException {
return objectMapper.writeValueAsString(t); return objectMapper.writeValueAsString(span);
} }
public io.opentracing.Span deserialize(String str) throws JsonParseException, JsonMappingException, IOException { public String serialize(List<Span> spans) throws JsonProcessingException {
return objectMapper.readValue(str, DDSpan.class); return objectMapper.writeValueAsString(spans);
}
public io.opentracing.Span deserialize(String str) throws Exception {
throw new Exception("Deserialisation of spans is not implemented yet");
} }
public static void main(String[] args) throws Exception{ public static void main(String[] args) throws Exception{
Tracer tracer = new Tracer(); Tracer tracer = new Tracer();
io.opentracing.Span span = tracer.buildSpan("Hello!") Span span = tracer.buildSpan("Hello!")
.withTag("port", 1234) .withTag("port", 1234)
.withTag("bool", true) .withTag("bool", true)
.withTag("hello", "world") .withTag("hello", "world")

View File

@ -27,7 +27,7 @@ public class Tracer implements io.opentracing.Tracer {
public class SpanBuilder implements io.opentracing.Tracer.SpanBuilder { public class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
private final String operationName; private final String operationName;
private Map<String, Object> tags = new HashMap<>(); private Map<String, Object> tags = new HashMap<String,Object>();
private Long timestamp; private Long timestamp;
private SpanContext parent; private SpanContext parent;
private String serviceName; private String serviceName;
@ -39,16 +39,16 @@ public class Tracer implements io.opentracing.Tracer {
this.operationName = operationName; this.operationName = operationName;
} }
public io.opentracing.Tracer.SpanBuilder asChildOf(SpanContext spanContext) { public Tracer.SpanBuilder asChildOf(SpanContext spanContext) {
this.parent = spanContext; this.parent = spanContext;
return this; return this;
} }
public io.opentracing.Tracer.SpanBuilder asChildOf(Span span) { public Tracer.SpanBuilder asChildOf(Span span) {
return asChildOf(span.context()); return asChildOf(span.context());
} }
public io.opentracing.Tracer.SpanBuilder addReference(String referenceType, SpanContext spanContext) { public Tracer.SpanBuilder addReference(String referenceType, SpanContext spanContext) {
if (References.CHILD_OF.equals(referenceType) || References.FOLLOWS_FROM.equals(referenceType)) { if (References.CHILD_OF.equals(referenceType) || References.FOLLOWS_FROM.equals(referenceType)) {
// @todo: implements the notion of referenceType, currently only link a span to a parent one // @todo: implements the notion of referenceType, currently only link a span to a parent one
@ -59,24 +59,24 @@ public class Tracer implements io.opentracing.Tracer {
} }
} }
public io.opentracing.Tracer.SpanBuilder withTag(String tag, Number number) { public Tracer.SpanBuilder withTag(String tag, Number number) {
return withTag(tag, (Object) number); return withTag(tag, (Object) number);
} }
public io.opentracing.Tracer.SpanBuilder withTag(String tag, String string) { public Tracer.SpanBuilder withTag(String tag, String string) {
return withTag(tag, (Object) string); return withTag(tag, (Object) string);
} }
public io.opentracing.Tracer.SpanBuilder withTag(String tag, boolean bool) { public Tracer.SpanBuilder withTag(String tag, boolean bool) {
return withTag(tag, (Object) bool); return withTag(tag, (Object) bool);
} }
private io.opentracing.Tracer.SpanBuilder withTag(String tag, Object value) { private Tracer.SpanBuilder withTag(String tag, Object value) {
this.tags.put(tag, value); this.tags.put(tag, value);
return this; return this;
} }
public io.opentracing.Tracer.SpanBuilder withStartTimestamp(long timestamp) { public Tracer.SpanBuilder withStartTimestamp(long timestamp) {
this.timestamp = timestamp; this.timestamp = timestamp;
return this; return this;
} }
@ -109,7 +109,6 @@ public class Tracer implements io.opentracing.Tracer {
// build the context // build the context
DDSpanContext context = buildTheSpanContext(); DDSpanContext context = buildTheSpanContext();
return new DDSpan( return new DDSpan(
Tracer.this, Tracer.this,
this.operationName, this.operationName,

View File

@ -5,8 +5,12 @@ import java.io.OutputStreamWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.datadoghq.trace.impl.DDSpanSerializer;
import com.datadoghq.trace.impl.Tracer;
import io.opentracing.Span; import io.opentracing.Span;
public class DDApi { public class DDApi {
@ -36,9 +40,10 @@ public class DDApi {
} }
private int callPUT(String endpoint,String content){ private int callPUT(String endpoint,String content){
HttpURLConnection httpCon = null;
try { try {
URL url = new URL(tracesEndpoint); URL url = new URL(tracesEndpoint);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true); httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT"); httpCon.setRequestMethod("PUT");
httpCon.setRequestProperty("Content-Type", "application/json"); httpCon.setRequestProperty("Content-Type", "application/json");
@ -56,4 +61,35 @@ public class DDApi {
return -1; return -1;
} }
} }
public static void main(String[] args) throws Exception{
Tracer tracer = new Tracer();
List<Span> array = new ArrayList<Span>();
Span span = tracer.buildSpan("Hello!")
// .withTag("port", 1234)
// .withTag("bool", true)
.withTag("hello", "world")
.start();
array.add(span);
Span span2 = tracer.buildSpan("Hello2!")
// .withTag("port", 1234)
// .withTag("bool", true)
.withTag("hello", "world")
.start();
array.add(span2);
DDSpanSerializer ddSpanSerializer = new DDSpanSerializer();
String str = ddSpanSerializer.serialize(array);
str = "["+str+"]";
DDApi api = new DDApi(DDAgentWriter.DEFAULT_HOSTNAME, DDAgentWriter.DEFAULT_PORT);
int status = api.callPUT(api.tracesEndpoint, str);
System.out.println("Status: "+status);
}
} }

View File

@ -187,8 +187,8 @@ public class DDSpanBuilderTest {
assertThat(span.getOperationName()).isEqualTo(expectedName); assertThat(span.getOperationName()).isEqualTo(expectedName);
assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue); assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue);
assertThat(span.DDContext().getServiceName()).isEqualTo(expectedServiceName); assertThat(((DDSpanContext) span.context()).getServiceName()).isEqualTo(expectedServiceName);
assertThat(span.DDContext().getResourceName()).isNotEqualTo(expectedResourceName); assertThat(((DDSpan) span.context()).getResourceName()).isNotEqualTo(expectedResourceName);
} }