Skip to content

Commit e3e1c8d

Browse files
lciansfanahata
authored andcommitted
feat(axum): provide docs for Axum (#13822)
Co-authored-by: Shannon Anahata <[email protected]>
1 parent 6290688 commit e3e1c8d

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: axum
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.

src/components/platformIcon.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ import SvelteSVG from 'platformicons/svg/svelte.svg';
129129
import SwiftSVG from 'platformicons/svg/swift.svg';
130130
import SymfonySVG from 'platformicons/svg/symfony.svg';
131131
import TanstackSVG from 'platformicons/svg/tanstack.svg';
132+
import TokioSVG from 'platformicons/svg/tokio.svg';
132133
import TornadoSVG from 'platformicons/svg/tornado.svg';
133134
import TrytonSVG from 'platformicons/svg/tryton.svg';
134135
import UnitySVG from 'platformicons/svg/unity.svg';
@@ -272,6 +273,7 @@ import SvelteSVGLarge from 'platformicons/svg_80x80/svelte.svg';
272273
import SwiftSVGLarge from 'platformicons/svg_80x80/swift.svg';
273274
import SymfonySVGLarge from 'platformicons/svg_80x80/symfony.svg';
274275
import TanstackSVGLarge from 'platformicons/svg_80x80/tanstack.svg';
276+
import TokioSVGLarge from 'platformicons/svg_80x80/tokio.svg';
275277
import TornadoSVGLarge from 'platformicons/svg_80x80/tornado.svg';
276278
import TrytonSVGLarge from 'platformicons/svg_80x80/tryton.svg';
277279
import UnitySVGLarge from 'platformicons/svg_80x80/unity.svg';
@@ -346,6 +348,10 @@ const formatToSVG = {
346348
sm: AwslambdaSVG,
347349
lg: AwslambdaSVGLarge,
348350
},
351+
axum: {
352+
sm: TokioSVG,
353+
lg: TokioSVGLarge,
354+
},
349355
'azure-functions': {
350356
sm: AzurefunctionsSVG,
351357
lg: AzurefunctionsSVGLarge,
@@ -1033,6 +1039,7 @@ export const PLATFORM_TO_ICON = {
10331039
'ruby-sinatra': 'sinatra',
10341040
rust: 'rust',
10351041
'rust-actix-web': 'actix',
1042+
'rust-axum': 'axum',
10361043
scala: 'scala',
10371044
stride3d: 'stride3d',
10381045
sql: 'sql',

0 commit comments

Comments
 (0)