Connect Axum to Bugsink
The Rust SDK’s Tower integration attaches an Axum request to errors captured inside its handler. This guide adds that integration to an existing inventory API, captures a handled domain error, and keeps the application’s own 500 response unchanged.
To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.
Install the SDK and Tower integration
Add the open-source Sentry Rust SDK, its Tower integration, and Tower itself. Bugsink accepts the Sentry event protocol, so the SDK can send events directly to your Bugsink project:
cargo add sentry --no-default-features \ --features backtrace,contexts,panic,reqwest,rustls cargo add sentry-tower --features http cargo add towercargo add sentry --no-default-features \ --features backtrace,contexts,panic,reqwest,rustls cargo add sentry-tower --features http cargo add tower
Initialize before Tokio starts
Initialize Sentry before creating the multithreaded Tokio runtime. This lets the runtime’s worker threads inherit the configured SDK hub. Keep the returned guard alive for the server’s lifetime so queued events can be sent during shutdown. Reading the DSN from the environment keeps it out of source control:
fn main() { let _sentry = sentry::init(env::var("SENTRY_DSN").unwrap_or_default()); tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap() .block_on(run()); }fn main() { let _sentry = sentry::init(env::var("SENTRY_DSN").unwrap_or_default()); tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap() .block_on(run()); }
Set SENTRY_DSN to the DSN shown by your Bugsink project when you start the
application:
export SENTRY_DSN=https://key@bugsink.example/1export SENTRY_DSN=https://key@bugsink.example/1
Attach HTTP request details
The Tower layers give each request its own SDK hub and add its HTTP method, URL, and headers to events captured while that request is active. Layer order matters: create the request-local hub first, then add the HTTP details, and wrap the Axum router with the resulting service:
let sentry_layers = ServiceBuilder::new() .layer(NewSentryLayer::new_from_top()) .layer(SentryHttpLayer::new()); let app = Router::new() .route("/", get(index)) .route("/checkout/{sku}", get(checkout)) .layer(sentry_layers);let sentry_layers = ServiceBuilder::new() .layer(NewSentryLayer::new_from_top()) .layer(SentryHttpLayer::new()); let app = Router::new() .route("/", get(index)) .route("/checkout/{sku}", get(checkout)) .layer(sentry_layers);
Capture a handled request error
Axum lets this handler turn a missing product into its own JSON response, so
the error is handled rather than an uncaught failure. Call capture_error()
before returning when that handled error should still be visible in Bugsink.
The call accepts a normal std::error::Error; it does not choose or alter the
HTTP response:
let error = ProductNotFound(format!("Product {sku} is not in inventory")); sentry::capture_error(&error);let error = ProductNotFound(format!("Product {sku} is not in inventory")); sentry::capture_error(&error);
Start the application, then request an unknown product:
cargo run --locked :::bash curl --include http://127.0.0.1:8080/checkout/rust-missingcargo run --locked :::bash curl --include http://127.0.0.1:8080/checkout/rust-missing
The handler still returns HTTP 500 with its own JSON body:
{"error":"Product rust-missing is not in inventory"}{"error":"Product rust-missing is not in inventory"}
See the error in Bugsink
Open your Bugsink project. The new ProductNotFound issue contains the error
value and the concrete Axum request attached by the Tower HTTP layer.
http://127.0.0.1:8086/checkout/rust-missing GET */* 127.0.0.1:8086 curl/8.5.0Request
REQUEST HEADERS
That’s it: handled Axum errors you capture inside a request now include that request in Bugsink while your handler keeps control of the response.
Limitations
Only errors passed to capture_error() follow the path above. Returning an
Axum error or allowing a handler to panic does not automatically report it.
This capture path also arrives without an application stack, so Bugsink shows
the request and error value but not the call site.
For panics, manual capture, logging, messages, tags, structured context, breadcrumbs, releases, and shutdown behavior, continue with the main Rust guide.
