8000 adding meta support by gpolaert · Pull Request #30 · DataDog/dd-trace-java · GitHub
[go: up one dir, main page]

Skip to content

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 1 commit into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
154 changes: 81 additions & 73 deletions dd-trace/src/main/java/com/datadoghq/trace/writer/DDApi.java
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());
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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.



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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return httpCon;
}

}
0