adding a close method to flush traces

This commit is contained in:
Guillaume Polaert 2017-05-16 12:11:58 +02:00
parent c5184070f4
commit 0c06397094
1 changed files with 31 additions and 27 deletions

View File

@ -35,7 +35,7 @@ public class DDTracer implements io.opentracing.Tracer {
* Sampler defines the sampling policy in order to reduce the number of traces for instance * Sampler defines the sampling policy in order to reduce the number of traces for instance
*/ */
private final Sampler sampler; private final Sampler sampler;
/** /**
* Default service name if none provided on the trace or span * Default service name if none provided on the trace or span
*/ */
@ -49,7 +49,7 @@ public class DDTracer implements io.opentracing.Tracer {
private final static Logger logger = LoggerFactory.getLogger(DDTracer.class); private final static Logger logger = LoggerFactory.getLogger(DDTracer.class);
private final CodecRegistry registry; private final CodecRegistry registry;
public static final String UNASSIGNED_DEFAULT_SERVICE_NAME = "unnamed-java-app"; public static final String UNASSIGNED_DEFAULT_SERVICE_NAME = "unnamed-java-app";
public static final Writer UNASSIGNED_WRITER = new DDAgentWriter(); public static final Writer UNASSIGNED_WRITER = new DDAgentWriter();
public static final Sampler UNASSIGNED_SAMPLER = new AllSampler(); public static final Sampler UNASSIGNED_SAMPLER = new AllSampler();
@ -60,17 +60,17 @@ public class DDTracer implements io.opentracing.Tracer {
public DDTracer() { public DDTracer() {
this(UNASSIGNED_WRITER); this(UNASSIGNED_WRITER);
} }
public DDTracer(Writer writer) { public DDTracer(Writer writer) {
this(writer, new AllSampler()); this(writer, new AllSampler());
} }
public DDTracer(Writer writer, Sampler sampler) { public DDTracer(Writer writer, Sampler sampler) {
this(UNASSIGNED_DEFAULT_SERVICE_NAME,writer,sampler); this(UNASSIGNED_DEFAULT_SERVICE_NAME, writer, sampler);
} }
public DDTracer(String defaultServiceName,Writer writer, Sampler sampler) { public DDTracer(String defaultServiceName, Writer writer, Sampler sampler) {
this.defaultServiceName = defaultServiceName; this.defaultServiceName = defaultServiceName;
this.writer = writer; this.writer = writer;
this.writer.start(); this.writer.start();
this.sampler = sampler; this.sampler = sampler;
@ -84,19 +84,19 @@ 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() { public List<DDSpanContextDecorator> getSpanContextDecorators() {
return Collections.unmodifiableList(spanContextDecorators); return Collections.unmodifiableList(spanContextDecorators);
} }
/** /**
* Add a new decorator in the list ({@link DDSpanContextDecorator}) * Add a new decorator in the list ({@link DDSpanContextDecorator})
* *
* @param decorator The decorator in the list * @param decorator The decorator in the list
*/ */
public void addDecorator(DDSpanContextDecorator decorator){ public void addDecorator(DDSpanContextDecorator decorator) {
spanContextDecorators.add(decorator); spanContextDecorators.add(decorator);
} }
public DDSpanBuilder buildSpan(String operationName) { public DDSpanBuilder buildSpan(String operationName) {
return new DDSpanBuilder(operationName); return new DDSpanBuilder(operationName);
} }
@ -117,7 +117,7 @@ public class DDTracer implements io.opentracing.Tracer {
if (codec == null) { if (codec == null) {
logger.warn("Unsupported format for propagation - {}", format.getClass().getName()); logger.warn("Unsupported format for propagation - {}", format.getClass().getName());
} else { } else {
return codec.extract(carrier); return codec.extract(carrier);
} }
return null; return null;
} }
@ -139,6 +139,10 @@ public class DDTracer implements io.opentracing.Tracer {
} }
} }
public void close() {
writer.close();
}
/** /**
* Spans are built using this builder * Spans are built using this builder
*/ */
@ -181,15 +185,15 @@ public class DDTracer implements io.opentracing.Tracer {
} }
public DDTracer.DDSpanBuilder withTag(String tag, String string) { public DDTracer.DDSpanBuilder withTag(String tag, String string) {
if(tag.equals(DDTags.SERVICE_NAME)){ if (tag.equals(DDTags.SERVICE_NAME)) {
return withServiceName(string); return withServiceName(string);
}else if(tag.equals(DDTags.RESOURCE_NAME)){ } else if (tag.equals(DDTags.RESOURCE_NAME)) {
return withResourceName(string); return withResourceName(string);
}else if(tag.equals(DDTags.SPAN_TYPE)){ } else if (tag.equals(DDTags.SPAN_TYPE)) {
return withSpanType(string); return withSpanType(string);
}else{ } else {
return withTag(tag, (Object) string); return withTag(tag, (Object) string);
} }
} }
public DDTracer.DDSpanBuilder withTag(String tag, boolean bool) { public DDTracer.DDSpanBuilder withTag(String tag, boolean bool) {
@ -249,7 +253,7 @@ public class DDTracer implements io.opentracing.Tracer {
// Private methods // Private methods
private DDTracer.DDSpanBuilder withTag(String tag, Object value) { private DDTracer.DDSpanBuilder withTag(String tag, Object value) {
if (this.tags.isEmpty()){ if (this.tags.isEmpty()) {
this.tags = new HashMap<String, Object>(); this.tags = new HashMap<String, Object>();
} }
this.tags.put(tag, value); this.tags.put(tag, value);
@ -281,10 +285,10 @@ public class DDTracer implements io.opentracing.Tracer {
String serviceName = this.serviceName; String serviceName = this.serviceName;
if (serviceName == null) { if (serviceName == null) {
if(p != null){ if (p != null && p.getServiceName() != null) {
serviceName = p.getServiceName(); serviceName = p.getServiceName();
}else{ } else {
serviceName = defaultServiceName; serviceName = defaultServiceName;
} }
} }