Server metrics are tagged with the wrong status codes
See original GitHub issueThe context
I’d like to get the metric values for grpc.server.processing.duration
and distinguish different status codes, e.g. OK, NOT_FOUND, INVALID_ARGUMENT, UNKNOWN
The bug
If my gRPC service is called three times, and it responds twice with an OK status and once with with an NOT_FOUND status, I’d expect the tags that are captured for the metrics grpc.server.processing.duration
to be “OK” and “NOT_FOUND”.
But instead, the “NOT_FOUND” error seems to be reported as an “UNKNOWN” error.
Stacktrace and logs
I don’t know
Steps to Reproduce
I created a very simple example project, with one gRPC service and one GrpcAdvice that translates my “business” exception into a NOT_FOUND status.
Please check out the integration test, that shows how to reproduce the issue: https://github.com/harti2006/grpc-metrics-demo/blob/trunk/src/test/java/com/github/harti2006/grpc/metrics/GrpcMetricsDemoApplicationTests.java
The application’s environment
Which versions do you use?
- Spring (boot): 2.7.5
- grpc-java: 1.51.0
- grpc-spring-boot-starter: 2.14.0.RELEASE
- java: 11
Additional context
- It used to work fine with the previous release
2.13.1.RELEASE
. When downgrading to this version, the above mentioned integration test works fine
Issue Analytics
- State:
- Created 10 months ago
- Comments:7
Yeah, I found these places while debugging the problem. And I saw that funny behavior: The responseCode is only updated in the close method, but that method was only called when the grpc handler method completed successfully. On errors, the close method wasn’t called at all, so the default value “UNKNOWN” was reported. Tomorrow, I will be able to add another case to my example to verify if this happens only when using the GrpcAdvice approach of sending errors, or if it’s a general problem.
Thanks for investigating. As promised I added another case to the example project where I send the exception directly via streamObserver:
I did the same test as before and with this approach, the metrics were reported correctly. See https://github.com/harti2006/grpc-metrics-demo/blob/933c29641a3c813db649cdd275ba022e08637820/src/test/java/com/github/harti2006/grpc/metrics/GrpcMetricsDemoApplicationTests.java#L72
I also tried your suggestion with changing the order of the
MetricCollectingServerInterceptor
bean, but it made things even worse: No metrics were reported at all.