use of io.opentracing.Scope in project opentracing-java by opentracing.
the class Client method send.
public Future<Object> send(final Object message, final long milliseconds) {
Scope scope = tracer.scopeManager().active();
final Continuation cont = ((AutoFinishScope) scope).capture();
return executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
logger.info("Child thread with message '{}' started", message);
try (Scope parentScope = cont.activate()) {
try (Scope subtaskScope = tracer.buildSpan("subtask").startActive(false)) {
// Simulate work.
sleep(milliseconds);
}
}
logger.info("Child thread with message '{}' finished", message);
return message + "::response";
}
});
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class NestedCallbacksTest method submitCallbacks.
private void submitCallbacks(final Span span) {
executor.submit(new Runnable() {
@Override
public void run() {
try (Scope scope = tracer.scopeManager().activate(span, false)) {
span.setTag("key1", "1");
executor.submit(new Runnable() {
@Override
public void run() {
try (Scope scope = tracer.scopeManager().activate(span, false)) {
span.setTag("key2", "2");
executor.submit(new Runnable() {
@Override
public void run() {
try (Scope scope = tracer.scopeManager().activate(span, true)) {
span.setTag("key3", "3");
}
}
});
}
}
});
}
}
});
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class MultipleCallbacksTest method test.
@Test
public void test() throws Exception {
Client client = new Client(tracer);
try (Scope scope = tracer.buildSpan("parent").startActive(false)) {
client.send("task1", 300);
client.send("task2", 200);
client.send("task3", 100);
}
await().atMost(15, TimeUnit.SECONDS).until(finishedSpansSize(tracer), equalTo(4));
List<MockSpan> spans = tracer.finishedSpans();
assertEquals(4, spans.size());
assertEquals("parent", spans.get(3).operationName());
MockSpan parentSpan = spans.get(3);
for (int i = 0; i < 3; i++) {
assertEquals(true, parentSpan.finishMicros() >= spans.get(i).finishMicros());
assertEquals(parentSpan.context().traceId(), spans.get(i).context().traceId());
assertEquals(parentSpan.context().spanId(), spans.get(i).parentId());
}
assertNull(tracer.scopeManager().active());
}
use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.
the class TracingAsyncRestTemplateInterceptor method intercept.
@Override
public ListenableFuture<ClientHttpResponse> intercept(final HttpRequest httpRequest, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException {
final Scope scope = tracer.buildSpan(httpRequest.getMethod().toString()).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).startActive(false);
tracer.inject(scope.span().context(), Format.Builtin.HTTP_HEADERS, new HttpHeadersCarrier(httpRequest.getHeaders()));
for (RestTemplateSpanDecorator spanDecorator : spanDecorators) {
try {
spanDecorator.onRequest(httpRequest, scope.span());
} catch (RuntimeException exDecorator) {
log.error("Exception during decorating span", exDecorator);
}
}
ListenableFuture<ClientHttpResponse> future = execution.executeAsync(httpRequest, body);
final Span span = scope.span();
future.addCallback(new ListenableFutureCallback<ClientHttpResponse>() {
@Override
public void onSuccess(ClientHttpResponse httpResponse) {
try (Scope asyncScope = tracer.scopeManager().activate(span, true)) {
for (RestTemplateSpanDecorator spanDecorator : spanDecorators) {
try {
spanDecorator.onResponse(httpRequest, httpResponse, scope.span());
} catch (RuntimeException exDecorator) {
log.error("Exception during decorating span", exDecorator);
}
}
}
}
@Override
public void onFailure(Throwable ex) {
try (Scope asyncScope = tracer.scopeManager().activate(span, true)) {
for (RestTemplateSpanDecorator spanDecorator : spanDecorators) {
try {
spanDecorator.onError(httpRequest, ex, scope.span());
} catch (RuntimeException exDecorator) {
log.error("Exception during decorating span", exDecorator);
}
}
}
}
});
scope.close();
return future;
}
use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.
the class TracingHandlerInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
if (!isTraced(httpServletRequest)) {
return true;
}
/**
* 1. check if there is an active span, it has been activated in servlet filter or in this interceptor (forward)
* 2. if there is no active span then it can be handling of an async request or spring boot default error handling
*/
Scope serverSpan = tracer.scopeManager().active();
if (serverSpan == null) {
if (httpServletRequest.getAttribute(CONTINUATION_FROM_ASYNC_STARTED) != null) {
Span contd = (Span) httpServletRequest.getAttribute(CONTINUATION_FROM_ASYNC_STARTED);
serverSpan = tracer.scopeManager().activate(contd, false);
httpServletRequest.removeAttribute(CONTINUATION_FROM_ASYNC_STARTED);
} else {
// spring boot default error handling, executes interceptor after processing in the filter (ugly huh?)
serverSpan = tracer.buildSpan(httpServletRequest.getMethod()).addReference(References.FOLLOWS_FROM, TracingFilter.serverSpanContext(httpServletRequest)).startActive(true);
}
}
for (HandlerInterceptorSpanDecorator decorator : decorators) {
decorator.onPreHandle(httpServletRequest, handler, serverSpan.span());
}
Deque<Scope> activeSpanStack = getScopeStack(httpServletRequest);
activeSpanStack.push(serverSpan);
return true;
}
Aggregations