tonic
Pre v 0.1 I was able to intercept requests and measure the duration of request processing as shown below. Now svc is no longer a parameter in the interceptor and I was wondering if you might have any idea if there is currently a way to measure the duration in a central place.
let grpc_server = Server::builder()
// set up prometheus metrics by intercepting requests
.interceptor_fn(|svc, req| {
let monitoring_labels = &[&req.uri().to_string(), req.method().as_str()];
let timer = monitoring::GRPC_REQ_HISTOGRAM
.with_label_values(monitoring_labels)
.start_timer();
monitoring::GRPC_REQ_COUNTER
.with_label_values(monitoring_labels)
.inc();
let response = svc.call(req);
timer.observe_duration();
response
})
Not sure maybe there is someway via Tower for this?
@marcelbuesing so in 0.1 I decided to move interceptors to be a more general gRPC concept instead of being tied to the actual on the wire impl. That said, this is a great spot for something like tower to play a role. I have an implementation in the interop tests that show using a tower service to wrap a specific gRPC service. In this case it intercepts the headers and echos them in a warpped body. But the actual call fn is the same as you have above. In fact interceptor_fn was originally designed to fix this use case.
I would suggest looking here https://github.com/hyperium/tonic/blob/master/interop/src/server.rs#L169
I would also love to add an example showing how to do this.
Most helpful comment
@marcelbuesing so in 0.1 I decided to move interceptors to be a more general gRPC concept instead of being tied to the actual on the wire impl. That said, this is a great spot for something like tower to play a role. I have an implementation in the
interoptests that show using a tower service to wrap a specific gRPC service. In this case it intercepts the headers and echos them in a warpped body. But the actualcallfn is the same as you have above. In factinterceptor_fnwas originally designed to fix this use case.I would suggest looking here https://github.com/hyperium/tonic/blob/master/interop/src/server.rs#L169
I would also love to add an example showing how to do this.