SpringCloud TracingFilter
2023-12-01 16:22 更新
您还可以修改TracingFilter
的行为,该行为负责处理输入的HTTP请求并基于HTTP响应添加标签。您可以通过注册自己的TracingFilter
bean实例来自定义标签或修改响应头。
在下面的示例中,我们注册TracingFilter
bean,添加包含当前Span的跟踪ID的ZIPKIN-TRACE-ID
响应标头,并添加带有键custom
和一个值的标签tag
到跨度。
@Component @Order(TraceWebServletAutoConfiguration.TRACING_FILTER_ORDER + 1) class MyFilter extends GenericFilterBean { private final Tracer tracer; MyFilter(Tracer tracer) { this.tracer = tracer; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Span currentSpan = this.tracer.currentSpan(); if (currentSpan == null) { chain.doFilter(request, response); return; } // for readability we're returning trace id in a hex form ((HttpServletResponse) response).addHeader("ZIPKIN-TRACE-ID", currentSpan.context().traceIdString()); // we can also add some custom tags currentSpan.tag("custom", "tag"); chain.doFilter(request, response); } }
以上内容是否对您有帮助:
更多建议: