简体   繁体   中英

injecting tracing headers in the response

I have added Sleuth integration to the application. Now the logs are showing traceId and spanId.

INFO [AppName,5dd62861a751e1b0,5dd62861a751e1b0,true]

I can send HTTP request with the open tracing headers

X-B3-TraceId
X-B3-SpanId

that are used by the tracer and I can see that in the logs. However I want to return them in the response.

I have the filter where I wanted to add those headers in the response

public class RequestResponseFilter implements Filter {

    private final Tracer tracer;
    private final Injector<HttpServletResponse> tracingInjector;
    
    public RequestResponseFilter(final Tracing tracing, final Tracer tracer) {
        this.tracer = tracer;
        this.tracingInjector = tracing.propagation().injector(HttpServletResponse::addHeader);
    }

    @Override
    public void doFilter(final ServletRequest request, final ServletResponse response,
        final FilterChain chain) throws IOException, ServletException {

        final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        final HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        ....

        // inject tracing header
        Span currentSpan = tracer.currentSpan();
        tracingInjector.inject(currentSpan.context(), httpServletResponse);

        chain.doFilter(httpServletRequest, httpServletResponse);
    }
}

But they are not added in a way I expected. In the response I see

b3: 5dd62861a751e1b0,5dd62861a751e1b0-1

What is wrong?

If you look at the documentation you have it written how to customize the HTTP server span - https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/howto.html#how-to-cutomize-http-server-spans since you have access to HttpServletResponse and the TraceContext you can retrieve the trace id / span id and put it in the response.

Sleuth 3.x

@Configuration(proxyBeanMethods = false)
public static class ServerParserConfiguration {

    @Bean(name = HttpServerRequestParser.NAME)
    HttpRequestParser myHttpRequestParser() {
        return (request, context, span) -> {
            // Span customization
            span.tag("ServerRequest", "Tag");
            Object unwrap = request.unwrap();
            if (unwrap instanceof HttpServletRequest) {
                HttpServletRequest req = (HttpServletRequest) unwrap;
                // Span customization
                span.tag("ServerRequestServlet", req.getMethod());
            }
        };
    }

    @Bean(name = HttpServerResponseParser.NAME)
    HttpResponseParser myHttpResponseParser() {
        return (response, context, span) -> {
            // Span customization
            span.tag("ServerResponse", "Tag");
            Object unwrap = response.unwrap();
            if (unwrap instanceof HttpServletResponse) {
                HttpServletResponse resp = (HttpServletResponse) unwrap;
                // Span customization
                span.tag("ServerResponseServlet", String.valueOf(resp.getStatus()));
            }
        };
    }

}

Sleuth 2.x (almost exactly the same) - https://docs.spring.io/spring-cloud-sleuth/docs/2.2.x/reference/html/#data-policy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM