Fix refactored decorators + ErrorFlag + DBStatement decorator + DDapi is streaming JSON (optim)

This commit is contained in:
renaudboutet 2017-05-29 19:17:44 +02:00
parent e7204baa5a
commit 26342e48d9
12 changed files with 103 additions and 41 deletions

View File

@ -1,8 +1,6 @@
# Decorators are used to add extra information to span # Decorators are used to add extra information to span
# Could be DBServiceDecorator, MapperDecorator or HTTPServiceDecorator # Could be DBServiceDecorator, MapperDecorator or HTTPServiceDecorator
decorators: decorators:
- type: HTTPComponent
matchingValue: java-web-servlet
- type: HTTPComponent - type: HTTPComponent
matchingValue: java-okhttp matchingValue: java-okhttp
setValue: http-client setValue: http-client
@ -16,3 +14,5 @@ decorators:
matchingValue: java-aws-sdk matchingValue: java-aws-sdk
setValue: aws-client setValue: aws-client
- type: URLAsResourceName - type: URLAsResourceName
- type: DBStatementAsResourceName
- type: ErrorFlag

View File

@ -0,0 +1,24 @@
# Service name used if none is provided in the app
defaultServiceName: java-app
# The writer to use.
# Could be: LoggingWritter or DDAgentWriter (default)
writer:
# LoggingWriter: Spans are logged using the application configuration
# DDAgentWriter: Spans are forwarding to a Datadog Agent
# - Param 'host': the hostname where the DD Agent running (default: localhost)
# - Param 'port': the port to reach the DD Agent (default: 8126)
type: DDAgentWriter
host: localhost
port: 8126
# The sampler to use.
# Could be: AllSampler (default) or RateSampler
sampler:
# AllSampler: all spans are reported to the writer
# RateSample: only a portion of spans are reported to the writer
# - Param 'rate': the portion of spans to keep
type: AllSampler
# Enable custom tracing (annotations)
enableCustomTracing: true

View File

