Tonic: Request duration metrics

Created on 16 Jan 2020  路  1Comment  路  Source: hyperium/tonic

Feature Request

Crates

tonic

Motivation

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
        })

Proposal

Not sure maybe there is someway via Tower for this?

Alternatives

  • "Wrap" all grpc fns separately

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 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.

>All comments

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cthulhua picture cthulhua  路  3Comments

LucioFranco picture LucioFranco  路  6Comments

ebkalderon picture ebkalderon  路  5Comments

LucioFranco picture LucioFranco  路  6Comments

pranjalssh picture pranjalssh  路  4Comments