Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request id example #2912

Merged
merged 4 commits into from
Sep 28, 2024
Merged

Conversation

conways-glider
Copy link
Contributor

Add a request-id example using tower, tower-http, and tracing.

Motivation

This is something I've had to do a few times now in axum that I had to figure out through a combination of axum docs and tower-http docs. Having a concrete example shows a way of doing this in a way that axum supports without having to dig through 2 docs.rs pages (or 3 if you're looking in tower as well).

Figuring out the middleware, the order, and how to write the TraceLayer to avoid extra clones has prevented issues for other engineers I've interacted with as well, so the goal is also to clarify an easy way to achieve request ids.

Solution

  • Add an example of how to add randomized request ids at examples/request-id.
  • Utilize tower and tower-http crates to handle request-id middleware.
  • Use tracing and tracing-subscriber crates to show how to add these request ids to each requests span.

examples/request-id/src/main.rs Outdated Show resolved Hide resolved
examples/request-id/src/main.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@mladedav mladedav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@jplatte jplatte merged commit 2542acb into tokio-rs:main Sep 28, 2024
18 checks passed
@conways-glider conways-glider deleted the examples/request-id branch September 28, 2024 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants