diff --git a/dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java b/dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java index 933c2f45fb1..605f667e1ab 100644 --- a/dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java +++ b/dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java @@ -22,6 +22,9 @@ */ public class DDTracer implements io.opentracing.Tracer { + public final static String CURRENT_VERSION = DDTracer.class.getPackage().getImplementationVersion(); + public final static String JAVA_VERSION = System.getProperty("java.version", "unknown"); + /** * Writer is an charge of reporting traces and spans to the desired endpoint */ diff --git a/dd-trace/src/main/java/com/datadoghq/trace/writer/DDApi.java b/dd-trace/src/main/java/com/datadoghq/trace/writer/DDApi.java index 21df592c8fb..164eeba3a6f 100644 --- a/dd-trace/src/main/java/com/datadoghq/trace/writer/DDApi.java +++ b/dd-trace/src/main/java/com/datadoghq/trace/writer/DDApi.java @@ -1,93 +1,101 @@ package com.datadoghq.trace.writer; -import java.io.OutputStreamWriter; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.datadoghq.trace.DDBaseSpan; +import com.datadoghq.trace.DDTracer; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.List; /** * The API pointing to a DD agent */ public class DDApi { - private static final Logger logger = LoggerFactory.getLogger(DDApi.class.getName()); + private static final Logger logger = LoggerFactory.getLogger(DDApi.class.getName()); + + + private static final String TRACES_ENDPOINT = "/v0.3/traces"; + + private final String tracesEndpoint; - private static final String TRACES_ENDPOINT = "/v0.3/traces"; -// private static final String SERVICES_ENDPOINT = "/v0.3/services"; + private final ObjectMapper objectMapper = new ObjectMapper(); + private final JsonFactory jsonFactory = objectMapper.getFactory(); - private final String tracesEndpoint; -// private final String servicesEndpoint; - - private final ObjectMapper objectMapper = new ObjectMapper(); - private final JsonFactory jsonFactory = objectMapper.getFactory(); + public DDApi(String host, int port) { + this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT; + } - public DDApi(String host, int port) { - this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT; -// this.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT; - } + /** + * Send traces to the DD agent + * + * @param traces the traces to be sent + * @return the staus code returned + */ + public boolean sendTraces(List>> traces) { + int status = callPUT(traces); + if (status == 200) { + logger.debug("Succesfully sent {} traces to the DD agent.", traces.size()); + return true; + } else { + logger.warn("Error while sending {} traces to the DD agent. Status: {}", traces.size(), status); + return false; + } + } - /** - * Send traces to the DD agent - * - * @param traces the traces to be sent - * @return the staus code returned - */ - public boolean sendTraces(List>> traces) { - int status = callPUT(tracesEndpoint, traces); - if (status == 200) { - logger.debug("Succesfully sent {} traces to the DD agent.", traces.size()); - return true; - } else { - logger.warn("Error while sending {} traces to the DD agent. Status: {}", traces.size(), status); - return false; - } - } + /** + * PUT to an endpoint the provided JSON content + * + * @param endpoint + * @param content + * @return the status code + */ + private int callPUT(Object content) { + HttpURLConnection httpCon = null; + try { + httpCon = getHttpURLConnection(); + } catch (Exception e) { + logger.warn("Error thrown before PUT call to the DD agent.", e); + return -1; + } - /** - * PUT to an endpoint the provided JSON content - * - * @param endpoint - * @param content - * @return the status code - */ - private int callPUT(String endpoint, Object content) { - HttpURLConnection httpCon = null; - try { - URL url = new URL(endpoint); - httpCon = (HttpURLConnection) url.openConnection(); - httpCon.setDoOutput(true); - httpCon.setRequestMethod("PUT"); - httpCon.setRequestProperty("Content-Type", "application/json"); - } catch (Exception e) { - logger.warn("Error thrown before PUT call to the DD agent.", e); - return -1; - } + try { + OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream()); + JsonGenerator jsonGen = jsonFactory.createGenerator(out); + objectMapper.writeValue(jsonGen, content); + jsonGen.flush(); + jsonGen.close(); + int responseCode = httpCon.getResponseCode(); + if (responseCode == 200) { + logger.debug("Sent the payload to the DD agent."); + } else { + logger.warn("Could not send the payload to the DD agent. Status: {} ResponseMessage: {}", httpCon.getResponseCode(), httpCon.getResponseMessage()); + } + return responseCode; + } catch (Exception e) { + logger.warn("Could not send the payload to the DD agent.", e); + return -1; + } + } - try { - OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream()); - JsonGenerator jsonGen = jsonFactory.createGenerator(out); - objectMapper.writeValue(jsonGen, content); - jsonGen.flush(); - jsonGen.close(); - int responseCode = httpCon.getResponseCode(); - if (responseCode == 200) { - logger.debug("Sent the payload to the DD agent."); - } else { - logger.warn("Could not send the payload to the DD agent. Status: {} ResponseMessage: {}", httpCon.getResponseCode(), httpCon.getResponseMessage()); - } - return responseCode; - } catch (Exception e) { - logger.warn("Could not send the payload to the DD agent.", e); - return -1; - } - } + private HttpURLConnection getHttpURLConnection() throws IOException { + HttpURLConnection httpCon; + URL url = new URL(tracesEndpoint); + httpCon = (HttpURLConnection) url.openConnection(); + httpCon.setDoOutput(true); + httpCon.setRequestMethod("PUT"); + httpCon.setRequestProperty("Content-Type", "application/json"); + httpCon.setRequestProperty("Datadog-Meta-Lang", "java"); + httpCon.setRequestProperty("Datadog-Meta-Lang-Version", DDTracer.JAVA_VERSION); + httpCon.setRequestProperty("Datadog-Meta-Tracer-Version", DDTracer.CURRENT_VERSION); + return httpCon; + } }