-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(axum): provide docs for Axum #13822
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
title: axum |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,99 @@ | ||||||
--- | ||||||
title: axum | ||||||
description: "Learn about monitoring your axum application with Sentry." | ||||||
--- | ||||||
|
||||||
The Sentry SDK offers a middleware for the [`axum`](https://github.com/tokio-rs/axum) framework that supports: | ||||||
|
||||||
- Reporting errors and panics with the correct request correlation. | ||||||
- Starting a [transaction](https://docs.sentry.io/concepts/key-terms/tracing/) for each request-response cycle. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We try to do platform links when linking to other parts of our own documentation site. |
||||||
|
||||||
The integration actually supports any crate based on [`tower`](https://github.com/tower-rs/tower), not just `axum`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Code + hyperlink looks a little strange. |
||||||
|
||||||
## Install | ||||||
|
||||||
To add Sentry with the `axum` integration to your Rust project, add a new dependency to your `Cargo.toml`: | ||||||
|
||||||
```toml {filename:Cargo.toml} | ||||||
[dependencies] | ||||||
axum = "0.8.4" | ||||||
tower = "0.5.2" | ||||||
tokio = { version = "1.45.0", features = ["full"] } | ||||||
sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["tower-axum-matched-path"] } | ||||||
``` | ||||||
|
||||||
## Configure | ||||||
|
||||||
Initialize and configure the Sentry client. This will enable a set of default integrations, such as panic reporting. | ||||||
Then, initialize `axum` with the Sentry middleware. | ||||||
|
||||||
<Alert level="warning"> | ||||||
|
||||||
Macros like `#[tokio::main]` are not supported. The Sentry client must be initialized before the async runtime is started, as shown below. | ||||||
|
||||||
</Alert> | ||||||
|
||||||
```rust {filename:main.rs} | ||||||
use axum::{body::Body, http::Request, routing::get, Router}; | ||||||
use sentry::integrations::tower::{NewSentryLayer, SentryHttpLayer}; | ||||||
use std::io; | ||||||
use tower::ServiceBuilder; | ||||||
|
||||||
async fn failing() -> () { | ||||||
panic!("Everything is on fire!") | ||||||
} | ||||||
|
||||||
fn main() -> io::Result<()> { | ||||||
let _guard = sentry::init(( | ||||||
"___PUBLIC_DSN___", | ||||||
sentry::ClientOptions { | ||||||
release: sentry::release_name!(), | ||||||
// Capture all traces and spans. Set to a lower value in production | ||||||
traces_sample_rate: 1.0, | ||||||
// Capture user IPs and potentially sensitive headers when using HTTP server integrations | ||||||
// see https://docs.sentry.io/platforms/rust/data-management/data-collected for more info | ||||||
send_default_pii: true, | ||||||
..Default::default() | ||||||
}, | ||||||
)); | ||||||
|
||||||
let app = Router::new().route("/", get(failing)).layer( | ||||||
ServiceBuilder::new() | ||||||
// If you're binding the layers directly on the `Router`, bind them in the opposite order, otherwise you might run into a memory leak | ||||||
.layer(NewSentryLayer::<Request<Body>>::new_from_top()) // Bind a new Hub per request, to ensure correct error <> request correlation | ||||||
.layer(SentryHttpLayer::new().enable_transaction()), // Start a transaction (Sentry root span) for each request | ||||||
); | ||||||
|
||||||
tokio::runtime::Builder::new_multi_thread() | ||||||
.enable_all() | ||||||
.build()? | ||||||
.block_on(async { | ||||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3001") | ||||||
.await | ||||||
.unwrap(); | ||||||
axum::serve(listener, app.into_make_service()) | ||||||
.await | ||||||
.unwrap(); | ||||||
}); | ||||||
|
||||||
Ok(()) | ||||||
} | ||||||
``` | ||||||
|
||||||
## Verify | ||||||
|
||||||
The snippet above sets up a service that always panics, so you can test that everything is working as soon as you set it up. | ||||||
|
||||||
Send a request to the application. The panic will be captured by Sentry. | ||||||
Comment on lines
+83
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be moved up above the snippet. We want to make sure that we're explaining things before demoing them. Change it to say "the following snippet sets up...". |
||||||
|
||||||
```bash | ||||||
curl http://localhost:3001/ | ||||||
``` | ||||||
|
||||||
<Alert> | ||||||
|
||||||
Learn more about manually capturing an error or message in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>. | ||||||
|
||||||
</Alert> | ||||||
|
||||||
To view and resolve the recorded error, log into [sentry.io](https://sentry.io) and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would that error now be found under |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking a bit strange as code and a link. I suggest just keeping it as a hyperlink.