|
| 1 | +--- |
| 2 | +title: axum |
| 3 | +description: "Learn about monitoring your axum application with Sentry." |
| 4 | +--- |
| 5 | + |
| 6 | +The Sentry SDK offers a middleware for the [`axum`](https://github.com/tokio-rs/axum) framework that supports: |
| 7 | + |
| 8 | +- Reporting errors and panics with the correct request correlation. |
| 9 | +- Starting a [transaction](https://docs.sentry.io/concepts/key-terms/tracing/) for each request-response cycle. |
| 10 | + |
| 11 | +The integration actually supports any crate based on [`tower`](https://github.com/tower-rs/tower), not just `axum`. |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +To add Sentry with the `axum` integration to your Rust project, just add a new dependency to your `Cargo.toml`: |
| 16 | + |
| 17 | +```toml {filename:Cargo.toml} |
| 18 | +[dependencies] |
| 19 | +axum = "0.8.4" |
| 20 | +tower = "0.5.2" |
| 21 | +tokio = { version = "1.45.0", features = ["full"] } |
| 22 | +sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["tower-axum-matched-path"] } |
| 23 | +``` |
| 24 | + |
| 25 | +## Configure |
| 26 | + |
| 27 | +Initialize and configure the Sentry client. This will enable a set of default integrations, such as panic reporting. |
| 28 | +Then, initialize `axum` with the Sentry middleware. |
| 29 | + |
| 30 | +<Alert level="warning"> |
| 31 | + |
| 32 | +Macros like `#[tokio::main]` are not supported. The Sentry client must be initialized before the async runtime is started, as shown below. |
| 33 | + |
| 34 | +</Alert> |
| 35 | + |
| 36 | +```rust {filename:main.rs} |
| 37 | +use axum::{body::Body, http::Request, routing::get, Router}; |
| 38 | +use sentry::integrations::tower::{NewSentryLayer, SentryHttpLayer}; |
| 39 | +use std::io; |
| 40 | +use tower::ServiceBuilder; |
| 41 | + |
| 42 | +async fn failing() -> () { |
| 43 | + panic!("Everything is on fire!") |
| 44 | +} |
| 45 | + |
| 46 | +fn main() -> io::Result<()> { |
| 47 | + let _guard = sentry::init(( |
| 48 | + "https://[email protected]/4508694563782656", |
| 49 | + sentry::ClientOptions { |
| 50 | + release: sentry::release_name!(), |
| 51 | + // Capture all traces. Set to a lower value in production. |
| 52 | + traces_sample_rate: 1.0, |
| 53 | + // Capture user IPs and potentially sensitive headers when using HTTP server integrations |
| 54 | + // see https://docs.sentry.io/platforms/rust/data-management/data-collected for more info |
| 55 | + send_default_pii: true, |
| 56 | + ..Default::default() |
| 57 | + }, |
| 58 | + )); |
| 59 | + |
| 60 | + let app = Router::new().route("/", get(failing)).layer( |
| 61 | + ServiceBuilder::new() |
| 62 | + // If you're binding the layers directly on the `Router`, bind them in the opposite order, otherwise you might run into a memory leak. |
| 63 | + .layer(NewSentryLayer::<Request<Body>>::new_from_top()) // Bind a new Hub per request, to ensure correct error <> request correlation |
| 64 | + .layer(SentryHttpLayer::new().enable_transaction()), // Start a transaction (Sentry root span) for each request |
| 65 | + ); |
| 66 | + |
| 67 | + tokio::runtime::Builder::new_multi_thread() |
| 68 | + .enable_all() |
| 69 | + .build()? |
| 70 | + .block_on(async { |
| 71 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:3001") |
| 72 | + .await |
| 73 | + .unwrap(); |
| 74 | + axum::serve(listener, app.into_make_service()) |
| 75 | + .await |
| 76 | + .unwrap(); |
| 77 | + }); |
| 78 | + |
| 79 | + Ok(()) |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Verify |
| 84 | + |
| 85 | +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. |
| 86 | + |
| 87 | +Send a request to the application. The panic will be captured by Sentry. |
| 88 | + |
| 89 | +```bash |
| 90 | +curl http://localhost:3001/ |
| 91 | +``` |
| 92 | + |
| 93 | +<Alert> |
| 94 | + |
| 95 | +Learn more about manually capturing an error or message in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>. |
| 96 | + |
| 97 | +</Alert> |
| 98 | + |
| 99 | +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. |
0 commit comments