|
| 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](/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 | +<OnboardingOptionButtons |
| 16 | + options={["error-monitoring", "performance"]} |
| 17 | +/> |
| 18 | + |
| 19 | +To add Sentry with the `axum` integration to your Rust project, add a new dependency to your `Cargo.toml`: |
| 20 | + |
| 21 | +```toml {filename:Cargo.toml} |
| 22 | +[dependencies] |
| 23 | +axum = "0.8.4" |
| 24 | +tower = "0.5.2" |
| 25 | +tokio = { version = "1.45.0", features = ["full"] } |
| 26 | +sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["tower-axum-matched-path"] } |
| 27 | +``` |
| 28 | + |
| 29 | +## Configure |
| 30 | + |
| 31 | +Initialize and configure the Sentry client. This will enable a set of default integrations, such as panic reporting. |
| 32 | +Then, initialize `axum` with the Sentry middleware. |
| 33 | + |
| 34 | +This snippet sets up a service that always panics, so you can test that everything is working as soon as you set it up. |
| 35 | + |
| 36 | +<Alert level="warning"> |
| 37 | + |
| 38 | +Macros like `#[tokio::main]` are not supported. The Sentry client must be initialized before the async runtime is started, as shown below. |
| 39 | + |
| 40 | +</Alert> |
| 41 | + |
| 42 | +```rust {filename:main.rs} |
| 43 | +use axum::{body::Body, http::Request, routing::get, Router}; |
| 44 | +use sentry::integrations::tower::{NewSentryLayer, SentryHttpLayer}; |
| 45 | +use std::io; |
| 46 | +use tower::ServiceBuilder; |
| 47 | + |
| 48 | +async fn failing() -> () { |
| 49 | + panic!("Everything is on fire!") |
| 50 | +} |
| 51 | + |
| 52 | +fn main() -> io::Result<()> { |
| 53 | + let _guard = sentry::init(( |
| 54 | + "___PUBLIC_DSN___", |
| 55 | + sentry::ClientOptions { |
| 56 | + release: sentry::release_name!(), |
| 57 | + # ___PRODUCT_OPTION_START___ performance |
| 58 | + // Capture all traces and spans. Set to a lower value in production |
| 59 | + traces_sample_rate: 1.0, |
| 60 | + # ___PRODUCT_OPTION_END___ performance |
| 61 | + // Capture user IPs and potentially sensitive headers when using HTTP server integrations |
| 62 | + // see https://docs.sentry.io/platforms/rust/data-management/data-collected for more info |
| 63 | + send_default_pii: true, |
| 64 | + ..Default::default() |
| 65 | + }, |
| 66 | + )); |
| 67 | + |
| 68 | + let app = Router::new().route("/", get(failing)).layer( |
| 69 | + ServiceBuilder::new() |
| 70 | + .layer(NewSentryLayer::<Request<Body>>::new_from_top()) // Bind a new Hub per request, to ensure correct error <> request correlation |
| 71 | + # ___PRODUCT_OPTION_START___ performance |
| 72 | + .layer(SentryHttpLayer::new().enable_transaction()), // Start a transaction (Sentry root span) for each request |
| 73 | + // If you're binding the layers directly on the `Router`, bind them in the opposite order, otherwise you might run into a memory leak |
| 74 | + # ___PRODUCT_OPTION_END___ performance |
| 75 | + ); |
| 76 | + |
| 77 | + tokio::runtime::Builder::new_multi_thread() |
| 78 | + .enable_all() |
| 79 | + .build()? |
| 80 | + .block_on(async { |
| 81 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:3001") |
| 82 | + .await |
| 83 | + .unwrap(); |
| 84 | + axum::serve(listener, app.into_make_service()) |
| 85 | + .await |
| 86 | + .unwrap(); |
| 87 | + }); |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Verify |
| 94 | + |
| 95 | +Send a request to the application. The panic will be captured by Sentry. |
| 96 | + |
| 97 | +```bash |
| 98 | +curl http://localhost:3001/ |
| 99 | +``` |
| 100 | + |
| 101 | +<Alert> |
| 102 | + |
| 103 | +Learn more about manually capturing an error or message in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>. |
| 104 | + |
| 105 | +</Alert> |
| 106 | + |
| 107 | +To view and resolve the recorded panic, log into [sentry.io](https://sentry.io) and select your project. Select Issues, and then Errors & Outages in the sidebar, where you will find the newly created issue. Clicking on the issue's title will open a page where you can see detailed information and mark it as resolved. |
0 commit comments