Serialization of spans
MODIFY
src/main/java/com/datadoghq/trace/SpanSerializer.java
src/main/java/com/datadoghq/trace/SpanSerializer.java
package com.datadoghq.trace;
+import java.util.List;
+
import io.opentracing.Span;
public interface SpanSerializer {
public String serialize(Span t) throws Exception;
+ public String serialize(List<Span> t) throws Exception;
+
public Span deserialize(String str) throws Exception;
}

MODIFY
src/main/java/com/datadoghq/trace/impl/DDSpan.java
src/main/java/com/datadoghq/trace/impl/DDSpan.java
package com.datadoghq.trace.impl;
-import io.opentracing.SpanContext;
-
import java.util.Map;
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 {
@@ -44,35 +47,35 @@ public class DDSpan implements io.opentracing.Span {
}
- public io.opentracing.Span setTag(String s, String s1) {
+ public Span setTag(String s, String s1) {
return null;
}
- public io.opentracing.Span setTag(String s, boolean b) {
+ public Span setTag(String s, boolean b) {
return null;
}
- public io.opentracing.Span setTag(String s, Number number) {
+ public Span setTag(String s, Number number) {
return null;
}
- public io.opentracing.Span log(Map<String, ?> map) {
+ public Span log(Map<String, ?> map) {
return null;
}
- public io.opentracing.Span log(long l, Map<String, ?> map) {
+ public Span log(long l, Map<String, ?> map) {
return null;
}
- public io.opentracing.Span log(String s) {
+ public Span log(String s) {
return null;
}
- public io.opentracing.Span log(long l, String s) {
+ public Span log(long l, String s) {
return null;
}
- public io.opentracing.Span setBaggageItem(String s, String s1) {
+ public Span setBaggageItem(String s, String s1) {
return null;
}
@@ -80,32 +83,70 @@ public class DDSpan implements io.opentracing.Span {
return null;
}
- public io.opentracing.Span setOperationName(String s) {
+ public Span setOperationName(String s) {
return null;
}
- public io.opentracing.Span log(String s, Object o) {
+ public Span log(String s, Object o) {
return null;
}
- public io.opentracing.Span log(long l, String s, Object o) {
+ public Span log(long l, String s, Object o) {
return null;
}
+ //Getters and JSON serialisation instructions
+
+ @JsonGetter(value="name")
public String getOperationName() {
return operationName;
}
+ @JsonGetter(value="meta")
public Map<String, Object> getTags() {
return this.tags;
}
+ @JsonGetter(value="start")
public long getStartTime() {
- return startTime;
+ return startTime * 1000000;
+ }
+
+ @JsonGetter(value="duration")
+ public long getDurationInNS(){
+ return durationMilliseconds * 1000000;
+ }
+
+ public String getService(){
+ 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 DDSpanContext getContext(){
- return context;
+ public int getError(){
+ return context.getErrorFlag()?1:0;
}
}

MODIFY
src/main/java/com/datadoghq/trace/impl/DDSpanContext.java
src/main/java/com/datadoghq/trace/impl/DDSpanContext.java
@@ -72,7 +72,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return resourceName;
}
- public boolean isErrorFlag() {
+ public boolean getErrorFlag() {
return errorFlag;
}
@@ -84,7 +84,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return spanType;
}
- public boolean isSampled() {
+ public boolean getSampled() {
return sampled;
}

MODIFY
src/main/java/com/datadoghq/trace/impl/DDSpanSerializer.java
src/main/java/com/datadoghq/trace/impl/DDSpanSerializer.java
package com.datadoghq.trace.impl;
-import java.io.IOException;
+import java.util.List;
import com.datadoghq.trace.SpanSerializer;
-import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
+import io.opentracing.Span;
+
public class DDSpanSerializer implements SpanSerializer {
protected final ObjectMapper objectMapper = new ObjectMapper();
- public String serialize(io.opentracing.Span t) throws JsonProcessingException {
- return objectMapper.writeValueAsString(t);
+ public String serialize(Span span) throws JsonProcessingException {
+ return objectMapper.writeValueAsString(span);
+ }
+
+ public String serialize(List<Span> spans) throws JsonProcessingException {
+ return objectMapper.writeValueAsString(spans);
}
- public io.opentracing.Span deserialize(String str) throws JsonParseException, JsonMappingException, IOException {
- return objectMapper.readValue(str, DDSpan.class);
+ 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{
Tracer tracer = new Tracer();
- io.opentracing.Span span = tracer.buildSpan("Hello!")
+ Span span = tracer.buildSpan("Hello!")
.withTag("port", 1234)
.withTag("bool", true)
.withTag("hello", "world")

MODIFY
src/main/java/com/datadoghq/trace/impl/Tracer.java
src/main/java/com/datadoghq/trace/impl/Tracer.java
@@ -22,10 +22,10 @@ public class Tracer implements io.opentracing.Tracer {
return null;
}
- class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
+ public class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
private final String operationName;
- private Map<String, Object> tags = new HashMap();
+ private Map<String, Object> tags = new HashMap<String,Object>();
private Long timestamp;
private SpanContext parent;
@@ -33,16 +33,16 @@ public class Tracer implements io.opentracing.Tracer {
this.operationName = operationName;
}
- public io.opentracing.Tracer.SpanBuilder asChildOf(SpanContext spanContext) {
+ public Tracer.SpanBuilder asChildOf(SpanContext spanContext) {
this.parent = spanContext;
return this;
}
- public io.opentracing.Tracer.SpanBuilder asChildOf(Span span) {
+ public Tracer.SpanBuilder asChildOf(Span span) {
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)) {
// @todo: implements the notion of referenceType, currently only link a span to a parent one
@@ -53,24 +53,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);
}
- public io.opentracing.Tracer.SpanBuilder withTag(String tag, String string) {
+ public Tracer.SpanBuilder withTag(String tag, String 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);
}
- private io.opentracing.Tracer.SpanBuilder withTag(String tag, Object value) {
+ private Tracer.SpanBuilder withTag(String tag, Object value) {
this.tags.put(tag, value);
return this;
}
- public io.opentracing.Tracer.SpanBuilder withStartTimestamp(long timestamp) {
+ public Tracer.SpanBuilder withStartTimestamp(long timestamp) {
this.timestamp = timestamp;
return this;
}
@@ -80,7 +80,6 @@ public class Tracer implements io.opentracing.Tracer {
// build the context
DDSpanContext context = buildTheSpanContext();
-
return new DDSpan(
Tracer.this,
this.operationName,

MODIFY
src/main/java/com/datadoghq/trace/writer/impl/DDApi.java
src/main/java/com/datadoghq/trace/writer/impl/DDApi.java
@@ -5,8 +5,12 @@ import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.ArrayList;
import java.util.List;
+import com.datadoghq.trace.impl.DDSpanSerializer;
+import com.datadoghq.trace.impl.Tracer;
+
import io.opentracing.Span;
public class DDApi {
@@ -36,9 +40,10 @@ public class DDApi {
}
private int callPUT(String endpoint,String content){
+ HttpURLConnection httpCon = null;
try {
URL url = new URL(tracesEndpoint);
- HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
+ httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
httpCon.setRequestProperty("Content-Type", "application/json");
@@ -56,4 +61,35 @@ public class DDApi {
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);
+
+ }
}

Serialization of spans