Skip to content
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

Document limitations of Servlet Filter observations #29398

Closed
jonatan-ivanov opened this issue Oct 28, 2022 · 7 comments
Closed

Document limitations of Servlet Filter observations #29398

jonatan-ivanov opened this issue Oct 28, 2022 · 7 comments
Assignees
Labels
theme: observability An issue related to observability and tracing type: documentation A documentation task
Milestone

Comments

@jonatan-ivanov
Copy link
Member

Affects: 6.0.0-SNAPSHOT

If micrometer-observation is set-up and if I throw an exception from a controller:

@GetMapping("/trouble")
String trouble() {
    LOGGER.info("3,2,1... Boom!");
    throw new IllegalArgumentException("Noooooo!");
}

The ERROR log entry does not contain the traceID/spanId, see [boot3-sample,,] (log correlation does not work):

2022-10-28T12:37:16.647-07:00  INFO [boot3-sample,635c2f6c8cb895fa15f922f7cacc6954,15f922f7cacc6954] 96088 --- [nio-8080-exec-1] i.m.boot3.samples.SampleController       : 3,2,1... Boom!
2022-10-28T12:37:16.660-07:00 ERROR [boot3-sample,,] 96088 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentException: Noooooo!] with root cause

java.lang.IllegalArgumentException: Noooooo!
...

I think the issue is either the error logging is not "observed" or it is not "in-Scope".

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 28, 2022
@bclozel
Copy link
Member

bclozel commented Oct 31, 2022

Hey @jonatan-ivanov

From what I understand, this is being logged from an org.apache.catalina class? In this case, it's called outside of the observation scope of the Servlet filter so it's expected at this stage not to have the log correlation anymore.

I don't think Framework can do anything about that.

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Oct 31, 2022
@jonatan-ivanov
Copy link
Member Author

Yes, it is definitely out of the scope of the Observation.
I'm not sure if it is purely Catalina or a component that we provide to Catalina (dispatcherServlet in the name of the logger).

@marcingrzejszczak might have more details, in Sleuth, we fixed it with a Valve: TraceValve

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 31, 2022
@bclozel
Copy link
Member

bclozel commented Oct 31, 2022

I'm not sure if it is purely Catalina or a component that we provide to Catalina (dispatcherServlet in the name of the logger).

I guess Tomcat is logging the name of the Servlet.

I did discuss the Valve possibility with Marcin, and while it would bring better measurements in general, I don't think it's the right approach for Framework:

  • we don't ship container specific API and we usually stick to the Servlet API
  • we would need to implement the same feature for Jetty, Undertow and others - containers would need to offer the same features and apply it at the same phase during the request lifecycle

Taking a step back, I think the Servlet filter is enough to instrument the application behavior.

@jonatan-ivanov
Copy link
Member Author

I think having traceId/spanId on error logs is a vital feature.
Errors are the events where users most likely want to look into, with missing traceId/spanId they are unable to do so out of the box.

@bclozel
Copy link
Member

bclozel commented Nov 2, 2022

I think that the main issue here is that exceptions unhandled by the web framework are not logged by Spring and bubble up to the Servlet container, which chooses to log them at the ERROR level. If Spring web frameworks logged those exceptions, this user experience issue would be solved. Considering a custom valve here would only work for Tomcat and would not address the issues listed in my previous comment.

DispatcherServlet is not logging those at the moment, maybe a dedicated logger and/or a configuration setting would help. This is a fundamental part of the logging experience, and it's been like this for a very long time so changing this behavior is likely to bother a lot of Spring developers out there.

Note that exceptions handled by Spring MVC are logged by the Framework, not by the container. For example here, using the new Problem Detail support (this would also work for any exception handler in general).

@Controller
public class SomeController {

	private static final Log logger = LogFactory.getLog(SomeController.class);

	@GetMapping("/test")
	public String test() {
		logger.info("test() is called");
		throw new IllegalStateException("the request has an invalid state");
	}

	@ExceptionHandler(IllegalStateException.class)
	ProblemDetail handleIllegalState(IllegalStateException exc) {
		ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
		problemDetail.setTitle("Illegal State");
		return problemDetail;
	}

}
$ http localhost:8080/test

HTTP/1.1 400
Connection: close
Content-Type: application/problem+json
Transfer-Encoding: chunked

{
    "instance": "/test",
    "status": 400,
    "title": "Illegal State",
    "type": "about:blank"
}
INFO [,ec159f78ffd24972f83a88a093fd16dc,b23d770731d64cb8]  28380 --- [nio-8080-exec-1] com.example.error.SomeController         : test() is called
WARN [,ec159f78ffd24972f83a88a093fd16dc,b23d770731d64cb8]  28380 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.IllegalStateException: the request has an invalid state]

@jonatan-ivanov
Copy link
Member Author

jonatan-ivanov commented Nov 4, 2022

I only used the valve as an example (Tomcat), I'm not saying we should use it or that would be a universal solution. I'm only saying that out of the box this does not work and this is a rather important functionality.

I guess the question for me is: can we do anything with this in the 6.0 timeline or in 6.1? I.e.: brainstorming about it with the whole team?

If this can't be fixed in 6.0, can we document it? I guess other options should work too, like having a ControllerAdvice or using a custom ObservationHandler.

@bclozel bclozel added type: documentation A documentation task theme: observability An issue related to observability and tracing and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Jan 30, 2023
@bclozel bclozel added this to the 6.0.x milestone Jan 30, 2023
@bclozel
Copy link
Member

bclozel commented Jan 30, 2023

I think we need to document this as a known behavior in our web observability support documentation. Since the instrumentation is done at the Servlet filter level, we can't extend the observation outside of this scope. Applications and libraries can write container-specific instrumentation with broader support but this won't be supported at the Spring Framework level.

alexandreroman added a commit to alexandreroman/tap-otel-demo that referenced this issue Feb 3, 2023
This is a workaround for missing log correlation
(see details in spring-projects/spring-framework#29398).
@bclozel bclozel modified the milestones: 6.0.x, 6.1.x Jul 4, 2023
@bclozel bclozel self-assigned this Jul 4, 2023
@bclozel bclozel changed the title Missing log correlation for controller errors Document limitations of Servlet Filter observations Jul 6, 2023
@bclozel bclozel modified the milestones: 6.1.x, 6.0.11 Jul 6, 2023
@bclozel bclozel closed this as completed in 64e04b7 Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: observability An issue related to observability and tracing type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants