spring-cloud-sleuth icon indicating copy to clipboard operation
spring-cloud-sleuth copied to clipboard

How to output traceId in WebFilter log?

Open Duelist-Fiora opened this issue 6 years ago • 10 comments

I want to authenticate in WebFilter, but Sleuth can't output traceId in WebFilter, which makes me very upset, I checked #1529 and #1589. Code like this didn't solve my problem return webFilterChain.filter(serverWebExchange).doOnNext(log.info(...));. Is Sleuth not yet supporting this feature?

Duelist-Fiora avatar Jun 11 '20 09:06 Duelist-Fiora

Which version of sleuth are you using? This is a pretty basic feature that does work

marcingrzejszczak avatar Jun 15 '20 06:06 marcingrzejszczak

Which version of sleuth are you using? This is a pretty basic feature that does work

Thanks Reply. I use version 2.1.0.

Duelist-Fiora avatar Jun 15 '20 07:06 Duelist-Fiora

Please try with the latest version 2.2.3.RELEASE

marcingrzejszczak avatar Jun 15 '20 07:06 marcingrzejszczak

Please try with the latest version 2.2.3.RELEASE

Confusingly, the latest version still does not work. code:

@Component
public class BlueApiFilter implements WebFilter {
    private static final Logger LOG = LoggerFactory.getLogger(BlueApiFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        // I want to authenticate here, so I need to output traceId
        LOG.info("url:{}, method:{}", exchange.getRequest().getURI(), exchange.getRequest().getMethod());
        return chain.filter(exchange);
    }
}

log:

2020-06-15 16:02:44.745  INFO [blue-api,,,] 27165 --- [reactor-http-epoll-3] c.blue.api.filter.BlueApiFilter   : url:http://host:port/path, method:GET
2020-06-15 16:02:44.769 DEBUG [blue-api,6bef5f2766c58391,6bef5f2766c58391,false] 27165 --- [reactor-http-epoll-3] c.j.b.api.controller.Controller   : other information

WebFilter logs do not have traceId

Duelist-Fiora avatar Jun 15 '20 08:06 Duelist-Fiora

After my testing, the following code can run normally in Controller, but in WebFilter you will get NullPointerException (tracer.currentSpan() is null). How to make Sleuth complete initialization before executing WebFliter?

@Autowired
private brave.Tracer tracer;

LOG.debug("traceId: {}", tracer.currentSpan().context().traceIdString());

Duelist-Fiora avatar Jun 18 '20 06:06 Duelist-Fiora

But you're not in a flux at that point so why would there be any trace there? We put the tracing information inside the flux not in the webfilter as such. If you did doOnNext(...) and then called the line then you should get the span.

marcingrzejszczak avatar Jun 18 '20 06:06 marcingrzejszczak

But you're not in a flux at that point so why would there be any trace there? We put the tracing information inside the flux not in the webfilter as such. If you did doOnNext(...) and then called the line then you should get the span.

Maybe my level is not enough, I have made many attempts and log Mono, I still cannot trigger Mono's doOnNext callback, can you provide a more complete example? Thank you very much.

Code:

@Component
public class BlueApiFilter implements WebFilter {
    private static final Logger LOG = LoggerFactory.getLogger(BlueApiFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        LOG.info("url:{},method:{}", exchange.getRequest().getURI(), exchange.getRequest().getMethod());
        return chain.filter(exchange).log("logger");
    }
}

log:

2020-06-20 15:52:23.282  INFO [blue-api,,,] 6724 --- [ctor-http-nio-2] blue.api.filter.BlueApiFilter   : url:http://127.0.0.1:8861/test,method:GET
2020-06-20 15:52:23.286  INFO [blue-api,,,] 6724 --- [ctor-http-nio-2] logger                                   : onSubscribe([Fuseable] MonoFlatMap.FlatMapMain)
2020-06-20 15:52:23.287  INFO [blue-api,,,] 6724 --- [ctor-http-nio-2] logger                                   : request(unbounded)
2020-06-20 15:52:23.300  INFO [blue-api,,,] 6724 --- [ctor-http-nio-2] logger                                   : onContextUpdate(Context1{class brave.Span=NoopSpan(f90047628d914f0f/f90047628d914f0f)})
2020-06-20 15:52:23.306  INFO [blue-api,,,] 6724 --- [ctor-http-nio-2] logger                                   : onContextUpdate(Context1{class brave.Span=NoopSpan(f90047628d914f0f/f90047628d914f0f)})
2020-06-20 15:52:23.307  INFO [blue-api,,,] 6724 --- [ctor-http-nio-2] logger                                   : onContextUpdate(Context1{class brave.Span=NoopSpan(f90047628d914f0f/f90047628d914f0f)})
2020-06-20 15:52:23.307  INFO [blue-api,,,] 6724 --- [ctor-http-nio-2] logger                                   : onContextUpdate(Context1{class brave.Span=NoopSpan(f90047628d914f0f/f90047628d914f0f)})
2020-06-20 15:52:23.309  INFO [blue-api,,,] 6724 --- [ctor-http-nio-2] logger                                   : onContextUpdate(Context1{class brave.Span=NoopSpan(f90047628d914f0f/f90047628d914f0f)})
2020-06-20 15:52:23.320  INFO [blue-api,f90047628d914f0f,f90047628d914f0f,false] 6724 --- [ctor-http-nio-2] logger                                   : onContextUpdate(Context1{class brave.Span=NoopSpan(f90047628d914f0f/f90047628d914f0f)})
2020-06-20 15:52:23.329 DEBUG [blue-api,f90047628d914f0f,f90047628d914f0f,false] 6724 --- [ctor-http-nio-2] log.aspect.LogAspect                     : HTTP request: {"url":"test","method":"blue.api.controller.FallbackController.test","httpMethod":"GET"}
2020-06-20 15:52:23.347 DEBUG [blue-api,f90047628d914f0f,f90047628d914f0f,false] 6724 --- [ctor-http-nio-2] log.aspect.LogAspect                     : HTTP response body: {"scanAvailable":true}
2020-06-20 15:52:23.349  INFO [blue-api,f90047628d914f0f,f90047628d914f0f,false] 6724 --- [ctor-http-nio-2] logger                                   : onComplete()

Duelist-Fiora avatar Jun 20 '20 08:06 Duelist-Fiora

With the latest Sleuth snapshots you can leverage the new manual reactor instrumentation mechanism where you should set the property spring.sleuth.reactor.instrumentation-type to manual and then change your code as follows:

@Component
public class BlueApiFilter implements WebFilter {
    private static final Logger LOG = LoggerFactory.getLogger(BlueApiFilter.class);

    private final Tracing tracing;

    // constructor injection

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
       WebFluxSleuthOperators.withSpanInScope(tracing, exchange, () ->  LOG.info("url:{},method:{}", exchange.getRequest().getURI(), exchange.getRequest().getMethod()));
        return chain.filter(exchange).log("logger");
    }
}

By manually controlling the way spans are put in scope you take full control of trace context passing and the performance will improve.

marcingrzejszczak avatar Jul 29 '20 15:07 marcingrzejszczak

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Aug 05 '20 16:08 spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-projects-issues avatar Aug 12 '20 15:08 spring-projects-issues