Search in sources :

Example 1 with SpanWrapper

use of io.smallrye.opentracing.contrib.jaxrs2.internal.SpanWrapper in project quarkus by quarkusio.

the class QuarkusSmallRyeTracingStandaloneContainerResponseFilter method filter.

// this needs to be executed after ServerTracingFilter
@ServerResponseFilter(priority = Priorities.HEADER_DECORATOR - 1)
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext, Throwable t) {
    Object wrapperObj = requestContext.getProperty(SpanWrapper.PROPERTY_NAME);
    if (!(wrapperObj instanceof SpanWrapper)) {
        return;
    }
    SpanWrapper wrapper = (SpanWrapper) wrapperObj;
    Tags.HTTP_STATUS.set(wrapper.get(), responseContext.getStatus());
    if (t != null) {
        FilterUtil.addExceptionLogs(wrapper.get(), t);
    }
}
Also used : SpanWrapper(io.smallrye.opentracing.contrib.jaxrs2.internal.SpanWrapper) ServerResponseFilter(org.jboss.resteasy.reactive.server.ServerResponseFilter)

Example 2 with SpanWrapper

use of io.smallrye.opentracing.contrib.jaxrs2.internal.SpanWrapper in project legend-shared by finos.

the class TracingInterceptor method buildSpan.

private Span buildSpan(InterceptorContext context, String operationName) {
    final SpanWrapper spanWrapper = findSpan(context);
    if (spanWrapper == null) {
        return NoopSpan.INSTANCE;
    }
    final Tracer.SpanBuilder spanBuilder = tracer.buildSpan(operationName);
    if (spanWrapper.isFinished()) {
        spanBuilder.addReference(References.FOLLOWS_FROM, spanWrapper.get().context());
    } else {
        spanBuilder.asChildOf(spanWrapper.get());
    }
    return spanBuilder.start();
}
Also used : SpanWrapper(io.opentracing.contrib.jaxrs2.internal.SpanWrapper) Tracer(io.opentracing.Tracer)

Example 3 with SpanWrapper

use of io.smallrye.opentracing.contrib.jaxrs2.internal.SpanWrapper in project legend-shared by finos.

the class OpenTracingFilterTest method filterExceptionTest.

