|
| 1 | +package test; |
| 2 | + |
| 3 | +import datadog.trace.api.Trace; |
| 4 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 5 | +import io.opentelemetry.api.trace.Span; |
| 6 | +import io.opentelemetry.api.trace.SpanBuilder; |
| 7 | +import io.opentelemetry.api.trace.Tracer; |
| 8 | +import io.opentelemetry.context.Scope; |
| 9 | +import java.time.Duration; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.Random; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import reactor.core.publisher.Mono; |
| 14 | +import reactor.util.context.Context; |
| 15 | + |
| 16 | +public class ReactorTest { |
| 17 | + |
| 18 | + @Trace(operationName = "child", resourceName = "child") |
| 19 | + private String doSomeMapping(String s) { |
| 20 | + try { |
| 21 | + // simulate some work |
| 22 | + Thread.sleep(500 + new Random(System.currentTimeMillis()).nextInt(1000)); |
| 23 | + } catch (InterruptedException ie) { |
| 24 | + Thread.currentThread().interrupt(); |
| 25 | + } |
| 26 | + return s; |
| 27 | + } |
| 28 | + |
| 29 | + @Trace(operationName = "mono", resourceName = "mono") |
| 30 | + private Mono<String> monoMethod() { |
| 31 | + // This mono will complete when the delay is expired |
| 32 | + return Mono.delay(Duration.ofSeconds(1)).map(ignored -> "Hello World"); |
| 33 | + } |
| 34 | + |
| 35 | + @Trace(operationName = "mono", resourceName = "mono") |
| 36 | + private Mono<String> monoMethodDownstreamPropagate() { |
| 37 | + // here the active span is the one created by the @Trace annotation before the method executes |
| 38 | + return Mono.just("Hello World").contextWrite(Context.of("dd.span", Span.current())); |
| 39 | + } |
| 40 | + |
| 41 | + private <T> Mono<T> tracedMono( |
| 42 | + final Tracer tracer, final String spanName, Span parentSpan, final Mono<T> mono) { |
| 43 | + SpanBuilder spanBuilder = tracer.spanBuilder(spanName); |
| 44 | + if (parentSpan != null) { |
| 45 | + spanBuilder.setParent(io.opentelemetry.context.Context.current().with(parentSpan)); |
| 46 | + } |
| 47 | + final Span span = spanBuilder.startSpan(); |
| 48 | + return mono // |
| 49 | + .contextWrite(Context.of("dd.span", span)) |
| 50 | + .doFinally(ignored -> span.end()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testSimpleUpstreamPropagation() { |
| 55 | + final Tracer tracer = GlobalOpenTelemetry.getTracer(""); |
| 56 | + final Span parent = tracer.spanBuilder("parent").startSpan(); |
| 57 | + assert io.opentelemetry.context.Context.current() == io.opentelemetry.context.Context.root(); |
| 58 | + try (final Scope parentScope = parent.makeCurrent()) { |
| 59 | + // monoMethod will start a trace when called but that span will complete only when the |
| 60 | + // returned mono completes. |
| 61 | + // doSomeMapping will open a span that's child of parent because it's the active one when we |
| 62 | + // subscribe |
| 63 | + assert Objects.equals(monoMethod().map(this::doSomeMapping).block(), "Hello World"); |
| 64 | + } finally { |
| 65 | + parent.end(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testSimpleDownstreamPropagation() { |
| 71 | + final Tracer tracer = GlobalOpenTelemetry.getTracer(""); |
| 72 | + final Span parent = tracer.spanBuilder("parent").startSpan(); |
| 73 | + assert io.opentelemetry.context.Context.current() == io.opentelemetry.context.Context.root(); |
| 74 | + try (final Scope parentScope = parent.makeCurrent()) { |
| 75 | + // monoMethod will start a trace when called but that span will complete only when the |
| 76 | + // returned mono completes. |
| 77 | + // doSomeMapping will open a span that's child of parent because it's the active one when we |
| 78 | + // subscribe |
| 79 | + assert Objects.equals( |
| 80 | + Mono.defer(this::monoMethodDownstreamPropagate).map(this::doSomeMapping).block(), |
| 81 | + "Hello World"); |
| 82 | + } finally { |
| 83 | + parent.end(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testComplexDownstreamPropagation() { |
| 89 | + final Tracer tracer = GlobalOpenTelemetry.getTracer(""); |
| 90 | + final Span parent = tracer.spanBuilder("parent").startSpan(); |
| 91 | + assert io.opentelemetry.context.Context.current() == io.opentelemetry.context.Context.root(); |
| 92 | + |
| 93 | + Mono<String> mono = |
| 94 | + // here we have no active span. when the mono is emitted we propagate the context captured |
| 95 | + // onSubscribe |
| 96 | + Mono.just("Hello World") // |
| 97 | + // change the downstream propagated span to that new one called 'first' |
| 98 | + // first will be child of parent since parent was captured onSubscribe |
| 99 | + // (when block is called) and propagated upstream |
| 100 | + .flatMap(s -> tracedMono(tracer, "first", null, Mono.just(s + ", GoodBye "))) |
| 101 | + // map will use the active one (first) hence the child will be under first |
| 102 | + .map(this::doSomeMapping) |
| 103 | + // we change again the downstream active span to 'second' that's child of 'first' |
| 104 | + .flatMap( |
| 105 | + s -> |
| 106 | + tracedMono( |
| 107 | + tracer, "second", null, Mono.create(sink -> sink.success(s + "World")))) |
| 108 | + // now we let the downstream propagate third child of parent |
| 109 | + .flatMap(s -> tracedMono(tracer, "third", parent, Mono.just(s + "!"))) |
| 110 | + // third is the active span downstream |
| 111 | + .map(this::doSomeMapping) // will create a child span having third as parent |
| 112 | + .doOnNext(System.out::println); |
| 113 | + try (final Scope parentScope = parent.makeCurrent()) { |
| 114 | + // block, like subscribe will capture the current scope (parent here) and propagate upstream |
| 115 | + assert Objects.equals(mono.block(), "Hello World, GoodBye World!"); |
| 116 | + } finally { |
| 117 | + parent.end(); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments