Propagate request_id
Roms1383 opened this issue · 8 comments
Hello and thank you very much for this library !
Also high five for the chapters on tracing @LukeMathWalker, these are pure gold ✨ .
I would like to know if there's a way to forward request_id
generated by TracingLogger
along in my grpc client call inside the server (using tonic crate).
I already know I could basically do something like, e.g. :
let request_id = outer_request // actix incoming request, it should contains `request_id`
.headers()
.get("request_id")
.expect("request_id in headers");
let mut inner_request = tonic::Request::new(());
inner_request
.metadata_mut()
.insert(
"request_id",
request_id
.parse()
.expect("metadata ASCII")
);
let response = grpc_client.clone().send(inner_request).await?.into_inner();
And I already know how to retrieve it on the grpc server side.
But I keep wondering if I can automate it since I have literally hundreds of grpc calls scattered across the codebase.
What makes this difficult is that:
- in
RootSpanBuilder
impl I only have a immutable borrow to ServiceRequest (see here) - in
Interceptor
closure or impl I only have access to outgoing gRPC request (see here and there) without any context of the parent HTTP request. - in
Span
I can record values but AFAIK there's no means to retrieve values stored there previously (on purpose, I guess). - I have looked into Service and used it for my servers, but I'm not sure if it can help on the client side, nor how to properly leverage it if it's the case.
If you guys have any suggestions, that would be very much appreciated ^^
Hey!
You can get the request id using the RequestId
extractor - see https://docs.rs/tracing-actix-web/0.5.0-beta.1/tracing_actix_web/struct.RequestId.html
Then you just need to propagate that all the way down to your gRPC client.
You could a task local from tokio
+ an Interceptor
to automate it - you set the request id in a task local in an actix-web
middleware and the retrieve from the task local in a tonic
Interceptor
.
Oh damn that's actually what I was starting to do... manually 😅
Thanks for pointing this out !
I'm not sure abouttokio
task though, or I don't get how it can be used to avoid passing down RequestId through functions parameter, would you mind giving a bit more explanations ?
Unfortunately I don't have time right now to flesh out the details - you can look at how this https://github.com/LukeMathWalker/actix-web-flash-messages/blob/3351e3cd1c1494da50d773019f42a6e68cc7ed6f/src/middleware.rs#L13 is used in that codebase to get an idea.
Ah no worries, I think I guess it now 👌
thanks !
Indeed it works like a breeze 🎉
Thanks a lot, feel free to close this issue anytime, and let me know if I should post the solution here too 🙏
Posting it would help other users 😄
@LukeMathWalker Or even open a PR which would provide it for those who need it, gated under an optional feature flag for those who don't ?
If you fancy! Let's see what it looks like.