Skip to content

Commit b16ca52

Browse files
authored
Merge pull request #30 from DataDog/gpolaert/meta
adding meta support
2 parents 1903019 + 78aaed7 commit b16ca52

File tree

2 files changed

+84
-73
lines changed

2 files changed

+84
-73
lines changed

dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
public class DDTracer implements io.opentracing.Tracer {
2424

25+
public final static String CURRENT_VERSION = DDTracer.class.getPackage().getImplementationVersion();
26+
public final static String JAVA_VERSION = System.getProperty("java.version", "unknown");
27+
2528
/**
2629
* Writer is an charge of reporting traces and spans to the desired endpoint
2730
*/
Lines changed: 81 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,101 @@
11
package com.datadoghq.trace.writer;
22

3-
import java.io.OutputStreamWriter;
4-
import java.net.HttpURLConnection;
5-
import java.net.URL;
6-
import java.util.List;
7-
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
10-
113
import com.datadoghq.trace.DDBaseSpan;
4+
import com.datadoghq.trace.DDTracer;
125
import com.fasterxml.jackson.core.JsonFactory;
136
import com.fasterxml.jackson.core.JsonGenerator;
147
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.io.IOException;
12+
import java.io.OutputStreamWriter;
13+
import java.net.HttpURLConnection;
14+
import java.net.URL;
15+
import java.util.List;
1516

1617
/**
1718
* The API pointing to a DD agent
1819
*/
1920
public class DDApi {
2021

21-
private static final Logger logger = LoggerFactory.getLogger(DDApi.class.getName());
22+
private static final Logger logger = LoggerFactory.getLogger(DDApi.class.getName());
23+
24+
25+
private static final String TRACES_ENDPOINT = "/v0.3/traces";
26+
27+
private final String tracesEndpoint;
2228

23-
private static final String TRACES_ENDPOINT = "/v0.3/traces";
24-
// private static final String SERVICES_ENDPOINT = "/v0.3/services";
29+
private final ObjectMapper objectMapper = new ObjectMapper();
30+
private final JsonFactory jsonFactory = objectMapper.getFactory();
2531

26-
private final String tracesEndpoint;
27-
// private final String servicesEndpoint;
28-
29-
private final ObjectMapper objectMapper = new ObjectMapper();
30-
private final JsonFactory jsonFactory = objectMapper.getFactory();
32+
public DDApi(String host, int port) {
33+
this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT;
34+
}
3135

32-
public DDApi(String host, int port) {
33-
this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT;
34-
// this.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT;
35-
}
36+
/**
37+
* Send traces to the DD agent
38+
*
39+
* @param traces the traces to be sent
40+
* @return the staus code returned
41+
*/
42+
public boolean sendTraces(List<List<DDBaseSpan<?>>> traces) {
43+
int status = callPUT(traces);
44+
if (status == 200) {
45+
logger.debug("Succesfully sent {} traces to the DD agent.", traces.size());
46+
return true;
47+
} else {
48+
logger.warn("Error while sending {} traces to the DD agent. Status: {}", traces.size(), status);
49+
return false;
50+
}
51+
}
3652

37-
/**
38-
* Send traces to the DD agent
39-
*
40-
* @param traces the traces to be sent
41-
* @return the staus code returned
42-
*/
43-
public boolean sendTraces(List<List<DDBaseSpan<?>>> traces) {
44-
int status = callPUT(tracesEndpoint, traces);
45-
if (status == 200) {
46-
logger.debug("Succesfully sent {} traces to the DD agent.", traces.size());
47-
return true;
48-
} else {
49-
logger.warn("Error while sending {} traces to the DD agent. Status: {}", traces.size(), status);
50-
return false;
51-
}
52-
}
53+
/**
54+
* PUT to an endpoint the provided JSON content
55+
*
56+
* @param endpoint
57+
* @param content
58+
* @return the status code
59+
*/
60+
private int callPUT(Object content) {
61+
HttpURLConnection httpCon = null;
62+
try {
63+
httpCon = getHttpURLConnection();
64+
} catch (Exception e) {
65+
logger.warn("Error thrown before PUT call to the DD agent.", e);
66+
return -1;
67+
}
5368

54-
/**
55-
* PUT to an endpoint the provided JSON content
56-
*
57-
* @param endpoint
58-
* @param content
59-
* @return the status code
60-
*/
61-
private int callPUT(String endpoint, Object content) {
62-
HttpURLConnection httpCon = null;
63-
try {
64-
URL url = new URL(endpoint);
65-
httpCon = (HttpURLConnection) url.openConnection();
66-
httpCon.setDoOutput(true);
67-
httpCon.setRequestMethod("PUT");
68-
httpCon.setRequestProperty("Content-Type", "application/json");
69-
} catch (Exception e) {
70-
logger.warn("Error thrown before PUT call to the DD agent.", e);
71-
return -1;
72-
}
69+
try {
70+
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
71+
JsonGenerator jsonGen = jsonFactory.createGenerator(out);
72+
objectMapper.writeValue(jsonGen, content);
73+
jsonGen.flush();
74+
jsonGen.close();
75+
int responseCode = httpCon.getResponseCode();
76+
if (responseCode == 200) {
77+
logger.debug("Sent the payload to the DD agent.");
78+
} else {
79+
logger.warn("Could not send the payload to the DD agent. Status: {} ResponseMessage: {}", httpCon.getResponseCode(), httpCon.getResponseMessage());
80+
}
81+
return responseCode;
82+
} catch (Exception e) {
83+
logger.warn("Could not send the payload to the DD agent.", e);
84+
return -1;
85+
}
86+
}
7387

74-
try {
75-
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
76-
JsonGenerator jsonGen = jsonFactory.createGenerator(out);
77-
objectMapper.writeValue(jsonGen, content);
78-
jsonGen.flush();
79-
jsonGen.close();
80-
int responseCode = httpCon.getResponseCode();
81-
if (responseCode == 200) {
82-
logger.debug("Sent the payload to the DD agent.");
83-
} else {
84-
logger.warn("Could not send the payload to the DD agent. Status: {} ResponseMessage: {}", httpCon.getResponseCode(), httpCon.getResponseMessage());
85-
}
86-
return responseCode;
87-
} catch (Exception e) {
88-
logger.warn("Could not send the payload to the DD agent.", e);
89-
return -1;
90-
}
91-
}
88+
private HttpURLConnection getHttpURLConnection() throws IOException {
89+
HttpURLConnection httpCon;
90+
URL url = new URL(tracesEndpoint);
91+
httpCon = (HttpURLConnection) url.openConnection();
92+
httpCon.setDoOutput(true);
93+
httpCon.setRequestMethod("PUT");
94+
httpCon.setRequestProperty("Content-Type", "application/json");
95+
httpCon.setRequestProperty("Datadog-Meta-Lang", "java");
96+
httpCon.setRequestProperty("Datadog-Meta-Lang-Version", DDTracer.JAVA_VERSION);
97+
httpCon.setRequestProperty("Datadog-Meta-Tracer-Version", DDTracer.CURRENT_VERSION);
98+
return httpCon;
99+
}
92100

93101
}

0 commit comments

Comments
 (0)