|
1 | 1 | package com.datadoghq.trace.writer; |
2 | 2 |
|
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 | | - |
11 | 3 | import com.datadoghq.trace.DDBaseSpan; |
| 4 | +import com.datadoghq.trace.DDTracer; |
12 | 5 | import com.fasterxml.jackson.core.JsonFactory; |
13 | 6 | import com.fasterxml.jackson.core.JsonGenerator; |
14 | 7 | 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; |
15 | 16 |
|
16 | 17 | /** |
17 | 18 | * The API pointing to a DD agent |
18 | 19 | */ |
19 | 20 | public class DDApi { |
20 | 21 |
|
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; |
22 | 28 |
|
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(); |
25 | 31 |
|
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 | + } |
31 | 35 |
|
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 | + } |
36 | 52 |
|
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 | + } |
53 | 68 |
|
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 | + } |
73 | 87 |
|
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 | + } |
92 | 100 |
|
93 | 101 | } |
0 commit comments