diff --git a/src/main/java/com/datadoghq/trace/impl/DDSpanSerializer.java b/src/main/java/com/datadoghq/trace/impl/DDSpanSerializer.java index 0eae68ac6f..84de8b772e 100644 --- a/src/main/java/com/datadoghq/trace/impl/DDSpanSerializer.java +++ b/src/main/java/com/datadoghq/trace/impl/DDSpanSerializer.java @@ -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 array = new ArrayList(); + 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> traces = new ArrayList>(); + traces.add(array); + + DDSpanSerializer serializer = new DDSpanSerializer(); + + System.out.println(serializer.objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(traces)); + } } diff --git a/src/main/java/com/datadoghq/trace/impl/Tracer.java b/src/main/java/com/datadoghq/trace/impl/Tracer.java index 8304fb13fd..11c7357e28 100644 --- a/src/main/java/com/datadoghq/trace/impl/Tracer.java +++ b/src/main/java/com/datadoghq/trace/impl/Tracer.java @@ -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 { diff --git a/src/main/java/com/datadoghq/trace/writer/impl/DDApi.java b/src/main/java/com/datadoghq/trace/writer/impl/DDApi.java index d72274e165..eab792fd72 100644 --- a/src/main/java/com/datadoghq/trace/writer/impl/DDApi.java +++ b/src/main/java/com/datadoghq/trace/writer/impl/DDApi.java @@ -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 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> traces){ - + + public boolean sendTraces(List> 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 services){ - + + public boolean sendServices(List 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 array = new ArrayList(); - 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 array = new ArrayList(); + 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> traces = new ArrayList>(); + 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)); + } }