Search in sources :

Example 16 with Scope

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";
        }
    });
}
Also used : Continuation(io.opentracing.util.AutoFinishScope.Continuation) AutoFinishScope(io.opentracing.util.AutoFinishScope) Scope(io.opentracing.Scope) AutoFinishScope(io.opentracing.util.AutoFinishScope)

Example 17 with Scope

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");
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }
    });
}
Also used : Scope(io.opentracing.Scope)

Example 18 with Scope

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());
}
Also used : Scope(io.opentracing.Scope) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 19 with Scope

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;
}
Also used : Scope(io.opentracing.Scope) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Span(io.opentracing.Span)

Example 20 with Scope

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;
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span)

Aggregations

Scope (io.opentracing.Scope)80 Test (org.junit.Test)52 Span (io.opentracing.Span)46 MockSpan (io.opentracing.mock.MockSpan)10 Tracer (io.opentracing.Tracer)7 Response (javax.ws.rs.core.Response)6 InMemoryReporter (com.uber.jaeger.reporters.InMemoryReporter)5 ConstSampler (com.uber.jaeger.samplers.ConstSampler)5 ScopeManager (io.opentracing.ScopeManager)5 SpanInScope (brave.Tracer.SpanInScope)4 SpanContext (io.opentracing.SpanContext)4 HashMap (java.util.HashMap)4 SpanBuilder (io.opentracing.Tracer.SpanBuilder)3 IOException (java.io.IOException)3 Traced (org.eclipse.microprofile.opentracing.Traced)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 BraveSpan (brave.opentracing.BraveSpan)2 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)2 RequestTracingService (fish.payara.nucleus.requesttracing.RequestTracingService)2 Downstream (io.jaegertracing.crossdock.api.Downstream)2