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);
}
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);
}
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);
}
}
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);
}
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);
}
Aggregations