Rust axum / hyper request local variables

Is there a way to bind a variable to Axum request?

In particular, I'm trying to add request id to each tracing event. I can do it with tower::trace middleware like this:

pub struct RequestSpan;

impl<B> tower_http::trace::MakeSpan<B> for RequestSpan {
    fn make_span(&mut self, request: &http::Request<B>) -> tracing::Span {
            id = %ulid::Ulid::new().to_string(),
            method = %request.method(),
            uri = %request.uri(),
            version = ?request.version(),


let middleware_stack = tower::ServiceBuilder::new()

It works in the scope of the server, but I also need to pass the request id into an external task queue. Any suggestions?

You might want to store it in the request extensions, which is a typemap allowing you to keep any Send + Sync + 'static value (like your request ID) in an HTTP request.

1 Like

Thank you, I've published it as a little crate: GitHub - imbolc/tower-request-id: A tower (hyper, axum, warp) service to add a unique id for every request

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.