Search in sources :

Example 26 with Span

use of io.opentracing.Span 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 27 with Span

use of io.opentracing.Span 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)

Example 28 with Span

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

the class TestController method localSpan.

@RequestMapping("/localSpan")
public String localSpan(HttpServletRequest request) {
    verifyActiveSpan();
    io.opentracing.Tracer.SpanBuilder spanBuilder = tracer.buildSpan("localSpan");
    Span localSpan = spanBuilder.startManual();
    localSpan.finish();
    return "sync";
}
Also used : Tracer(io.opentracing.Tracer) Span(io.opentracing.Span) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with Span

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

the class RemoteEjbClientIT method executeRemoteEjbMethodIT.

@Test
public void executeRemoteEjbMethodIT() throws NamingException {
    Properties contextProperties = new Properties();
    contextProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
    contextProperties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
    contextProperties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
    Context context = new InitialContext(contextProperties);
    EjbRemote ejb = (EjbRemote) context.lookup(String.format("java:global%sEjb", uri.getPath()));
    Tracer tracer = GlobalTracer.get();
    Span span = null;
    try {
        span = tracer.buildSpan("ExecuteEjb").start();
        tracer.activateSpan(span);
        span.setBaggageItem("Wibbles", "Wobbles");
        String baggageItems = ejb.annotatedMethod();
        Assert.assertTrue("Baggage items didn't match, received: " + baggageItems, baggageItems.contains("\nWibbles : Wobbles\n"));
        span.setBaggageItem("Nibbles", "Nobbles");
        baggageItems = ejb.nonAnnotatedMethod();
        Assert.assertTrue("Baggage items didn't match, received: " + baggageItems, baggageItems.contains("Wibbles : Wobbles") && baggageItems.contains("Nibbles : Nobbles"));
        span.setBaggageItem("Bibbles", "Bobbles");
        baggageItems = ejb.shouldNotBeTraced();
        Assert.assertTrue("Baggage items didn't match, received: " + baggageItems, baggageItems.contains("Wibbles : Wobbles") && baggageItems.contains("Nibbles : Nobbles") && baggageItems.contains("Bibbles : Bobbles"));
        baggageItems = ejb.editBaggageItems();
        Assert.assertTrue("Baggage items didn't match, received: " + baggageItems, baggageItems.contains("Wibbles : Wabbles") && baggageItems.contains("Nibbles : Nabbles") && baggageItems.contains("Bibbles : Babbles"));
    } finally {
        if (span != null) {
            span.finish();
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) Properties(java.util.Properties) Span(io.opentracing.Span) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 30 with Span

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

the class Ejb method editBaggageItems.

@Override
public String editBaggageItems() {
    randomSleep();
    Span activeSpan = tracer.activeSpan();
    if (activeSpan != null) {
        activeSpan.setBaggageItem("Wibbles", "Wabbles");
        activeSpan.setBaggageItem("Nibbles", "Nabbles");
        activeSpan.setBaggageItem("Bibbles", "Babbles");
        return getBaggageItems(activeSpan);
    } else {
        return "Nothing found!";
    }
}
Also used : Span(io.opentracing.Span)

Aggregations

Span (io.opentracing.Span)368 Future (io.vertx.core.Future)182 HttpURLConnection (java.net.HttpURLConnection)174 Tracer (io.opentracing.Tracer)123 JsonObject (io.vertx.core.json.JsonObject)119 ClientErrorException (org.eclipse.hono.client.ClientErrorException)117 Map (java.util.Map)115 SpanContext (io.opentracing.SpanContext)114 Objects (java.util.Objects)109 List (java.util.List)104 TracingHelper (org.eclipse.hono.tracing.TracingHelper)104 Optional (java.util.Optional)102 Promise (io.vertx.core.Promise)98 Test (org.junit.jupiter.api.Test)96 MessageHelper (org.eclipse.hono.util.MessageHelper)89 Vertx (io.vertx.core.Vertx)84 Constants (org.eclipse.hono.util.Constants)82 ServerErrorException (org.eclipse.hono.client.ServerErrorException)80 Truth.assertThat (com.google.common.truth.Truth.assertThat)79 Mockito.mock (org.mockito.Mockito.mock)79