API implem
This commit is contained in:
parent
dae7e563b7
commit
09c0cbb7d2
|
|
@ -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));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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,39 +17,55 @@ 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;
|
||||||
protected final String tracesEndpoint;
|
protected final String tracesEndpoint;
|
||||||
protected final String servicesEndpoint;
|
protected final String servicesEndpoint;
|
||||||
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");
|
||||||
|
|
@ -70,44 +84,44 @@ public class DDApi {
|
||||||
return -1;
|
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
|
public static void main(String[] args) throws Exception{
|
||||||
.buildSpan("hello-world")
|
|
||||||
.asChildOf(parent)
|
List<Span> array = new ArrayList<Span>();
|
||||||
.start();
|
Tracer tracer = new Tracer();
|
||||||
array.add(child);
|
|
||||||
|
Span parent = tracer
|
||||||
Thread.sleep(1000);
|
.buildSpan("hello-world")
|
||||||
|
.withServiceName("service-name")
|
||||||
child.finish();
|
.start();
|
||||||
|
array.add(parent);
|
||||||
Thread.sleep(1000);
|
|
||||||
|
parent.setBaggageItem("a-baggage", "value");
|
||||||
parent.finish();
|
|
||||||
|
Thread.sleep(1000);
|
||||||
DDSpanSerializer ddSpanSerializer = new DDSpanSerializer();
|
|
||||||
|
Span child = tracer
|
||||||
String str = ddSpanSerializer.serialize(array);
|
.buildSpan("hello-world")
|
||||||
str = "["+str+"]";
|
.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);
|
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));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue