Merge pull request #105 from DataDog/gpolaert/msgpack

Send traces as msgpack instead of json
This commit is contained in:
Tyler Benson 2017-08-18 11:10:48 -06:00 committed by GitHub
commit 797332caeb
3 changed files with 23 additions and 27 deletions

View File

@ -16,7 +16,6 @@ whitelistedInstructionClasses += whitelistedBranchClasses += [
'com.datadoghq.trace.DDTags',
'com.datadoghq.trace.DDTraceInfo',
'com.datadoghq.trace.util.Clock',
]
dependencies {
@ -29,6 +28,7 @@ dependencies {
compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.8.8'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'com.google.auto.service', name: 'auto-service', version: '1.0-rc3'
compile group: 'org.msgpack', name: 'jackson-dataformat-msgpack', version: '0.8.13'
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

View File

@ -3,16 +3,15 @@ package com.datadoghq.trace.writer;
import com.datadoghq.trace.DDBaseSpan;
import com.datadoghq.trace.DDTraceInfo;
import com.datadoghq.trace.Service;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.msgpack.jackson.dataformat.MessagePackFactory;
/** The API pointing to a DD agent */
@Slf4j
@ -24,8 +23,7 @@ public class DDApi {
private final String tracesEndpoint;
private final String servicesEndpoint;
private final ObjectMapper objectMapper = new ObjectMapper();
private final JsonFactory jsonFactory = objectMapper.getFactory();
private final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
public DDApi(final String host, final int port) {
this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT;
@ -72,7 +70,6 @@ public class DDApi {
/**
* PUT to an endpoint the provided JSON content
*
* @param endpoint
* @param content
* @return the status code
*/
@ -86,11 +83,10 @@ public class DDApi {
}
try {
final OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
final JsonGenerator jsonGen = jsonFactory.createGenerator(out);
objectMapper.writeValue(jsonGen, content);
jsonGen.flush();
jsonGen.close();
final OutputStream out = httpCon.getOutputStream();
objectMapper.writeValue(out, content);
out.flush();
out.close();
final int responseCode = httpCon.getResponseCode();
if (responseCode == 200) {
log.debug("Sent the payload to the DD agent.");
@ -113,7 +109,7 @@ public class DDApi {
httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
httpCon.setRequestProperty("Content-Type", "application/json");
httpCon.setRequestProperty("Content-Type", "application/msgpack");
httpCon.setRequestProperty("Datadog-Meta-Lang", "java");
httpCon.setRequestProperty("Datadog-Meta-Lang-Version", DDTraceInfo.JAVA_VERSION);
httpCon.setRequestProperty("Datadog-Meta-Lang-Interpreter", DDTraceInfo.JAVA_VM_NAME);

View File

@ -4,6 +4,7 @@ import com.datadoghq.trace.Service
import com.datadoghq.trace.SpanFactory
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import org.msgpack.jackson.dataformat.MessagePackFactory
import ratpack.http.Headers
import ratpack.http.MediaType
import spock.lang.Specification
@ -11,10 +12,9 @@ import spock.lang.Specification
import java.util.concurrent.atomic.AtomicReference
import static ratpack.groovy.test.embed.GroovyEmbeddedApp.ratpack
import static ratpack.http.MediaType.APPLICATION_JSON
class DDApiTest extends Specification {
static mapper = new ObjectMapper()
static mapper = new ObjectMapper(new MessagePackFactory())
def "sending an empty list of traces returns no errors"() {
setup:
@ -52,18 +52,18 @@ class DDApiTest extends Specification {
agent.close()
}
def "content is sent as JSON"() {
def "content is sent as MSGPACK"() {
setup:
def requestContentType = new AtomicReference<MediaType>()
def requestHeaders = new AtomicReference<Headers>()
def requestBody = new AtomicReference<String>()
def requestBody = new AtomicReference<byte[]>()
def agent = ratpack {
handlers {
put("v0.3/traces") {
requestContentType.set(request.contentType)
requestHeaders.set(request.headers)
request.body.then {
requestBody.set(it.text)
requestBody.set(it.bytes)
response.send()
}
}
@ -73,7 +73,7 @@ class DDApiTest extends Specification {
expect:
client.sendTraces(traces)
requestContentType.get().type == APPLICATION_JSON
requestContentType.get().type == "application/msgpack"
requestHeaders.get().get("Datadog-Meta-Lang") == "java"
requestHeaders.get().get("Datadog-Meta-Lang-Version") == System.getProperty("java.version", "unknown")
requestHeaders.get().get("Datadog-Meta-Tracer-Version") == "unknown"
@ -151,18 +151,18 @@ class DDApiTest extends Specification {
agent.close()
}
def "services content is sent as JSON"() {
def "services content is sent as MSGPACK"() {
setup:
def requestContentType = new AtomicReference<MediaType>()
def requestHeaders = new AtomicReference<Headers>()
def requestBody = new AtomicReference<String>()
def requestBody = new AtomicReference<byte[]>()
def agent = ratpack {
handlers {
put("v0.3/services") {
requestContentType.set(request.contentType)
requestHeaders.set(request.headers)
request.body.then {
requestBody.set(it.text)
requestBody.set(it.bytes)
response.send()
}
}
@ -172,7 +172,7 @@ class DDApiTest extends Specification {
expect:
client.sendServices(services)
requestContentType.get().type == APPLICATION_JSON
requestContentType.get().type == "application/msgpack"
requestHeaders.get().get("Datadog-Meta-Lang") == "java"
requestHeaders.get().get("Datadog-Meta-Lang-Version") == System.getProperty("java.version", "unknown")
requestHeaders.get().get("Datadog-Meta-Tracer-Version") == "unknown"
@ -191,11 +191,11 @@ class DDApiTest extends Specification {
]
}
static List<TreeMap<String, Object>> convertList(String json) {
return mapper.readValue(json, new TypeReference<List<TreeMap<String, Object>>>() {})
static List<TreeMap<String, Object>> convertList(byte[] bytes) {
return mapper.readValue(bytes, new TypeReference<List<TreeMap<String, Object>>>() {})
}
static TreeMap<String, Object> convertMap(String json) {
return mapper.readValue(json, new TypeReference<TreeMap<String, Object>>() {})
static TreeMap<String, Object> convertMap(byte[] bytes) {
return mapper.readValue(bytes, new TypeReference<TreeMap<String, Object>>() {})
}
}