in geronimo-opentracing-common/src/main/java/org/apache/geronimo/microprofile/opentracing/common/microprofile/server/OpenTracingFilter.java [118:205]
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
throws IOException, ServletException {
if (!HttpServletRequest.class.isInstance(request)) {
chain.doFilter(request, response);
return;
}
if (forcedUrls != null && !forcedUrls.isEmpty()) {
final HttpServletRequest req = HttpServletRequest.class.cast(request);
final String matching = req.getRequestURI().substring(req.getContextPath().length());
if (forcedUrls.stream().anyMatch(p -> p.test(matching))) {
final Tracer.SpanBuilder builder = tracer.buildSpan(buildServletOperationName(req));
builder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);
builder.withTag("component", "servlet");
ofNullable(ofNullable(tracer.activeSpan()).map(Span::context)
.orElseGet(() -> tracer.extract(Format.Builtin.HTTP_HEADERS,
new ServletHeaderTextMap(req, HttpServletResponse.class.cast(response)))))
.ifPresent(builder::asChildOf);
final Scope scope = builder.startActive(true);
final Span span = scope.span();
if (!skipDefaultTags) {
Tags.HTTP_METHOD.set(span, req.getMethod());
Tags.HTTP_URL.set(span, req.getRequestURL().toString());
}
request.setAttribute(OpenTracingFilter.class.getName(), scope);
}
}
if (skipUrls != null && !skipUrls.isEmpty()) {
final HttpServletRequest req = HttpServletRequest.class.cast(request);
final String matching = req.getRequestURI().substring(req.getContextPath().length());
if (skipUrls.stream().anyMatch(p -> p.test(matching))) {
chain.doFilter(request, response);
return;
}
}
try {
chain.doFilter(request, response);
} catch (final Exception ex) {
getCurrentScope(request).ifPresent(scope -> onError(response, ex, scope));
throw ex;
} finally {
getCurrentScope(request).ifPresent(scope -> {
if (request.isAsyncStarted()) {
request.getAsyncContext().addListener(new AsyncListener() {
@Override
public void onComplete(final AsyncEvent event) {
scope.close();
}
@Override
public void onTimeout(final AsyncEvent event) {
OpenTracingFilter.this.onError(
event.getSuppliedResponse(),
ofNullable(event.getThrowable()).orElseGet(TimeoutException::new),
scope);
}
@Override
public void onError(final AsyncEvent event) {
OpenTracingFilter.this.onError(event.getSuppliedResponse(), event.getThrowable(), scope);
}
@Override
public void onStartAsync(final AsyncEvent event) {
// no-op
}
});
ScopeManager managerImpl = manager;
if (!ScopeManagerImpl.class.isInstance(managerImpl) && Proxy.isProxyClass(manager.getClass())) {
final InvocationHandler handler = Proxy.getInvocationHandler(manager);
if (Container.Unwrappable.class.isInstance(handler)) {
managerImpl = ScopeManager.class.cast(Container.Unwrappable.class.cast(handler).unwrap());
}
}
if (ScopeManagerImpl.class.isInstance(managerImpl)) {
ScopeManagerImpl.class.cast(managerImpl).clear();
}
} else {
scope.close();
}
});
}
}