Fix refactored decorators + ErrorFlag + DBStatement decorator + DDapi is streaming JSON (optim)
This commit is contained in:
parent
e7204baa5a
commit
26342e48d9
|
@ -1,8 +1,6 @@
|
|||
# Decorators are used to add extra information to span
|
||||
# Could be DBServiceDecorator, MapperDecorator or HTTPServiceDecorator
|
||||
decorators:
|
||||
- type: HTTPComponent
|
||||
matchingValue: java-web-servlet
|
||||
- type: HTTPComponent
|
||||
matchingValue: java-okhttp
|
||||
setValue: http-client
|
||||
|
@ -16,3 +14,5 @@ decorators:
|
|||
matchingValue: java-aws-sdk
|
||||
setValue: aws-client
|
||||
- type: URLAsResourceName
|
||||
- type: DBStatementAsResourceName
|
||||
- type: ErrorFlag
|
|
@ -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
|
|
@ -36,6 +36,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
|
|||
* True indicates that the span reports an error
|
||||
*/
|
||||
private boolean errorFlag;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
public void setErrorFlag(boolean errorFlag) {
|
||||
this.errorFlag = errorFlag;
|
||||
}
|
||||
|
||||
public String getSpanType() {
|
||||
return spanType;
|
||||
|
@ -175,8 +179,9 @@ public class DDSpanContext implements io.opentracing.SpanContext {
|
|||
this.tags.put(tag, value);
|
||||
|
||||
//Call decorators
|
||||
if (tracer.getSpanContextDecorators(tag) != null) {
|
||||
for (DDSpanContextDecorator decorator : tracer.getSpanContextDecorators(tag)) {
|
||||
List<DDSpanContextDecorator> decorators = tracer.getSpanContextDecorators(tag);
|
||||
if (decorators != null) {
|
||||
for (DDSpanContextDecorator decorator : decorators) {
|
||||
decorator.afterSetTag(this, tag, value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -78,10 +78,7 @@ public class DDTracer implements io.opentracing.Tracer {
|
|||
* @return the list of span context decorators
|
||||
*/
|
||||
public List<DDSpanContextDecorator> getSpanContextDecorators(String tag) {
|
||||
|
||||
List<DDSpanContextDecorator> decorators = spanContextDecorators.get(tag);
|
||||
|
||||
return decorators;
|
||||
return spanContextDecorators.get(tag);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,9 +22,6 @@ public class DBComponent extends DDSpanContextDecorator {
|
|||
if (super.afterSetTag(context, tag, value)) {
|
||||
//Assign span type to DB
|
||||
context.setSpanType("db");
|
||||
|
||||
//Assign resource name
|
||||
context.setResourceName(String.valueOf(value));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.datadoghq.trace.integration;
|
||||
|
||||
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
|
||||
|
@ -18,8 +19,17 @@ public abstract class DDSpanContextDecorator {
|
|||
public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
|
||||
if ((this.getMatchingValue() == null || value.equals(this.getMatchingValue()))) {
|
||||
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);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -21,7 +21,6 @@ public class HTTPComponent extends DDSpanContextDecorator {
|
|||
public boolean afterSetTag(DDSpanContext context, String tag, Object value) {
|
||||
//Assign service name
|
||||
if (super.afterSetTag(context, tag, value)) {
|
||||
|
||||
//Assign span type to WEB
|
||||
context.setSpanType("web");
|
||||
return true;
|
||||
|
|
|
@ -19,7 +19,7 @@ public class URLAsResourceName extends DDSpanContextDecorator {
|
|||
//Assign resource name
|
||||
try {
|
||||
String path = new java.net.URL(String.valueOf(value)).getPath();
|
||||
context.setTag(this.getSetTag(), path);
|
||||
context.setResourceName(path);
|
||||
} catch (MalformedURLException e) {
|
||||
context.setResourceName(String.valueOf(value));
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.datadoghq.trace.resolver;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
|
@ -10,6 +11,7 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
|||
/**
|
||||
* Tracer configuration
|
||||
*/
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class TracerConfig {
|
||||
private String defaultServiceName;
|
||||
private Map<String,Object> writer;
|
||||
|
|
|
@ -8,6 +8,10 @@ import java.util.List;
|
|||
import org.slf4j.Logger;
|
||||
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;
|
||||
|
||||
/**
|
||||
|
@ -23,20 +27,12 @@ public class DDApi {
|
|||
private final String tracesEndpoint;
|
||||
// private final String servicesEndpoint;
|
||||
|
||||
/**
|
||||
* The spans serializer: can be replaced. By default, it serialize in JSON.
|
||||
*/
|
||||
private final DDSpanSerializer spanSerializer;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final JsonFactory jsonFactory = objectMapper.getFactory();
|
||||
|
||||
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.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT;
|
||||
this.spanSerializer = spanSerializer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,15 +42,7 @@ public class DDApi {
|
|||
* @return the staus code returned
|
||||
*/
|
||||
public boolean sendTraces(List<List<Span>> traces) {
|
||||
String payload = null;
|
||||
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);
|
||||
int status = callPUT(tracesEndpoint, traces);
|
||||
if (status == 200) {
|
||||
logger.debug("Succesfully sent {} traces to the DD agent.", traces.size());
|
||||
return true;
|
||||
|
@ -71,7 +59,7 @@ public class DDApi {
|
|||
* @param content
|
||||
* @return the status code
|
||||
*/
|
||||
private int callPUT(String endpoint, String content) {
|
||||
private int callPUT(String endpoint, Object content) {
|
||||
HttpURLConnection httpCon = null;
|
||||
try {
|
||||
URL url = new URL(endpoint);
|
||||
|
@ -86,8 +74,10 @@ public class DDApi {
|
|||
|
||||
try {
|
||||
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
|
||||
out.write(content);
|
||||
out.close();
|
||||
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.");
|
||||
|
|
Loading…
Reference in New Issue