Search in sources :

Example 11 with Scope

use of io.opentracing.Scope in project opentracing-java by opentracing.

the class ActiveSpanReplacementTest method test.

@Test
public void test() throws Exception {
    // Start an isolated task and query for its result in another task/thread
    try (Scope scope = tracer.buildSpan("initial").startActive(false)) {
        // Explicitly pass a Span to be finished once a late calculation is done.
        submitAnotherTask(scope.span());
    }
    await().atMost(15, TimeUnit.SECONDS).until(finishedSpansSize(tracer), equalTo(3));
    List<MockSpan> spans = tracer.finishedSpans();
    assertEquals(3, spans.size());
    // Isolated task
    assertEquals("initial", spans.get(0).operationName());
    assertEquals("subtask", spans.get(1).operationName());
    assertEquals("task", spans.get(2).operationName());
    // task/subtask are part of the same trace, and subtask is a child of task
    assertEquals(spans.get(1).context().traceId(), spans.get(2).context().traceId());
    assertEquals(spans.get(2).context().spanId(), spans.get(1).parentId());
    // initial task is not related in any way to those two tasks
    assertNotEquals(spans.get(0).context().traceId(), spans.get(1).context().traceId());
    assertEquals(0, spans.get(0).parentId());
    assertNull(tracer.scopeManager().active());
}
Also used : Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 12 with Scope

use of io.opentracing.Scope in project opentracing-java by opentracing.

the class Actor method ask.

public Future<String> ask(final String message) {
    final Span parent = tracer.scopeManager().active().span();
    phaser.register();
    Future<String> future = executor.submit(new Callable<String>() {

        @Override
        public String call() throws Exception {
            try (Scope child = tracer.buildSpan("received").addReference(References.FOLLOWS_FROM, parent.context()).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CONSUMER).startActive(true)) {
                // child tracer started
                phaser.arriveAndAwaitAdvance();
                // assert size
                phaser.arriveAndAwaitAdvance();
                return "received " + message;
            } finally {
                // child tracer finished
                phaser.arriveAndAwaitAdvance();
                // assert size
                phaser.arriveAndAwaitAdvance();
            }
        }
    });
    return future;
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span)

Example 13 with Scope

use of io.opentracing.Scope in project opentracing-java by opentracing.

the class Client method send.

public void send() throws InterruptedException {
    Message message = new Message();
    try (Scope scope = tracer.buildSpan("send").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).withTag(Tags.COMPONENT.getKey(), "example-client").startActive(true)) {
        tracer.inject(scope.span().context(), Builtin.TEXT_MAP, new TextMapInjectAdapter(message));
        queue.put(message);
    }
}
Also used : Scope(io.opentracing.Scope) TextMapInjectAdapter(io.opentracing.propagation.TextMapInjectAdapter)

Example 14 with Scope

use of io.opentracing.Scope in project opentracing-java by opentracing.

the class ActorPropagationTest method testActorAsk.

@Test
public void testActorAsk() throws ExecutionException, InterruptedException {
    try (Actor actor = new Actor(tracer, phaser)) {
        phaser.register();
        Future<String> future1;
        Future<String> future2;
        try (Scope parent = tracer.buildSpan("actorAsk").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_PRODUCER).withTag(Tags.COMPONENT.getKey(), "example-actor").startActive(true)) {
            future1 = actor.ask("my message 1");
            future2 = actor.ask("my message 2");
        }
        // child tracer started
        phaser.arriveAndAwaitAdvance();
        assertThat(tracer.finishedSpans().size()).isEqualTo(1);
        // continue...
        phaser.arriveAndAwaitAdvance();
        // child tracer finished
        phaser.arriveAndAwaitAdvance();
        assertThat(tracer.finishedSpans().size()).isEqualTo(3);
        assertThat(getByTag(tracer.finishedSpans(), Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER)).hasSize(2);
        // continue...
        phaser.arriveAndDeregister();
        List<MockSpan> finished = tracer.finishedSpans();
        // This really should be a non-blocking callback...
        String message1 = future1.get();
        // This really should be a non-blocking callback...
        String message2 = future2.get();
        assertThat(message1).isEqualTo("received my message 1");
        assertThat(message2).isEqualTo("received my message 2");
        assertThat(finished.size()).isEqualTo(3);
        assertThat(finished.get(0).context().traceId()).isEqualTo(finished.get(1).context().traceId());
        assertThat(getByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER)).hasSize(2);
        assertThat(getOneByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_PRODUCER)).isNotNull();
        assertThat(tracer.scopeManager().active()).isNull();
    }
}
Also used : Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 15 with Scope

use of io.opentracing.Scope in project opentracing-java by opentracing.

the class ActorPropagationTest method testActorTell.

@Test
public void testActorTell() {
    try (Actor actor = new Actor(tracer, phaser)) {
        phaser.register();
        try (Scope parent = tracer.buildSpan("actorTell").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_PRODUCER).withTag(Tags.COMPONENT.getKey(), "example-actor").startActive(true)) {
            actor.tell("my message 1");
            actor.tell("my message 2");
        }
        // child tracer started
        phaser.arriveAndAwaitAdvance();
        // Parent should be reported
        assertThat(tracer.finishedSpans().size()).isEqualTo(1);
        // continue...
        phaser.arriveAndAwaitAdvance();
        // child tracer finished
        phaser.arriveAndAwaitAdvance();
        assertThat(tracer.finishedSpans().size()).isEqualTo(3);
        assertThat(getByTag(tracer.finishedSpans(), Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER)).hasSize(2);
        // continue...
        phaser.arriveAndDeregister();
        List<MockSpan> finished = tracer.finishedSpans();
        assertThat(finished.size()).isEqualTo(3);
        assertThat(finished.get(0).context().traceId()).isEqualTo(finished.get(1).context().traceId());
        assertThat(getByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER)).hasSize(2);
        assertThat(getOneByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_PRODUCER)).isNotNull();
        assertThat(tracer.scopeManager().active()).isNull();
    }
}
Also used : Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Aggregations

Scope (io.opentracing.Scope)80 Test (org.junit.Test)52 Span (io.opentracing.Span)46 MockSpan (io.opentracing.mock.MockSpan)10 Tracer (io.opentracing.Tracer)7 Response (javax.ws.rs.core.Response)6 InMemoryReporter (com.uber.jaeger.reporters.InMemoryReporter)5 ConstSampler (com.uber.jaeger.samplers.ConstSampler)5 ScopeManager (io.opentracing.ScopeManager)5 SpanInScope (brave.Tracer.SpanInScope)4 SpanContext (io.opentracing.SpanContext)4 HashMap (java.util.HashMap)4 SpanBuilder (io.opentracing.Tracer.SpanBuilder)3 IOException (java.io.IOException)3 Traced (org.eclipse.microprofile.opentracing.Traced)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 BraveSpan (brave.opentracing.BraveSpan)2 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)2 RequestTracingService (fish.payara.nucleus.requesttracing.RequestTracingService)2 Downstream (io.jaegertracing.crossdock.api.Downstream)2