@Test
public void filterExceptionTest() throws IOException, ServletException {
    HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    HttpServletRequest httpRequest = mock(HttpServletRequest.class);
    FilterChain chain = mock(FilterChain.class);
    when(httpRequest.getHeaderNames()).thenReturn(Collections.emptyEnumeration());
    MockTracer tracer = new MockTracer();
    ServletException exception = new ServletException("Stuff went wrong");
    doThrow(exception).when(chain).doFilter(httpRequest, httpResponse);
    OpenTracingFilter filter = new OpenTracingFilter(tracer);
    try {
        filter.doFilter(httpRequest, httpResponse, chain);
    } catch (ServletException ignored) {
    }
    verify(chain).doFilter(httpRequest, httpResponse);
    Assert.assertEquals(1, tracer.finishedSpans().size());
    MockSpan span = tracer.finishedSpans().get(0);
    ArgumentCaptor<SpanWrapper> spanCaptor = ArgumentCaptor.forClass(SpanWrapper.class);
    verify(httpRequest).setAttribute(eq(PROPERTY_NAME), spanCaptor.capture());
    Assert.assertEquals(span, spanCaptor.getValue().get());
    Assert.assertEquals(Boolean.TRUE, span.tags().get("error"));
    Assert.assertEquals(1, span.logEntries().size());
    MockSpan.LogEntry logEntry = span.logEntries().get(0);
    Assert.assertEquals(exception, logEntry.fields().get("error.object"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SpanWrapper(io.opentracing.contrib.jaxrs2.internal.SpanWrapper) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockTracer(io.opentracing.mock.MockTracer) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 4 with SpanWrapper

use of io.smallrye.opentracing.contrib.jaxrs2.internal.SpanWrapper in project legend-shared by finos.

the class OpenTracingFilterTest method filterTest.

@Test
public void filterTest() throws IOException, ServletException {
    HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    HttpServletRequest httpRequest = mock(HttpServletRequest.class);
    FilterChain chain = mock(FilterChain.class);
    when(httpRequest.getHeaderNames()).thenReturn(Collections.emptyEnumeration());
    MockTracer tracer = new MockTracer();
    OpenTracingFilter filter = new OpenTracingFilter(tracer);
    filter.doFilter(httpRequest, httpResponse, chain);
    verify(chain).doFilter(httpRequest, httpResponse);
    Assert.assertEquals(1, tracer.finishedSpans().size());
    MockSpan span = tracer.finishedSpans().get(0);
    ArgumentCaptor<SpanWrapper> spanCaptor = ArgumentCaptor.forClass(SpanWrapper.class);
    verify(httpRequest).setAttribute(eq(PROPERTY_NAME), spanCaptor.capture());
    Assert.assertEquals(span, spanCaptor.getValue().get());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SpanWrapper(io.opentracing.contrib.jaxrs2.internal.SpanWrapper) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockTracer(io.opentracing.mock.MockTracer) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.Test)

Example 5 with SpanWrapper

use of io.smallrye.opentracing.contrib.jaxrs2.internal.SpanWrapper in project legend-shared by finos.

the class OpenTracingFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String uri = httpRequest.getRequestURI();
    if (this.skipUrls.contains(uri)) {
        chain.doFilter(request, response);
        return;
    }
    Tracer.SpanBuilder spanBuilder = this.tracer.buildSpan(httpRequest.getPathInfo()).ignoreActiveSpan().withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);
    MultivaluedHashMap<String, String> headerMap = new MultivaluedHashMap<>();
    Collections.list(httpRequest.getHeaderNames()).forEach(header -> headerMap.put(header, Collections.list(httpRequest.getHeaders(header))));
    spanBuilder.asChildOf(this.tracer.extract(Format.Builtin.HTTP_HEADERS, new ServerHeadersExtractTextMap(headerMap)));
    try (Scope scope = spanBuilder.startActive(false)) {
        Span span = scope.span();
        try {
            // Update request
            try {
                this.spanDecorators.forEach(d -> d.decorateRequest(httpRequest, span));
                httpRequest.setAttribute(SCOPE_PROPERTY, scope);
                httpRequest.setAttribute(SpanWrapper.PROPERTY_NAME, new SpanWrapper(span, scope));
                Map<String, String> props = new HashMap<>();
                this.tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMapAdapter(props));
                props.forEach(httpResponse::addHeader);
            } catch (Throwable t) {
                LOGGER.warn("Error updating request (trace id: {})", span.context().toTraceId(), t);
                throw t;
            }
            // Handle request
            chain.doFilter(request, response);
            // Update response
            try {
                this.spanDecorators.forEach(d -> d.decorateResponse(httpResponse, span));
            } catch (Throwable t) {
                LOGGER.warn("Error updating response (trace id: {})", span.context().toTraceId(), t);
                throw t;
            }
        } catch (Throwable t) {
            Tags.HTTP_STATUS.set(span, httpResponse.getStatus());
            addExceptionLogs(span, t);
            throw t;
        } finally {
            if (request.isAsyncStarted()) {
                LOGGER.debug("Async request, not finishing the span now (trace id: {})", span.context().toTraceId());
                request.getAsyncContext().addListener(new SpanFinisher(span), request, response);
            } else {
                span.finish();
            }
        }
    }
    // Check if there is a lingering active span
    Span activeSpan = this.tracer.activeSpan();
    if (activeSpan != null) {
        LOGGER.error("There is still an open ActiveTracing span (trace id: {}). This probably means a scope is unclosed.", activeSpan.context().toTraceId());
        try {
            ACTIVE_SPAN_EXCEPTION.inc();
        } catch (Exception e) {
            LOGGER.error("Failed to update activeSpanException gauge", e);
        }
    }
}
Also used : SpanWrapper(io.opentracing.contrib.jaxrs2.internal.SpanWrapper) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Tracer(io.opentracing.Tracer) HttpServletResponse(javax.servlet.http.HttpServletResponse) TextMapAdapter(io.opentracing.propagation.TextMapAdapter) Span(io.opentracing.Span) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Scope(io.opentracing.Scope) ServerHeadersExtractTextMap(io.opentracing.contrib.jaxrs2.server.ServerHeadersExtractTextMap)

Aggregations

SpanWrapper (io.opentracing.contrib.jaxrs2.internal.SpanWrapper)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Tracer (io.opentracing.Tracer)2 MockSpan (io.opentracing.mock.MockSpan)2 MockTracer (io.opentracing.mock.MockTracer)2 SpanWrapper (io.smallrye.opentracing.contrib.jaxrs2.internal.SpanWrapper)2 FilterChain (javax.servlet.FilterChain)2 ServletException (javax.servlet.ServletException)2 Test (org.junit.Test)2 Scope (io.opentracing.Scope)1 Span (io.opentracing.Span)1 ServerHeadersExtractTextMap (io.opentracing.contrib.jaxrs2.server.ServerHeadersExtractTextMap)1 TextMapAdapter (io.opentracing.propagation.TextMapAdapter)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 ServerResponseFilter (org.jboss.resteasy.reactive.server.ServerResponseFilter)1