-
Notifications
You must be signed in to change notification settings - Fork 311
adding meta support #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 81 additions & 73 deletions
154
dd-trace/src/main/java/com/datadoghq/trace/writer/DDApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<DDBaseSpan<?>>> 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<List<DDBaseSpan<?>>> 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return httpCon; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like formatting changed a lot. Was this intentional? Perhaps we should have a discussion on applying and enforcing standardized formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mhhhhh thought it was intentional / agreed... if we're changing the code-style, let's agree on that plus update all the rest. If it's a mistake, just undo the code style refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather we do all the reformatting in one PR, otherwise it will pollute PR's going forward. I'm ok getting this merged as is though. Merging now.