Changed id generations + fixed serialization

This commit is contained in:
renaudboutet 2017-04-26 15:29:58 +02:00
parent 638f303d76
commit 7e38848abb
6 changed files with 45 additions and 48 deletions

View File

@ -1,14 +1,12 @@
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 String serialize(Object spans) throws Exception;
public Span deserialize(String str) throws Exception;

View File

@ -11,12 +11,12 @@ import io.opentracing.SpanContext;
public class DDSpan implements io.opentracing.Span {
private final Tracer tracer;
private final String operationName;
private Map<String, Object> tags;
private long startTime;
private long durationNano;
private final DDSpanContext context;
protected final Tracer tracer;
protected final String operationName;
protected Map<String, Object> tags;
protected long startTime;
protected long durationNano;
protected final DDSpanContext context;
DDSpan(
Tracer tracer,

View File

@ -1,9 +1,6 @@
package com.datadoghq.trace.impl;
import io.opentracing.Span;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

View File

@ -1,7 +1,5 @@
package com.datadoghq.trace.impl;
import java.util.List;
import com.datadoghq.trace.SpanSerializer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -16,25 +14,11 @@ public class DDSpanSerializer implements SpanSerializer {
return objectMapper.writeValueAsString(span);
}
public String serialize(List<Span> spans) throws JsonProcessingException {
public String serialize(Object spans) throws JsonProcessingException {
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{
Tracer tracer = new Tracer();
Span span = tracer.buildSpan("Hello!")
.withTag("port", 1234)
.withTag("bool", true)
.withTag("hello", "world")
.start();
DDSpanSerializer ddSpanSerializer = new DDSpanSerializer();
System.out.println(ddSpanSerializer.serialize(span));
}
}

View File

@ -134,6 +134,6 @@ public class Tracer implements io.opentracing.Tracer {
}
long generateNewId() {
return UUID.randomUUID().getMostSignificantBits();
return System.nanoTime();
}
}

View File

@ -7,8 +7,11 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import com.datadoghq.trace.SpanSerializer;
import com.datadoghq.trace.impl.DDSpanSerializer;
import com.datadoghq.trace.impl.DDTags;
import com.datadoghq.trace.impl.Tracer;
import io.opentracing.Span;
@ -22,16 +25,22 @@ public class DDApi {
protected final int port;
protected final String tracesEndpoint;
protected final String servicesEndpoint;
protected final SpanSerializer spanSerializer;
public DDApi(String host, int port) {
this(host,port,null);
}
public DDApi(String host, int port,Optional<SpanSerializer> serializer) {
super();
this.host = host;
this.port = port;
this.tracesEndpoint = "http://"+host+":"+port+TRACES_ENDPOINT;
this.servicesEndpoint = "http://"+host+":"+port+TRACES_SERVICES;
this.spanSerializer = serializer.orElse(new DDSpanSerializer());
}
public void sendSpans(List<Span> spans){
public void sendTraces(List<List<Span>> traces){
}
@ -48,7 +57,7 @@ public class DDApi {
httpCon.setRequestMethod("PUT");
httpCon.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
out.write("Resource content");
out.write(content);
out.close();
return httpCon.getResponseCode();
} catch (MalformedURLException e) {
@ -64,26 +73,35 @@ public class DDApi {
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);
Tracer tracer = new Tracer();
Span parent = tracer
.buildSpan("hello-world")
.withTag(DDTags.SERVICE.getKey(), "service-name")
.start();
array.add(parent);
parent.setBaggageItem("a-baggage", "value");
Thread.sleep(1000);
Span child = tracer
.buildSpan("hello-world")
.asChildOf(parent)
.start();
array.add(child);
Thread.sleep(1000);
child.finish();
Thread.sleep(1000);
parent.finish();
DDSpanSerializer ddSpanSerializer = new DDSpanSerializer();
String str = ddSpanSerializer.serialize(array);
str = "["+str+"]";