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