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;
import java.util.ArrayList;
import java.util.List;
import com.datadoghq.trace.SpanSerializer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -19,6 +22,45 @@ public class DDSpanSerializer implements SpanSerializer {
}
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;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import io.opentracing.References;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.propagation.Format;
import io.opentracing.tag.Tags;
import java.util.*;
public class Tracer implements io.opentracing.Tracer {

View File

@ -7,11 +7,9 @@ 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;
@ -19,39 +17,55 @@ import io.opentracing.Span;
public class DDApi {
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 int port;
protected final String tracesEndpoint;
protected final String servicesEndpoint;
protected final SpanSerializer spanSerializer;
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();
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());
this.servicesEndpoint = "http://"+host+":"+port+SERVICES_ENDPOINT;
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){
HttpURLConnection httpCon = null;
try {
URL url = new URL(tracesEndpoint);
URL url = new URL(endpoint);
httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
@ -70,44 +84,44 @@ public class DDApi {
return -1;
}
}
public static void main(String[] args) throws Exception{
List<Span> array = new ArrayList<Span>();
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+"]";
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);
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));
}
}