Adding async examples
This commit is contained in:
parent
c61da7f5fc
commit
acaf9e5116
|
@ -3,6 +3,9 @@ apply from: "${rootDir}/gradle/jacoco.gradle"
|
|||
|
||||
description = 'async-tracing'
|
||||
dependencies {
|
||||
compile project(':dd-trace')
|
||||
compile group: 'io.opentracing.contrib', name: 'opentracing-spanmanager', version: '0.0.5'
|
||||
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'
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
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();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?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>
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue