Search in sources :

Example 36 with Scope

use of io.opentracing.Scope in project cxf by apache.

the class AbstractOpenTracingProvider method startTraceSpan.

protected TraceScopeHolder<TraceScope> startTraceSpan(final Map<String, List<String>> requestHeaders, URI uri, String method) {
    SpanContext parent = tracer.extract(Builtin.HTTP_HEADERS, new TextMapAdapter(requestHeaders.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, this::getFirstValueOrEmpty))));
    final Span activeSpan;
    final Scope scope;
    if (parent == null) {
        activeSpan = tracer.buildSpan(buildSpanDescription(uri.getPath(), method)).start();
        scope = tracer.scopeManager().activate(activeSpan);
    } else {
        activeSpan = tracer.buildSpan(buildSpanDescription(uri.getPath(), method)).asChildOf(parent).start();
        scope = tracer.scopeManager().activate(activeSpan);
    }
    // Set additional tags
    activeSpan.setTag(Tags.HTTP_METHOD.getKey(), method);
    activeSpan.setTag(Tags.HTTP_URL.getKey(), uri.toString());
    // If the service resource is using asynchronous processing mode, the trace
    // scope will be closed in another thread and as such should be detached.
    Span span = null;
    if (isAsyncResponse()) {
        // Do not modify the current context span
        span = activeSpan;
        propagateContinuationSpan(span);
        scope.close();
    }
    return new TraceScopeHolder<TraceScope>(new TraceScope(activeSpan, scope), span != null);
}
Also used : SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) TextMapAdapter(io.opentracing.propagation.TextMapAdapter) Map(java.util.Map) Span(io.opentracing.Span)

Example 37 with Scope

use of io.opentracing.Scope in project cxf by apache.

the class OpenTracingTracingTest method testThatProvidedSpanIsNotClosedWhenActive.

@Test
public void testThatProvidedSpanIsNotClosedWhenActive() throws Exception {
    final BookStoreService service = createJaxWsService(new OpenTracingClientFeature(tracer));
    final Span span = tracer.buildSpan("test span").start();
    try (Scope scope = tracer.activateSpan(span)) {
        assertThat(service.getBooks().size(), equalTo(2));
        assertThat(tracer.activeSpan(), not(nullValue()));
        assertThat(REPORTER.getSpans().size(), equalTo(3));
        assertThat(REPORTER.getSpans().get(0).getOperationName(), equalTo("Get Books"));
        assertThat(REPORTER.getSpans().get(0).getReferences(), not(empty()));
        assertThat(REPORTER.getSpans().get(1).getOperationName(), equalTo("POST /BookStore"));
        assertThat(REPORTER.getSpans().get(1).getReferences(), not(empty()));
        assertThat(REPORTER.getSpans().get(2).getOperationName(), equalTo("POST http://localhost:" + PORT + "/BookStore"));
        assertThat(REPORTER.getSpans().get(2).getReferences(), not(empty()));
    } finally {
        span.finish();
    }
    // Await till flush happens, usually every second
    await().atMost(Duration.ofSeconds(1L)).until(() -> REPORTER.getSpans().size() == 4);
    assertThat(REPORTER.getSpans().size(), equalTo(4));
    assertThat(REPORTER.getSpans().get(3).getOperationName(), equalTo("test span"));
    assertThat(REPORTER.getSpans().get(3).getReferences(), empty());
}
Also used : OpenTracingClientFeature(org.apache.cxf.tracing.opentracing.OpenTracingClientFeature) Scope(io.opentracing.Scope) BookStoreService(org.apache.cxf.systest.jaxws.tracing.BookStoreService) Span(io.opentracing.Span) Test(org.junit.Test)

Example 38 with Scope

use of io.opentracing.Scope in project cxf by apache.

the class OpenTracingTracingTest method testThatProvidedSpanIsNotClosedWhenActive.

@Test
public void testThatProvidedSpanIsNotClosedWhenActive() throws MalformedURLException {
    final WebClient client = createWebClient("/bookstore/books", new OpenTracingClientProvider(tracer));
    final Span span = tracer.buildSpan("test span").start();
    try (Scope scope = tracer.scopeManager().activate(span)) {
        final Response r = client.get();
        assertEquals(Status.OK.getStatusCode(), r.getStatus());
        assertEquals(REPORTER.getSpans().toString(), 3, REPORTER.getSpans().size());
        assertThat(REPORTER.getSpans().get(0).getOperationName(), equalTo("Get Books"));
        assertThat(REPORTER.getSpans().get(0).getReferences(), not(empty()));
        assertThat(REPORTER.getSpans().get(1).getOperationName(), equalTo("GET /bookstore/books"));
        assertThat(REPORTER.getSpans().get(1).getReferences(), not(empty()));
        assertThat(REPORTER.getSpans().get(2).getOperationName(), equalTo("GET " + client.getCurrentURI()));
        assertThat(REPORTER.getSpans().get(2).getReferences(), not(empty()));
    } finally {
        span.finish();
    }
    // Await till flush happens, usually every second
    await().atMost(Duration.ofSeconds(1L)).until(() -> REPORTER.getSpans().size() == 4);
    assertThat(REPORTER.getSpans().size(), equalTo(4));
    assertThat(REPORTER.getSpans().get(3).getOperationName(), equalTo("test span"));
    assertThat(REPORTER.getSpans().get(3).getReferences(), empty());
}
Also used : Response(javax.ws.rs.core.Response) Scope(io.opentracing.Scope) OpenTracingClientProvider(org.apache.cxf.tracing.opentracing.jaxrs.OpenTracingClientProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient) HasSpan.hasSpan(org.apache.cxf.systest.jaxrs.tracing.opentracing.HasSpan.hasSpan) Span(io.opentracing.Span) Test(org.junit.Test)

Example 39 with Scope

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

the class AutoFinishScopeManagerTest method activateSpan.

@Test
public void activateSpan() 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 active = source.activate(span, true);
    try {
        assertNotNull(active);
        Scope otherScope = source.active();
        assertEquals(otherScope, active);
    } finally {
        active.close();
    }
    // Make sure the Span got finish()ed.
    verify(span).finish();
    // And now it's gone:
    Scope missingSpan = source.active();
    assertNull(missingSpan);
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Test(org.junit.Test)

Example 40 with Scope

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

the class AutoFinishScopeTest method continuation.

@Test
public void continuation() throws Exception {
    Span span = mock(Span.class);
    // Quasi try-with-resources (this is 1.6).
    AutoFinishScope active = (AutoFinishScope) manager.activate(span, true);
    AutoFinishScope.Continuation continued = null;
    try {
        assertNotNull(active);
        continued = active.capture();
    } finally {
        active.close();
    }
    // Make sure the Span was not finished since there was a capture().
    verify(span, never()).finish();
    // Activate the continuation.
    try {
        active = continued.activate();
    } finally {
        active.close();
    }
    // Now the Span should be finished.
    verify(span, times(1)).finish();
    // And now it's no longer active.
    Scope missingSpan = manager.active();
    assertNull(missingSpan);
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) 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