Skip to content

Commit f78ba5b

Browse files
committed
feat(axum): provide docs for Axum
1 parent 0af2bb6 commit f78ba5b

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.

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)