-
Notifications
You must be signed in to change notification settings - Fork 38k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trace lost in error log #30675
Comments
What do you mean? What were you using for tracing support? Spring Cloud Sleuth? The repository is empty. |
I'm using Micrometer for tracing support (Spring Boot 3)
Sorry, failed to push just now, now pushed. Please see again https://github.com/wapkch/web-tracing-log-demo |
After looking into it, i found that it was related to the When using Spring Boot 2 with Spring Cloud Sleuth, DispatcherType.FORWARD is included in the DispatcherTypes: @Bean
FilterRegistrationBean traceWebFilter(BeanFactory beanFactory, SleuthWebProperties webProperties) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new LazyTracingFilter(beanFactory));
filterRegistrationBean.setDispatcherTypes(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.FORWARD,
DispatcherType.INCLUDE, DispatcherType.REQUEST);
filterRegistrationBean.setOrder(webProperties.getFilterOrder());
return filterRegistrationBean;
} When using Spring Boot 3 with Micrometer Tracing, DispatcherType.FORWARD is not included in the DispatcherTypes: @Bean
@ConditionalOnMissingFilterBean
public FilterRegistrationBean<ServerHttpObservationFilter> webMvcObservationFilter(ObservationRegistry registry,
ObjectProvider<ServerRequestObservationConvention> customConvention,
ObjectProvider<WebMvcTagsProvider> customTagsProvider,
ObjectProvider<WebMvcTagsContributor> contributorsProvider) {
String name = httpRequestsMetricName(this.observationProperties, this.metricsProperties);
ServerRequestObservationConvention convention = createConvention(customConvention.getIfAvailable(), name,
customTagsProvider.getIfAvailable(), contributorsProvider.orderedStream().toList());
ServerHttpObservationFilter filter = new ServerHttpObservationFilter(registry, convention);
FilterRegistrationBean<ServerHttpObservationFilter> registration = new FilterRegistrationBean<>(filter);
registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);
return registration;
} So, i think it should be an issue of Spring Boot instead of Spring Framework, feel free to close this issue. |
Before moving this issue to Spring Boot, I’d like to ensure that using other dispatcher types (error or include) would work as expected with the filter. The filter is a « once per request filter » now and I’m not sure this fits here. |
You're right, using other dispatcher types still don't work cause it's a OncePerRequestFilter now. But add a tomcat TraceValve(Like Spring Cloud Sleuth instrument does) will make it work: public class TraceValve extends ValveBase {
private final ObservationRegistry observationRegistry;
public TraceValve(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
Observation observation = Observation
.createNotStarted(TraceValve.class.getName(), observationRegistry).start();
try (Observation.Scope scope = observation.openScope()) {
Valve next = getNext();
if (null == next) {
// no next valve
return;
}
next.invoke(request, response);
} catch (Exception exception) {
observation.error(exception);
throw exception;
} finally {
// Only stop Observation if async processing is done or has never been started.
if (!request.isAsyncStarted()) {
Throwable error = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
if (error != null) {
observation.error(error);
}
observation.stop();
}
}
}
} I plan to add this to my project, but i'm not sure whether this should be added to apache tomcat. |
The Observation Servlet Filter is quite similar to what was done in Spring Boot 2.x with Metrics. I don't see a way to map it to other dispatch types without running into issues: recording the same observation multiple times, leaving observations opened, etc. Implementing a Tomcat Valve is not acceptable from a Spring Framework perspective, as we operate at the Servlet level for MVC applications. We would have to do the same for all containers and all supported versions. I'm closing this issue as a duplicate of #29398. |
@wapkch how did you solve this problem in the end? |
|
Affects: 6.0.9
Let's say we have the following code:
application.properties:
http:localhost:8080/hello
The output log is as follows:
We can find the trace exists in the first line, but lost in the second line error log. This is different from spring 5.x's behavior, which trace won't be lost in this error log.
Sample:
https://github.com/wapkch/web-tracing-log-demo
The text was updated successfully, but these errors were encountered: