smallrye-metrics
smallrye-metrics copied to clipboard
JAX-RS unmapped exception metric does not work
The assumption that the ContainerResponseFilter
is not called if an unmapped exception occurs is simple not true.
This depends on the used JAX-RS implementation because the JAX-RS spec does not specify behavior for it. See https://github.com/jakartaee/rest/blob/3.0.0/jaxrs-spec/src/main/asciidoc/chapters/filters_and_interceptors/_exceptions_filters_and_interceptors.adoc :
A response mapped from an exception MUST be processed using the filter chain and the interceptor chain (if an entity is present in the mapped response).
So if there is no entity, the filter does not have to be called.
What implementation and version are we talking about here?
I think we could make it work in both cases by updating the JaxRsMetricsFilter.filter(requestContext, responseContext)
method and checking it to see if there was an exception, in which case we wouldn't set the smallrye.metrics.jaxrs.successful
property on the request context, but I'm not completely sure we have a reliable way to tell if an exception is mapped or not. I'm not that familar with JAX-RS. Perhaps you have an idea how to do it reliably (and with any JAX-RS implementation, whether the filter is invoked or not), in which case a PR would be welcome.
We use Resteasy and from my experience the JAX-RS filters are always called if the resource method is called. Resteasy has 'unhandled' exceptions which would increment this metric, but I couldn't figure out how to trigger it. Maybe some internal Resteasy error. There is no way we can reliably say that exception is unmapped. What we basically want here is if the request returned a 500 status code or not. So just check in the servlet filter the status code and increment this metric if its a 500. Then maybe we need to rename the metric because a mapped exception could return a 500 too.