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);
}
}
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();
}
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"));
}
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());
}
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);
}
}
}
Aggregations