spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Http server metrics counts twice for Jersey endpoints

Open Declow0 opened this issue 3 years ago • 7 comments

After upgrading to Spring Boot 2.6 with Micrometer 1.8.1 metric "http_server_requests_seconds_count" counts twice every jersey call with different uri tag: correct uri by org.springframework.boot.actuate.autoconfigure.metrics.jersey.JerseyServerMetricsAutoConfiguration UNKNOWN by org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsAutoConfiguration in that order (by filter priority)

jersey-metrics

Simple project for problem demonstration https://github.com/Declow0/jersey-metrics

Can WebMvcMetricsFilter ignore request for counting if Jersey implemetation was already counted?

Declow0 avatar Jan 10 '22 18:01 Declow0

Here's a Java version of the sample: https://github.com/philwebb/scratch-gh-29303

philwebb avatar Jan 10 '22 19:01 philwebb

@Declow0 What version are you upgrading from? I tried 2.5 and I think I get the same result. Do you need to mix Spring MVC and Jersey in your application?

philwebb avatar Jan 10 '22 19:01 philwebb

I upgraded from 2.3 to 2.6. Yes, I mix REST Controllers and Jersey endpoints (from Camunda project). At 2.3 I don't have Jersey metrics autoconfiguration and all Jersey call has uri tag = UNKNOWN

Declow0 avatar Jan 10 '22 19:01 Declow0

I'd like to discuss this one with the rest of the team to see if they have any ideas. I wonder if we might be able to tighten WebMvcMetricsFilter and only apply it if the request has been handled by a DispatcherServlet. Perhaps we can check for a DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE in the request.

philwebb avatar Jan 10 '22 20:01 philwebb

See also #13064

philwebb avatar Jan 12 '22 16:01 philwebb

We're going to look at adding a property that will allow WebMvcMetricsFilter to check if the request is from a DispatcherServlet. Since existing users might be relying on the existing behavior, we won't flip the default until Spring Boot 2.7.

philwebb avatar Jan 12 '22 16:01 philwebb

We should also check what happens if Jersey's configured as a filter and with FILTER_FORWARD_ON_404. There's a chance that this will result in duplicate metrics as the request will have passed through Jersey and MVC's DispatcherServlet.

wilkinsona avatar Jan 13 '22 09:01 wilkinsona

I upgraded from 2.3 to 2.6. Yes, I mix REST Controllers and Jersey endpoints (from Camunda project). At 2.3 I don't have Jersey metrics autoconfiguration and all Jersey call has uri tag = UNKNOWN

Also facing same problem URI tag remain either UNKNOWN or /** instead actual endpoint urls? Any idea what might be wrong?

anand188 avatar Apr 09 '24 05:04 anand188

In the meantime, the observability support has been rewritten entirely and Jersey/MVC instrumentations are independent. Jersey apps should not depend on the web starter and as a result, the web layer will not be instrumented twice. Closing with https://github.com/spring-projects/spring-boot/issues/39294#issuecomment-1948529675

bclozel avatar Apr 15 '24 08:04 bclozel

hi i have exactly same issue but for all count not only http, but database call, webclient calls etc all are counted twice

cyril-telr avatar May 20 '24 08:05 cyril-telr

@cyril-telr in that case, it's not exactly the same issue. The problem that was tracked by this issue was specific to Jersey and how the web stack was instrumented. If you would like us to spend some time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can then share it with us by opening a new issue and pushing it to a separate repository on GitHub or by zipping it up and attaching it to the new issue.

wilkinsona avatar May 20 '24 09:05 wilkinsona