// Prevent duplicate spans for the same request TraceContext context = (TraceContext) request.getAttribute(TraceContext.class.getName()); if (context != null) { // A forwarded request might end up on another thread, so make sure it is scoped Scope scope = currentTraceContext.maybeScope(context); try { chain.doFilter(request, response); } finally { scope.close(); } return; }
// Add attributes for explicit access to customization or span context request.setAttribute(SpanCustomizer.class.getName(), span.customizer()); request.setAttribute(TraceContext.class.getName(), span.context());
Throwable error = null; Scope scope = currentTraceContext.newScope(span.context()); try { // any downstream code can see Tracer.currentSpan() or use Tracer.currentSpanCustomizer() chain.doFilter(httpRequest, httpResponse); } catch (IOException | ServletException | RuntimeException | Error e) { error = e; throw e; } finally { scope.close(); if (servlet.isAsync(httpRequest)) { // we don't have the actual response, handle later servlet.handleAsync(handler, httpRequest, httpResponse, span); } else { // we have a synchronous response, so we can finish the span handler.handleSend(ADAPTER.adaptResponse(httpRequest, httpResponse), error, span); } } } // .. }