How to output traceId in WebFilter log?
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?
Which version of sleuth are you using? This is a pretty basic feature that does work
Which version of sleuth are you using? This is a pretty basic feature that does work
Thanks Reply. I use version 2.1.0.
Please try with the latest version 2.2.3.RELEASE
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
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());
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.
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()
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.
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.
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.