@ -36,6 +36,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
* True indicates that the span reports an error * True indicates that the span reports an error
*/ */
private boolean errorFlag; private boolean errorFlag;
/** /**
* The type of the span. If null, the Datadog Agent will report as a custom * The type of the span. If null, the Datadog Agent will report as a custom
*/ */
@ -124,6 +125,9 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return errorFlag; return errorFlag;
} }
public void setErrorFlag(boolean errorFlag) {
this.errorFlag = errorFlag;
}
public String getSpanType() { public String getSpanType() {
return spanType; return spanType;
@ -175,8 +179,9 @@ public class DDSpanContext implements io.opentracing.SpanContext {
this.tags.put(tag, value); this.tags.put(tag, value);
//Call decorators //Call decorators
if (tracer.getSpanContextDecorators(tag) != null) { List<DDSpanContextDecorator> decorators = tracer.getSpanContextDecorators(tag);
for (DDSpanContextDecorator decorator : tracer.getSpanContextDecorators(tag)) { if (decorators != null) {
for (DDSpanContextDecorator decorator : decorators) {
decorator.afterSetTag(this, tag, value); decorator.afterSetTag(this, tag, value);
} }
} }

View File

@ -78,10 +78,7 @@ public class DDTracer implements io.opentracing.Tracer {
* @return the list of span context decorators * @return the list of span context decorators
*/ */
public List<DDSpanContextDecorator> getSpanContextDecorators(String tag) { public List<DDSpanContextDecorator> getSpanContextDecorators(String tag) {
return spanContextDecorators.get(tag);
List<DDSpanContextDecorator> decorators = spanContextDecorators.get(tag);
return decorators;
} }
/** /**

View File

@ -22,9 +22,6 @@ public class DBComponent extends DDSpanContextDecorator {
if (super.afterSetTag(context, tag, value)) { if (super.afterSetTag(context, tag, value)) {
//Assign span type to DB //Assign span type to DB
context.setSpanType("db"); context.setSpanType("db");
//Assign resource name
context.setResourceName(String.valueOf(value));
return true; return true;
} }
return false; return false;

View File

@ -0,0 +1,14 @@
package com.datadoghq.trace.integration;
import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags;
public class DBStatementAsResourceName extends DDSpanContextDecorator {
public DBStatementAsResourceName() {
super();
this.setMatchingTag(Tags.DB_STATEMENT.getKey());
this.setSetTag(DDTags.RESOURCE_NAME);
}
}

View File

@ -1,6 +1,7 @@
package com.datadoghq.trace.integration; package com.datadoghq.trace.integration;
import com.datadoghq.trace.DDSpanContext; import com.datadoghq.trace.DDSpanContext;
import com.datadoghq.trace.DDTags;
/** /**
* Span decorators are called when new tags are written and proceed to various remappings and enrichments * Span decorators are called when new tags are written and proceed to various remappings and enrichments
@ -18,8 +19,17 @@ public abstract class DDSpanContextDecorator {
public boolean afterSetTag(DDSpanContext context, String tag, Object value) { public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
if ((this.getMatchingValue() == null || value.equals(this.getMatchingValue()))) { if ((this.getMatchingValue() == null || value.equals(this.getMatchingValue()))) {
String targetTag = getSetTag() == null ? tag : getSetTag(); String targetTag = getSetTag() == null ? tag : getSetTag();
String targetValue = getSetValue() == null ? String.valueOf(value) : getSetTag(); String targetValue = getSetValue() == null ? String.valueOf(value) : getSetValue();
if (targetTag.equals(DDTags.SERVICE_NAME)) {
context.setServiceName(targetValue);
} else if (targetTag.equals(DDTags.RESOURCE_NAME)) {
context.setResourceName(targetValue);
} else if (targetTag.equals(DDTags.SPAN_TYPE)) {
context.setSpanType(targetValue);
} else {
context.setTag(targetTag, targetValue); context.setTag(targetTag, targetValue);
}
return true; return true;
} else { } else {
return false; return false;

View File

@ -0,0 +1,24 @@
package com.datadoghq.trace.integration;
import com.datadoghq.trace.DDSpanContext;
import io.opentracing.tag.Tags;
public class ErrorFlag extends DDSpanContextDecorator {
public ErrorFlag() {
super();
this.setMatchingTag(Tags.DB_STATEMENT.getKey());
}
@Override
public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
//Assign resource name
try{
context.setErrorFlag(Boolean.parseBoolean(String.valueOf(value)));
}catch(Throwable t){
//DO NOTHING
}
return true;
}
}

View File

@ -21,7 +21,6 @@ public class HTTPComponent extends DDSpanContextDecorator {
public boolean afterSetTag(DDSpanContext context, String tag, Object value) { public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
//Assign service name //Assign service name
if (super.afterSetTag(context, tag, value)) { if (super.afterSetTag(context, tag, value)) {
//Assign span type to WEB //Assign span type to WEB
context.setSpanType("web"); context.setSpanType("web");
return true; return true;

View File

@ -19,7 +19,7 @@ public class URLAsResourceName extends DDSpanContextDecorator {
//Assign resource name //Assign resource name
try { try {
String path = new java.net.URL(String.valueOf(value)).getPath(); String path = new java.net.URL(String.valueOf(value)).getPath();
context.setTag(this.getSetTag(), path); context.setResourceName(path);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
context.setResourceName(String.valueOf(value)); context.setResourceName(String.valueOf(value));
} }

View File

@ -3,6 +3,7 @@ package com.datadoghq.trace.resolver;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
@ -10,6 +11,7 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
/** /**
* Tracer configuration * Tracer configuration
*/ */
@JsonIgnoreProperties(ignoreUnknown = true)
public class TracerConfig { public class TracerConfig {
private String defaultServiceName; private String defaultServiceName;
private Map<String,Object> writer; private Map<String,Object> writer;

View File

@ -8,6 +8,10 @@ import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.opentracing.Span; import io.opentracing.Span;
/** /**
@ -23,20 +27,12 @@ public class DDApi {
private final String tracesEndpoint; private final String tracesEndpoint;
// private final String servicesEndpoint; // private final String servicesEndpoint;
/** private final ObjectMapper objectMapper = new ObjectMapper();
* The spans serializer: can be replaced. By default, it serialize in JSON. private final JsonFactory jsonFactory = objectMapper.getFactory();
*/
private final DDSpanSerializer spanSerializer;
public DDApi(String host, int port) { public DDApi(String host, int port) {
this(host, port, new DDSpanSerializer());
}
public DDApi(String host, int port, DDSpanSerializer spanSerializer) {
super();
this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT; this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT;
// this.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT; // this.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT;
this.spanSerializer = spanSerializer;
} }
/** /**
@ -46,15 +42,7 @@ public class DDApi {
* @return the staus code returned * @return the staus code returned
*/ */
public boolean sendTraces(List<List<Span>> traces) { public boolean sendTraces(List<List<Span>> traces) {
String payload = null; int status = callPUT(tracesEndpoint, traces);
try {
payload = spanSerializer.serialize(traces);
} catch (Exception e) {
logger.error("Error during serialization of " + traces.size() + " traces.", e);
return false;
}
int status = callPUT(tracesEndpoint, payload);
if (status == 200) { if (status == 200) {
logger.debug("Succesfully sent {} traces to the DD agent.", traces.size()); logger.debug("Succesfully sent {} traces to the DD agent.", traces.size());
return true; return true;
@ -71,7 +59,7 @@ public class DDApi {
* @param content * @param content
* @return the status code * @return the status code
*/ */
private int callPUT(String endpoint, String content) { private int callPUT(String endpoint, Object content) {
HttpURLConnection httpCon = null; HttpURLConnection httpCon = null;
try { try {
URL url = new URL(endpoint); URL url = new URL(endpoint);
@ -86,8 +74,10 @@ public class DDApi {
try { try {
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream()); OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
out.write(content); JsonGenerator jsonGen = jsonFactory.createGenerator(out);
out.close(); objectMapper.writeValue(jsonGen, content);
jsonGen.flush();
jsonGen.close();
int responseCode = httpCon.getResponseCode(); int responseCode = httpCon.getResponseCode();
if (responseCode == 200) { if (responseCode == 200) {
logger.debug("Sent the payload to the DD agent."); logger.debug("Sent the payload to the DD agent.");