Serialization of spans
This commit is contained in:
parent
299cc57fc1
commit
0399cb03e3
|
@ -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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package com.datadoghq.trace.impl;
|
package com.datadoghq.trace.impl;
|
||||||
|
|
||||||
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 {
|
||||||
|
|
||||||
|
@ -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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public io.opentracing.Span setTag(String s, boolean b) {
|
public Span setTag(String s, boolean b) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public io.opentracing.Span setTag(String s, Number number) {
|
public Span setTag(String s, Number number) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 s, String s1) {
|
public Span setBaggageItem(String s, String s1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,32 +83,70 @@ public class DDSpan implements io.opentracing.Span {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public io.opentracing.Span setOperationName(String s) {
|
public Span setOperationName(String s) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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 int getError(){
|
||||||
|
return context.getErrorFlag()?1:0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,7 +72,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
|
||||||
return resourceName;
|
return resourceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isErrorFlag() {
|
public boolean getErrorFlag() {
|
||||||
return errorFlag;
|
return errorFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
|
||||||
return spanType;
|
return spanType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSampled() {
|
public boolean getSampled() {
|
||||||
return sampled;
|
return sampled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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")
|
||||||
|
|
|
@ -22,10 +22,10 @@ public class Tracer implements io.opentracing.Tracer {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
|
@ -33,16 +33,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
|
||||||
|
@ -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);
|
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;
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,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,
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue