API implem

This commit is contained in:
renaudboutet 2017-04-26 16:19:12 +02:00
parent dae7e563b7
commit 09c0cbb7d2
3 changed files with 116 additions and 58 deletions

View File

@ -1,5 +1,8 @@
package com.datadoghq.trace.impl; package com.datadoghq.trace.impl;
import java.util.ArrayList;
import java.util.List;
import com.datadoghq.trace.SpanSerializer; import com.datadoghq.trace.SpanSerializer;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -19,6 +22,45 @@ public class DDSpanSerializer implements SpanSerializer {
} }
public io.opentracing.Span deserialize(String str) throws Exception { public io.opentracing.Span deserialize(String str) throws Exception {
throw new Exception("Deserialisation of spans is not implemented yet"); throw new UnsupportedOperationException("Deserialisation of spans is not implemented yet");
}
public static void main(String[] args) throws Exception{
List<Span> array = new ArrayList<Span>();
Tracer tracer = new Tracer();
Span parent = tracer
.buildSpan("hello-world")
.withServiceName("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();
List<List<Span>> traces = new ArrayList<List<Span>>();
traces.add(array);
DDSpanSerializer serializer = new DDSpanSerializer();
System.out.println(serializer.objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(traces));
} }
} }

View File

@ -1,12 +1,14 @@
package com.datadoghq.trace.impl; package com.datadoghq.trace.impl;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import io.opentracing.References; import io.opentracing.References;
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 io.opentracing.tag.Tags;
import java.util.*;
public class Tracer implements io.opentracing.Tracer { public class Tracer implements io.opentracing.Tracer {

View File

@ -7,11 +7,9 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.datadoghq.trace.SpanSerializer; import com.datadoghq.trace.SpanSerializer;
import com.datadoghq.trace.impl.DDSpanSerializer; import com.datadoghq.trace.impl.DDSpanSerializer;
import com.datadoghq.trace.impl.DDTags;
import com.datadoghq.trace.impl.Tracer; import com.datadoghq.trace.impl.Tracer;
import io.opentracing.Span; import io.opentracing.Span;
@ -19,7 +17,7 @@ import io.opentracing.Span;
public class DDApi { public class DDApi {
protected static final String TRACES_ENDPOINT = "/v0.3/traces"; protected static final String TRACES_ENDPOINT = "/v0.3/traces";
protected static final String TRACES_SERVICES = "/v0.3/services"; protected static final String SERVICES_ENDPOINT = "/v0.3/services";
protected final String host; protected final String host;
protected final int port; protected final int port;
@ -28,30 +26,46 @@ public class DDApi {
protected final SpanSerializer spanSerializer; protected final SpanSerializer spanSerializer;
public DDApi(String host, int port) { public DDApi(String host, int port) {
this(host,port,null); this(host,port,new DDSpanSerializer());
} }
public DDApi(String host, int port,Optional<SpanSerializer> serializer) { public DDApi(String host, int port,SpanSerializer spanSerializer) {
super(); super();
this.host = host; this.host = host;
this.port = port; this.port = port;
this.tracesEndpoint = "http://"+host+":"+port+TRACES_ENDPOINT; this.tracesEndpoint = "http://"+host+":"+port+TRACES_ENDPOINT;
this.servicesEndpoint = "http://"+host+":"+port+TRACES_SERVICES; this.servicesEndpoint = "http://"+host+":"+port+SERVICES_ENDPOINT;
this.spanSerializer = serializer.orElse(new DDSpanSerializer()); this.spanSerializer = spanSerializer;
} }
public void sendTraces(List<List<Span>> traces){ public boolean sendTraces(List<List<Span>> traces){
try {
String payload = spanSerializer.serialize(traces);
int status = callPUT(tracesEndpoint,payload);
if(status == 200){
return true;
}else{
//FIXME log status here
return false;
}
} catch (Exception e) {
//FIXME proper exceptino
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
} }
public void sendServices(List<String> services){ public boolean sendServices(List<String> services){
return false;
} }
private int callPUT(String endpoint,String content){ private int callPUT(String endpoint,String content){
HttpURLConnection httpCon = null; HttpURLConnection httpCon = null;
try { try {
URL url = new URL(tracesEndpoint); URL url = new URL(endpoint);
httpCon = (HttpURLConnection) url.openConnection(); httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true); httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT"); httpCon.setRequestMethod("PUT");
@ -71,14 +85,14 @@ public class DDApi {
} }
} }
public static void main(String[] args) throws Exception{ public static void main(String[] args) throws Exception{
List<Span> array = new ArrayList<Span>(); List<Span> array = new ArrayList<Span>();
Tracer tracer = new Tracer(); Tracer tracer = new Tracer();
Span parent = tracer Span parent = tracer
.buildSpan("hello-world") .buildSpan("hello-world")
.withTag(DDTags.SERVICE.getKey(), "service-name") .withServiceName("service-name")
.start(); .start();
array.add(parent); array.add(parent);
@ -100,14 +114,14 @@ public static void main(String[] args) throws Exception{
parent.finish(); parent.finish();
DDSpanSerializer ddSpanSerializer = new DDSpanSerializer(); List<List<Span>> traces = new ArrayList<List<Span>>();
traces.add(array);
String str = ddSpanSerializer.serialize(array);
str = "["+str+"]";
DDApi api = new DDApi(DDAgentWriter.DEFAULT_HOSTNAME, DDAgentWriter.DEFAULT_PORT); DDApi api = new DDApi(DDAgentWriter.DEFAULT_HOSTNAME, DDAgentWriter.DEFAULT_PORT);
int status = api.callPUT(api.tracesEndpoint, str);
System.out.println("Status: "+status); String service = "{\"service_name\": {\"app\": \"service-name\",\"app_type\": \"web\"}}";
System.out.println("Pushed service: "+api.callPUT(api.servicesEndpoint, service));
System.out.println("Pushed traces: "+api.sendTraces(traces));
} }
} }