Search in sources :

Example 41 with Scope

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

the class ThreadLocalScopeManagerTest method dontFinishSpanNoClose.

@Test
public void dontFinishSpanNoClose() throws Exception {
    Span span = mock(Span.class);
    // We can't use 1.7 features like try-with-resources in this repo without meddling with pom details for tests.
    Scope scope = source.activate(span, false);
    try {
        assertNotNull(scope);
        assertNotNull(source.active());
    } finally {
        scope.close();
    }
    // Make sure the Span did *not* get finish()ed.
    verify(span, never()).finish();
    // Verify it's gone.
    assertNull(source.active());
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Test(org.junit.Test)

Example 42 with Scope

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

the class ThreadLocalScopeManagerTest method finishSpanClose.

@Test
public void finishSpanClose() throws Exception {
    Span span = mock(Span.class);
    // We can't use 1.7 features like try-with-resources in this repo without meddling with pom details for tests.
    Scope scope = source.activate(span, true);
    try {
        assertNotNull(scope);
        assertNotNull(source.active());
    } finally {
        scope.close();
    }
    // Make sure the Span got finish()ed.
    verify(span, times(1)).finish();
    // Verify it's gone.
    assertNull(source.active());
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Test(org.junit.Test)

Example 43 with Scope

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

the class ThreadLocalScopeTest method implicitSpanStack.

@Test
public void implicitSpanStack() throws Exception {
    Span backgroundSpan = mock(Span.class);
    Span foregroundSpan = mock(Span.class);
    // Quasi try-with-resources (this is 1.6).
    Scope backgroundActive = scopeManager.activate(backgroundSpan, true);
    try {
        assertNotNull(backgroundActive);
        // Activate a new Scope on top of the background one.
        Scope foregroundActive = scopeManager.activate(foregroundSpan, true);
        try {
            Scope shouldBeForeground = scopeManager.active();
            assertEquals(foregroundActive, shouldBeForeground);
        } finally {
            foregroundActive.close();
        }
        // And now the backgroundActive should be reinstated.
        Scope shouldBeBackground = scopeManager.active();
        assertEquals(backgroundActive, shouldBeBackground);
    } finally {
        backgroundActive.close();
    }
    // The background and foreground Spans should be finished.
    verify(backgroundSpan, times(1)).finish();
    verify(foregroundSpan, times(1)).finish();
    // And now nothing is active.
    Scope missingSpan = scopeManager.active();
    assertNull(missingSpan);
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Test(org.junit.Test)

Example 44 with Scope

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

the class ThreadLocalScopeTest method testDeactivateWhenDifferentSpanIsActive.

@Test
public void testDeactivateWhenDifferentSpanIsActive() {
    Span span = mock(Span.class);
    Scope active = scopeManager.activate(span, false);
    scopeManager.activate(mock(Span.class), false);
    active.close();
    verify(span, times(0)).finish();
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Test(org.junit.Test)

Example 45 with Scope

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

the class HandlerTest method bad_solution_to_set_parent.

/**
 * Solution is bad because parent is per client (we don't have better choice).
 * Therefore all client requests will have the same parent.
 * But if client is long living and injected/reused in different places then initial parent will not be correct.
 */
@Test
public void bad_solution_to_set_parent() throws Exception {
    Client client;
    try (Scope parent = tracer.buildSpan("parent").startActive(true)) {
        client = new Client(new RequestHandler(tracer, parent.span().context()));
        String response = client.send("correct_parent").get(15, TimeUnit.SECONDS);
        assertEquals("correct_parent:response", response);
    }
    // Send second request, now there is no active parent, but it will be set, ups
    String response = client.send("wrong_parent").get(15, TimeUnit.SECONDS);
    assertEquals("wrong_parent:response", response);
    List<MockSpan> finished = tracer.finishedSpans();
    assertEquals(3, finished.size());
    sortByStartMicros(finished);
    MockSpan parent = getOneByOperationName(finished, "parent");
    assertNotNull(parent);
    // now there is parent/child relation between first and second span:
    assertEquals(parent.context().spanId(), finished.get(1).parentId());
    // third span should not have parent, but it has, damn it
    assertEquals(parent.context().spanId(), finished.get(2).parentId());
}
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