Tonic: Need a way to log specific message in interceptor

Created on 21 Apr 2020  ·  11Comments  ·  Source: hyperium/tonic

Feature Request

Motivation

Similar to typical web server log, just want a way to have a record of what's come in.

Proposal

Something like into_inner that doesn't move the request? I'm unclear on the larger architecture.

crattonic enhancement help wanted

All 11 comments

Yup, so basically if I understand correctly we want some way to know the type of the request and the service its on so we can correlate logs with that. Currently, with the Request<()> type you don't know what service or handler it is.

@dansgithubuser Maybe you want get_ref?

Following the tracing example, you could do something like:

tracing::info!(messsage = ?request.get_ref());

@ajguerrer I'm not sure I'm following. Calling get_ref on a Request<()> gets me a &(). I want an interceptor to write generic log logic, like "gRPC server got request: {}" similar to a web server. Right now I write a log statement per handler.

The tracing example, if I understand correctly, shows me how to use spans for tracing. But I don't see where the interceptor (or some generic place to put code) is?

Currently, this is not possible at the interceptor level. We plan on adding this type of feature.

For now you can write a tower service layer that intercepts at the http2 level and use the path in there to log the service and rpc.

There is now a tower example https://github.com/hyperium/tonic/blob/master/examples/src/tower/server.rs, closing this, feel free to re-open if you have anymore questions!

There is now a tower example https://github.com/hyperium/tonic/blob/master/examples/src/tower/server.rs, closing this, feel free to re-open if you have anymore questions!

If I collect the body into a Vec<u8>, how can I get the typed request message from it? :innocent:

@saschagrunert what do you mean by typed request message?

@saschagrunert what do you mean by typed request message?

My main goal was to automatically trace the request and print their fields. If I create a tower interceptor I can collect the body as Vec. But how do I get this vector to be represented as my proto message type? 💁🏻‍♂️ Maybe adding a trace log to each RPC is indeed the more easy solution here.

I personally would go the route of per rpc method. There is a reason that the interceptor is limited. It is because it gets called before we even receive the body. So I think the later solution works the best or else you will do many avoidable copies and slowing everything down.

Yes thank you for the clarification. I also thought waiting for the whole body and collecting it in a vector might have a bigger performance impact. I’ll stick to the typed RPC approach. 👍

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rlabrecque picture rlabrecque  ·  5Comments

cthulhua picture cthulhua  ·  3Comments

jen20 picture jen20  ·  6Comments

LucioFranco picture LucioFranco  ·  6Comments

LucioFranco picture LucioFranco  ·  5Comments