Adding async examples
MODIFY
dd-trace-examples/async-tracing/async-tracing.gradle
dd-trace-examples/async-tracing/async-tracing.gradle
@@ -5,4 +5,7 @@ description = 'async-tracing'
dependencies {
compile project(':dd-trace')
compile group: 'io.opentracing.contrib', name: 'opentracing-spanmanager', version: '0.0.5'
+ compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
+ compile group:'io.reactivex.rxjava2', name:'rxjava', version:'2.1.1'
+
}

ADD
dd-trace-examples/async-tracing/src/main/java/com/datadoghq/trace/AsyncExample.java
+package com.datadoghq.trace;
+
+import com.datadoghq.trace.sampling.AllSampler;
+import com.datadoghq.trace.writer.DDAgentWriter;
+import com.datadoghq.trace.writer.DDApi;
+import io.opentracing.ActiveSpan;
+import io.opentracing.Tracer;
+import io.opentracing.util.GlobalTracer;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+public class AsyncExample {
+
+ private static ExecutorService ex = Executors.newFixedThreadPool(1);
+
+ public static Integer bar() throws Exception {
+
+ try (ActiveSpan __ = GlobalTracer.get()
+ .buildSpan("bar")
+ .startActive()) {
+ System.out.println("bar");
+ Thread.sleep(1000);
+ }
+
+ return 42;
+ }
+
+ public static Future<Integer> foo() throws Exception {
+
+ try (ActiveSpan span = GlobalTracer.get()
+ .buildSpan("foo")
+ .startActive()) {
+
+ System.out.println("foo");
+ Thread.sleep(500);
+
+ final ActiveSpan.Continuation cont = span.capture();
+
+ Future<Integer> future = ex.submit(() -> {
+ try (ActiveSpan __ = cont.activate()) {
+
+ return bar();
+ }
+ });
+
+
+
+ return future;
+
+
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+
+ DDAgentWriter writer = new DDAgentWriter(new DDApi("localhost", 8126));
+ Tracer tracer = new DDTracer("dd-trace-test-app", writer, new AllSampler());
+// Tracer tracer = new DDTracer(new LoggingWriter(), new AllSampler());
+ GlobalTracer.register(tracer);
+
+ System.out.printf("%d%n", foo().get());
+
+ writer.close();
+ ex.shutdownNow();
+
+ }
+
+}

ADD
dd-trace-examples/async-tracing/src/main/java/com/datadoghq/trace/AsyncExampleReactive.java
+package com.datadoghq.trace;
+
+import com.datadoghq.trace.sampling.AllSampler;
+import com.datadoghq.trace.writer.DDAgentWriter;
+import com.datadoghq.trace.writer.DDApi;
+import io.opentracing.ActiveSpan;
+import io.opentracing.Tracer;
+import io.opentracing.util.GlobalTracer;
+import io.reactivex.Observable;
+
+
+public class AsyncExampleReactive {
+
+
+ public static Integer bar() throws Exception {
+ try (ActiveSpan __ = GlobalTracer.get()
+ .buildSpan("bar")
+ .startActive()) {
+ System.out.println("bar");
+ Thread.sleep(1000);
+ }
+
+ return 42;
+ }
+
+ public static Observable<Integer> foo() throws Exception {
+ try (ActiveSpan span = GlobalTracer.get()
+ .buildSpan("foo")
+ .startActive()) {
+
+ System.out.println("foo");
+ Thread.sleep(500);
+
+ final ActiveSpan.Continuation cont = span.capture();
+ return Observable.fromCallable(() -> {
+ try (ActiveSpan __ = cont.activate()) {
+ return bar();
+ }
+ });
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ DDAgentWriter writer = new DDAgentWriter(new DDApi("localhost", 8126));
+ Tracer tracer = new DDTracer("dd-trace-test-app", writer, new AllSampler());
+ GlobalTracer.register(tracer);
+
+ foo().subscribe(System.out::println);
+
+ writer.close();
+ }
+
+}
+

ADD
dd-trace-examples/async-tracing/src/main/resources/logback.xml
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+
+ <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+ <layout class="ch.qos.logback.classic.PatternLayout">
+ <Pattern>
+ %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
+ </Pattern>
+ </layout>
+ </appender>
+
+ <root level="debug">
+ <appender-ref ref="console"/>
+ </root>
+
+</configuration>

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java
dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java
@@ -304,6 +304,10 @@ public abstract class DDBaseSpan<S extends BaseSpan> implements BaseSpan<S> {
@Override
public String toString() {
- return context.toString();
+ return new StringBuilder()
+ .append(context.toString())
+ .append(", duration_ns=")
+ .append(durationNano)
+ .toString();
}
}

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
@@ -200,19 +200,20 @@ public class DDSpanContext implements io.opentracing.SpanContext {
@Override
public String toString() {
- return "Span [ "
- + traceId
- + " ] [ "
- + spanId
- + " | "
- + parentId
- + " ] [ "
- + getServiceName()
- + " | "
- + getOperationName()
- + " | "
- + getResourceName()
- + " ]";
+ return new StringBuilder()
+ .append("Span [ t_id=")
+ .append(traceId)
+ .append(", s_id=")
+ .append(spanId)
+ .append(", p_id=")
+ .append(parentId)
+ .append("] trace=")
+ .append(getServiceName())
+ .append("/")
+ .append(getOperationName())
+ .append("/")
+ .append(getResourceName())
+ .toString();
}
public void setOperationName(String operationName) {

Adding async examples