Search in sources :

Example 31 with SpanInScope

use of brave.Tracer.SpanInScope in project cxf by apache.

the class CatalogServiceImpl method addBookAsync.

public Future<?> addBookAsync(Book book, AsyncHandler<Book> handler) {
    final ServerAsyncResponse<Book> response = new ServerAsyncResponse<Book>();
    executor.submit(() -> {
        final Span span = brave.tracer().nextSpan().name("Inserting New Book").start();
        try (SpanInScope scope = brave.tracer().withSpanInScope(span)) {
            books.put(book.getId(), book);
            handler.handleResponse(response);
        } finally {
            span.finish();
        }
    });
    return response;
}
Also used : Book(demo.jaxws.tracing.server.Book) SpanInScope(brave.Tracer.SpanInScope) ServerAsyncResponse(org.apache.cxf.jaxws.ServerAsyncResponse) Span(brave.Span)

Example 32 with SpanInScope

use of brave.Tracer.SpanInScope in project spring-cloud-sleuth by spring-cloud.

the class BraveTracerTest method subsequentChildrenNestProperly_BraveStyle.

@Test
public void subsequentChildrenNestProperly_BraveStyle() {
    // this test is semantically identical to subsequentChildrenNestProperly_OTStyle, but uses
    // the Brave API instead of the OpenTracing API.
    Long shouldBeIdOfSpanA;
    Long idOfSpanB;
    Long shouldBeIdOfSpanB;
    Long parentIdOfSpanB;
    Long parentIdOfSpanC;
    Span spanA = brave.tracer().newTrace().name("spanA").start();
    Long idOfSpanA = spanA.context().spanId();
    try (SpanInScope scopeA = brave.tracer().withSpanInScope(spanA)) {
        Span spanB = brave.tracer().newChild(spanA.context()).name("spanB").start();
        idOfSpanB = spanB.context().spanId();
        parentIdOfSpanB = spanB.context().parentId();
        try (SpanInScope scopeB = brave.tracer().withSpanInScope(spanB)) {
            shouldBeIdOfSpanB = brave.currentTraceContext().get().spanId();
        } finally {
            spanB.finish();
        }
        shouldBeIdOfSpanA = brave.currentTraceContext().get().spanId();
        Span spanC = brave.tracer().newChild(spanA.context()).name("spanC").start();
        parentIdOfSpanC = spanC.context().parentId();
        try (SpanInScope scopeC = brave.tracer().withSpanInScope(spanC)) {
        // nothing to do here
        } finally {
            spanC.finish();
        }
    } finally {
        spanA.finish();
    }
    assertEquals("SpanA should have been active again after closing B", idOfSpanA, shouldBeIdOfSpanA);
    assertEquals("SpanB should have been active prior to its closure", idOfSpanB, shouldBeIdOfSpanB);
    assertEquals("SpanB's parent should be SpanA", idOfSpanA, parentIdOfSpanB);
    assertEquals("SpanC's parent should be SpanA", idOfSpanA, parentIdOfSpanC);
}
Also used : SpanInScope(brave.Tracer.SpanInScope) Span(brave.Span) BraveSpan(brave.opentracing.BraveSpan) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 33 with SpanInScope

use of brave.Tracer.SpanInScope in project incubator-servicecomb-java-chassis by apache.

the class ZipkinTracingAdviserTest method startsNewChildSpan.

@Test
public void startsNewChildSpan() throws Exception {
    CyclicBarrier cyclicBarrier = new CyclicBarrier(nThreads);
    CompletableFuture<?>[] futures = new CompletableFuture[nThreads];
    for (int i = 0; i < nThreads; i++) {
        futures[i] = CompletableFuture.runAsync(() -> {
            Span currentSpan = tracing.tracer().newTrace().start();
            waitTillAllAreReady(cyclicBarrier);
            try (SpanInScope spanInScope = tracing.tracer().withSpanInScope(currentSpan)) {
                assertThat(tracingAdviser.invoke(spanName, path, supplier), is(expected));
            } catch (Throwable throwable) {
                fail(throwable.getMessage());
            } finally {
                currentSpan.finish();
            }
        }, Executors.newFixedThreadPool(nThreads));
    }
    CompletableFuture.allOf(futures).join();
    assertThat(traces.size(), is(nThreads));
    for (Queue<zipkin2.Span> queue : traces.values()) {
        zipkin2.Span child = queue.poll();
        assertThat(child.name(), is(spanName));
        zipkin2.Span parent = queue.poll();
        assertThat(child.parentId(), is(parent.id()));
        assertThat(child.traceId(), is(parent.traceId()));
        assertThat(tracedValues(child), contains(this.getClass().getCanonicalName()));
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SpanInScope(brave.Tracer.SpanInScope) Span(brave.Span) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 34 with SpanInScope

use of brave.Tracer.SpanInScope in project incubator-servicecomb-java-chassis by apache.

the class TracePostZuulFilter method run.

@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();
    ((SpanInScope) context.getRequest().getAttribute(SpanInScope.class.getName())).close();
    clientHandler.handleReceive(context.getResponse(), null, tracer.currentSpan());
    log.debug("Closed span {} for {}", tracer.currentSpan(), context.getRequest().getMethod());
    return null;
}
Also used : SpanInScope(brave.Tracer.SpanInScope) RequestContext(com.netflix.zuul.context.RequestContext)

Example 35 with SpanInScope

use of brave.Tracer.SpanInScope in project incubator-servicecomb-java-chassis by apache.

the class TracePreZuulFilter method runFilter.

@Override
public ZuulFilterResult runFilter() {
    RequestContext ctx = RequestContext.getCurrentContext();
    Span span = clientHandler.handleSend(injector, ctx);
    saveHeadersAsInvocationContext(ctx, span);
    SpanInScope scope = tracer.withSpanInScope(span);
    log.debug("Generated tracing span {} for {}", span, ctx.getRequest().getMethod());
    ctx.getRequest().setAttribute(SpanInScope.class.getName(), scope);
    ZuulFilterResult result = super.runFilter();
    log.debug("Result of Zuul filter is [{}]", result.getStatus());
    if (ExecutionStatus.SUCCESS != result.getStatus()) {
        log.debug("The result of Zuul filter execution was not successful thus will close the current span {}", span);
        clientHandler.handleReceive(ctx.getResponse(), result.getException(), span);
        scope.close();
    }
    return result;
}
Also used : SpanInScope(brave.Tracer.SpanInScope) ZuulFilterResult(com.netflix.zuul.ZuulFilterResult) RequestContext(com.netflix.zuul.context.RequestContext) Span(brave.Span)

Aggregations

SpanInScope (brave.Tracer.SpanInScope)55 Span (brave.Span)34 Test (org.junit.Test)23 MutableSpan (brave.handler.MutableSpan)13 MockResponse (okhttp3.mockwebserver.MockResponse)4 Tracer (brave.Tracer)3 TopicPublisher (javax.jms.TopicPublisher)3 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)2 RequestContext (com.netflix.zuul.context.RequestContext)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 Destination (javax.jms.Destination)2 Response (javax.ws.rs.core.Response)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 HasSpan.hasSpan (org.apache.cxf.systest.jaxrs.tracing.brave.HasSpan.hasSpan)2 Request (org.apache.cxf.tracing.brave.internal.HttpAdapterFactory.Request)2 BraveClientProvider (org.apache.cxf.tracing.brave.jaxrs.BraveClientProvider)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 Tracing (brave.Tracing)1