Search in sources :

Example 76 with Scope

use of io.opentracing.Scope in project Payara by payara.

the class OpenTracingRequestEventListener method onIncomingRequest.

private void onIncomingRequest(final RequestEvent event, final String operationName) {
    LOG.fine(() -> "onIncomingRequest(event=" + event.getType() + ", operationName=" + operationName + ")");
    final ContainerRequest requestContext = event.getContainerRequest();
    final Tracer tracer = openTracing.getTracer(this.applicationName);
    // Create a Span and instrument it with details about the request
    final SpanBuilder spanBuilder = // 
    tracer.buildSpan(operationName).withTag(Tags.SPAN_KIND.getKey(), // 
    Tags.SPAN_KIND_SERVER).withTag(Tags.HTTP_METHOD.getKey(), // 
    requestContext.getMethod()).withTag(Tags.HTTP_URL.getKey(), // 
    toString(requestContext.getUriInfo())).withTag(Tags.COMPONENT.getKey(), "jaxrs");
    // If there was a context injected into the tracer, add it as a parent of the new span
    final SpanContext spanContext = findSpanContext(requestContext, tracer);
    if (spanContext != null) {
        spanBuilder.asChildOf(spanContext);
    }
    // Start the span and continue on to the targeted method
    Scope scope = tracer.activateSpan(spanBuilder.start());
    // tracer.activeSpan();
    requestContext.setProperty(Scope.class.getName(), scope);
    LOG.fine(() -> "Request tracing enabled for request=" + requestContext.getRequest() + " to application=" + this.applicationName + " on uri=" + toString(requestContext.getUriInfo()));
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder) SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) ContainerRequest(org.glassfish.jersey.server.ContainerRequest)

Example 77 with Scope

use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.

the class TracingHandlerInterceptor method afterCompletion.

@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception ex) throws Exception {
    if (!isTraced(httpServletRequest)) {
        return;
    }
    Deque<Scope> scopeStack = getScopeStack(httpServletRequest);
    Scope scope = scopeStack.pop();
    for (HandlerInterceptorSpanDecorator decorator : decorators) {
        decorator.onAfterCompletion(httpServletRequest, httpServletResponse, handler, ex, scope.span());
    }
    scope.close();
}
Also used : Scope(io.opentracing.Scope)

Example 78 with Scope

use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.

the class TestController method async.

@RequestMapping("/async")
public Callable<String> async() {
    verifyActiveSpan();
    final Span cont = tracer.scopeManager().active().span();
    return new Callable<String>() {

        public String call() throws Exception {
            try (Scope scope = tracer.scopeManager().activate(cont, false)) {
                if (tracer.scopeManager().active() == null) {
                    throw new RuntimeException("No active span");
                }
                Thread.sleep(1000);
                return "async";
            }
        }
    };
}
Also used : Scope(io.opentracing.Scope) Span(io.opentracing.Span) Callable(java.util.concurrent.Callable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 79 with Scope

use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.

the class TracingAsyncRestTemplateTest method testMultipleRequests.

@Test
public void testMultipleRequests() throws InterruptedException, ExecutionException {
    final String url = "http://localhost:8080/foo/";
    int numberOfCalls = 1000;
    mockServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(new Contains("/foo"))).andRespond(MockRestResponseCreators.withSuccess());
    ExecutorService executorService = Executors.newFixedThreadPool(100);
    List<Future<?>> futures = new ArrayList<>(numberOfCalls);
    for (int i = 0; i < numberOfCalls; i++) {
        final String requestUrl = url + i;
        final Scope parentSpan = mockTracer.buildSpan("foo").startActive(false);
        parentSpan.span().setTag("request-url", requestUrl);
        final Span cont = parentSpan.span();
        futures.add(executorService.submit(new Runnable() {

            @Override
            public void run() {
                try (Scope span = mockTracer.scopeManager().activate(cont, true)) {
                    client.getForEntity(requestUrl, String.class);
                }
            }
        }));
        parentSpan.close();
    }
    // wait to finish all calls
    for (Future<?> future : futures) {
        future.get();
    }
    executorService.awaitTermination(1, TimeUnit.SECONDS);
    executorService.shutdown();
    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    Assert.assertEquals(numberOfCalls * 2, mockSpans.size());
    final List<MockSpan> parentSpans = new ArrayList<>();
    final Map<Long, MockSpan> childSpans = new HashMap<>();
    for (MockSpan mockSpan : mockSpans) {
        if (mockSpan.tags().containsKey("request-url")) {
            parentSpans.add(mockSpan);
        } else {
            childSpans.put(mockSpan.parentId(), mockSpan);
        }
    }
    Assert.assertEquals(numberOfCalls, parentSpans.size());
    Assert.assertEquals(numberOfCalls, childSpans.size());
    for (MockSpan parentSpan : parentSpans) {
        MockSpan childSpan = childSpans.get(parentSpan.context().spanId());
        Assert.assertEquals(parentSpan.tags().get("request-url"), childSpan.tags().get(Tags.HTTP_URL.getKey()));
        Assert.assertEquals(parentSpan.context().traceId(), childSpan.context().traceId());
        Assert.assertEquals(parentSpan.context().spanId(), childSpan.parentId());
        Assert.assertEquals(0, childSpan.generatedErrors().size());
        Assert.assertEquals(0, parentSpan.generatedErrors().size());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MockSpan(io.opentracing.mock.MockSpan) Span(io.opentracing.Span) Scope(io.opentracing.Scope) Contains(org.mockito.internal.matchers.Contains) ExecutorService(java.util.concurrent.ExecutorService) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) Future(java.util.concurrent.Future) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 80 with Scope

use of io.opentracing.Scope in project keywhiz by square.

the class Tracing method trace.

public static <T> T trace(String spanName, Supplier<T> delegate) {
    Tracer tracer = GlobalTracer.get();
    Span span = tracer.buildSpan(spanName).start();
    try {
        try (Scope ignored = tracer.scopeManager().activate(span)) {
            return delegate.get();
        } catch (Exception e) {
            tagErrors(span, e);
            throw e;
        }
    } finally {
        span.finish();
    }
}
Also used : Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) Span(io.opentracing.Span) IOException(java.io.IOException)

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