Search in sources :

Example 46 with Scope

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

the class HandlerTest method parent_not_picked_up.

/**
 * active parent is not picked up by child
 */
@Test
public void parent_not_picked_up() throws Exception {
    try (Scope parent = tracer.buildSpan("parent").startActive(true)) {
        String response = client.send("no_parent").get(15, TimeUnit.SECONDS);
        assertEquals("no_parent:response", response);
    }
    List<MockSpan> finished = tracer.finishedSpans();
    assertEquals(2, finished.size());
    MockSpan child = getOneByOperationName(finished, RequestHandler.OPERATION_NAME);
    assertNotNull(child);
    MockSpan parent = getOneByOperationName(finished, "parent");
    assertNotNull(parent);
    // Here check that there is no parent-child relation although it should be because child is
    // created when parent is active
    assertNotEquals(parent.context().spanId(), child.parentId());
}
Also used : Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 47 with Scope

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

the class NestedCallbacksTest method test.

@Test
public void test() throws Exception {
    try (Scope scope = tracer.buildSpan("one").startActive(false)) {
        submitCallbacks(scope.span());
    }
    await().atMost(15, TimeUnit.SECONDS).until(finishedSpansSize(tracer), equalTo(1));
    List<MockSpan> spans = tracer.finishedSpans();
    assertEquals(1, spans.size());
    assertEquals("one", spans.get(0).operationName());
    Map<String, Object> tags = spans.get(0).tags();
    assertEquals(3, tags.size());
    for (int i = 1; i <= 3; i++) {
        assertEquals(Integer.toString(i), tags.get("key" + i));
    }
    assertNull(tracer.scopeManager().active());
}
Also used : Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 48 with Scope

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

the class PromisePropagationTest method testPromiseCallback.

@Test
public void testPromiseCallback() {
    // register test thread
    phaser.register();
    final AtomicReference<String> successResult1 = new AtomicReference<>();
    final AtomicReference<String> successResult2 = new AtomicReference<>();
    final AtomicReference<Throwable> errorResult = new AtomicReference<>();
    try (PromiseContext context = new PromiseContext(phaser, 3)) {
        try (Scope parent = tracer.buildSpan("promises").withTag(Tags.COMPONENT.getKey(), "example-promises").startActive(true)) {
            Promise<String> successPromise = new Promise<>(context, tracer);
            successPromise.onSuccess(new Promise.SuccessCallback<String>() {

                @Override
                public void accept(String s) {
                    tracer.scopeManager().active().span().log("Promised 1 " + s);
                    successResult1.set(s);
                    // result set
                    phaser.arriveAndAwaitAdvance();
                }
            });
            successPromise.onSuccess(new Promise.SuccessCallback<String>() {

                @Override
                public void accept(String s) {
                    tracer.scopeManager().active().span().log("Promised 2 " + s);
                    successResult2.set(s);
                    // result set
                    phaser.arriveAndAwaitAdvance();
                }
            });
            Promise errorPromise = new Promise(context, tracer);
            errorPromise.onError(new Promise.ErrorCallback() {

                @Override
                public void accept(Throwable t) {
                    errorResult.set(t);
                    // result set
                    phaser.arriveAndAwaitAdvance();
                }
            });
            assertThat(tracer.finishedSpans().size()).isEqualTo(0);
            successPromise.success("success!");
            errorPromise.error(new Exception("some error."));
        }
        // wait for results to be set
        phaser.arriveAndAwaitAdvance();
        assertThat(successResult1.get()).isEqualTo("success!");
        assertThat(successResult2.get()).isEqualTo("success!");
        assertThat(errorResult.get()).hasMessage("some error.");
        // wait for traces to be reported
        phaser.arriveAndAwaitAdvance();
        List<MockSpan> finished = tracer.finishedSpans();
        assertThat(finished.size()).isEqualTo(4);
        assertThat(getOneByTag(finished, Tags.COMPONENT, "example-promises")).isNotNull();
        assertThat(getOneByTag(finished, Tags.COMPONENT, "example-promises").parentId()).isEqualTo(0);
        long parentId = getOneByTag(finished, Tags.COMPONENT, "example-promises").context().spanId();
        assertThat(getByTag(finished, Tags.COMPONENT, "success")).hasSize(2);
        for (MockSpan span : getByTag(finished, Tags.COMPONENT, "success")) {
            assertThat(span.parentId()).isEqualTo(parentId);
        }
        assertThat(getOneByTag(finished, Tags.COMPONENT, "error")).isNotNull();
        assertThat(getOneByTag(finished, Tags.COMPONENT, "error").parentId()).isEqualTo(parentId);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 49 with Scope

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

the class Actor method tell.

public void tell(final String message) {
    final Span parent = tracer.scopeManager().active().span();
    phaser.register();
    executor.submit(new Runnable() {

        @Override
        public void run() {
            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();
                child.span().log("received " + message);
                // assert size
                phaser.arriveAndAwaitAdvance();
            }
            // child tracer finished
            phaser.arriveAndAwaitAdvance();
            // assert size
            phaser.arriveAndAwaitAdvance();
        }
    });
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span)

Example 50 with Scope

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

the class MockTracerTest method testActiveSpan.

@Test
public void testActiveSpan() {
    MockTracer mockTracer = new MockTracer();
    Assert.assertNull(mockTracer.activeSpan());
    Scope scope = null;
    try {
        scope = mockTracer.buildSpan("foo").startActive(true);
        Assert.assertEquals(mockTracer.scopeManager().active().span(), mockTracer.activeSpan());
    } finally {
        scope.close();
    }
    Assert.assertNull(mockTracer.activeSpan());
}
Also used : Scope(io.opentracing.Scope) 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