Search in sources :

Example 26 with SpanInScope

use of brave.Tracer.SpanInScope in project brave by openzipkin.

the class ITHttpClient method makesChildOfCurrentSpan.

@Test
public void makesChildOfCurrentSpan() throws Exception {
    Tracer tracer = httpTracing.tracing().tracer();
    server.enqueue(new MockResponse());
    brave.Span parent = tracer.newTrace().name("test").start();
    try (SpanInScope ws = tracer.withSpanInScope(parent)) {
        get(client, "/foo");
    } finally {
        parent.finish();
    }
    RecordedRequest request = server.takeRequest();
    assertThat(request.getHeader("x-b3-traceId")).isEqualTo(parent.context().traceIdString());
    assertThat(request.getHeader("x-b3-parentspanid")).isEqualTo(HexCodec.toLowerHex(parent.context().spanId()));
    assertThat(Arrays.asList(takeSpan(), takeSpan())).extracting(Span::kind).containsOnly(null, Span.Kind.CLIENT);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Tracer(brave.Tracer) SpanInScope(brave.Tracer.SpanInScope) Test(org.junit.Test)

Example 27 with SpanInScope

use of brave.Tracer.SpanInScope in project brave by openzipkin.

the class ITHttpClient method propagatesExtra_newTrace.

@Test
public void propagatesExtra_newTrace() throws Exception {
    Tracer tracer = httpTracing.tracing().tracer();
    server.enqueue(new MockResponse());
    brave.Span parent = tracer.newTrace().name("test").start();
    try (SpanInScope ws = tracer.withSpanInScope(parent)) {
        ExtraFieldPropagation.set(parent.context(), EXTRA_KEY, "joey");
        get(client, "/foo");
    } finally {
        parent.finish();
    }
    assertThat(server.takeRequest().getHeader(EXTRA_KEY)).isEqualTo("joey");
    // we report one local and one client span
    assertThat(Arrays.asList(takeSpan(), takeSpan())).extracting(Span::kind).containsOnly(null, Span.Kind.CLIENT);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Tracer(brave.Tracer) SpanInScope(brave.Tracer.SpanInScope) Test(org.junit.Test)

Example 28 with SpanInScope

use of brave.Tracer.SpanInScope in project brave by openzipkin.

the class TracingProtocolExec method execute.

@Override
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext, HttpExecutionAware execAware) throws IOException, HttpException {
    Span span = handler.nextSpan(request);
    CloseableHttpResponse response = null;
    Throwable error = null;
    try (SpanInScope ws = tracer.withSpanInScope(span)) {
        return response = protocolExec.execute(route, request, clientContext, execAware);
    } catch (IOException | HttpException | RuntimeException | Error e) {
        error = e;
        throw e;
    } finally {
        handler.handleReceive(response, error, span);
    }
}
Also used : SpanInScope(brave.Tracer.SpanInScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) Span(brave.Span)

Example 29 with SpanInScope

use of brave.Tracer.SpanInScope in project brave by openzipkin.

the class TracingContainerFilter method filter.

@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) {
    Span span = (Span) request.getProperty(Span.class.getName());
    SpanInScope spanInScope = (SpanInScope) request.getProperty(SpanInScope.class.getName());
    if (span != null) {
    // asynchronous response or we couldn't figure it out
    } else if (spanInScope != null) {
        // synchronous response
        span = tracer.currentSpan();
        spanInScope.close();
    } else if (response.getStatus() == 404) {
        span = handler.handleReceive(extractor, request);
    } else {
        // unknown state
        return;
    }
    handler.handleSend(response, null, span);
}
Also used : SpanInScope(brave.Tracer.SpanInScope) Span(brave.Span)

Example 30 with SpanInScope

use of brave.Tracer.SpanInScope in project brave by openzipkin.

the class TracingHandlerInterceptor method afterCompletion.

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception ex) {
    Span span = tracer.currentSpan();
    if (span == null)
        return;
    ((SpanInScope) request.getAttribute(SpanInScope.class.getName())).close();
    handler.handleSend(ADAPTER.adaptResponse(request, response), ex, span);
}
Also used : SpanInScope(brave.Tracer.SpanInScope) 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