From 273b4e5c2a9ff6a26cb2849ea15ff6c1e65b27f3 Mon Sep 17 00:00:00 2001 From: lcian Date: Fri, 23 May 2025 15:44:02 +0200 Subject: [PATCH 1/3] refactor(actix): improve Actix docs --- .../rust/guides/actix-web/config.yml | 1 + .../platforms/rust/guides/actix-web/index.mdx | 61 +++++++++++++------ 2 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 docs/platforms/rust/guides/actix-web/config.yml diff --git a/docs/platforms/rust/guides/actix-web/config.yml b/docs/platforms/rust/guides/actix-web/config.yml new file mode 100644 index 00000000000000..30c444ced51c0f --- /dev/null +++ b/docs/platforms/rust/guides/actix-web/config.yml @@ -0,0 +1 @@ +title: Actix Web \ No newline at end of file diff --git a/docs/platforms/rust/guides/actix-web/index.mdx b/docs/platforms/rust/guides/actix-web/index.mdx index d610611753059e..5cae1738585259 100644 --- a/docs/platforms/rust/guides/actix-web/index.mdx +++ b/docs/platforms/rust/guides/actix-web/index.mdx @@ -1,27 +1,33 @@ --- -title: actix-web -description: "Learn about using Sentry with Actix Web." +title: Actix Web +description: "Learn about monitoring your Actix Web application with Sentry." --- -The `sentry-actix` crate adds a middleware for [`actix-web`](https://actix.rs/) that captures errors and report them to `Sentry`. +The Sentry SDK offers a middleware for the [Actix Web](https://actix.rs/) framework that supports: -To use this middleware, configure Sentry then add it to your actix web app as a middleware. Because actix is generally working with non sendable objects and is highly concurrent, this middleware creates a new hub per request. As a result, many of the Sentry integrations, such as breadcrumbs, do not work unless you bind the actix hub. +- Capturing server errors returned by Actix Web services. +- Starting a [transaction](https://docs.sentry.io/concepts/key-terms/tracing/) for each request-response cycle. -Note: Macros like `#[tokio::main]` and `#[actix_web::main]` are not supported. The Sentry client must be initialized before the async runtime is started so that all threads are correctly connected to the Hub. +## Install -## Example +To add Sentry with the Actix Web integration to your Rust project, just add a new dependency to your `Cargo.toml`: -In your `Cargo.toml`: - -```toml {filename:Cargo.toml} +```toml {filename:Cargo.toml} [dependencies] -actix-web = "4.3.1" -sentry = "{{@inject packages.version('sentry.rust') }}" -sentry-actix = "{{@inject packages.version('sentry.rust') }}" +actix-web = "4.11.0" +sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["actix"] } ``` -And your Rust code: +## Configure + +Initialize and configure the Sentry client. This will enable a set of default integrations, such as panic reporting. +Then, initialize Actix Web with the Sentry middleware. + + +Macros like `#[tokio::main]` and `#[actix_web::main]` are not supported. The Sentry client must be initialized before the async runtime is started, as shown below. + + ```rust {filename:main.rs} use std::io; @@ -41,15 +47,21 @@ fn main() -> io::Result<()> { // Capture user IPs and potentially sensitive headers when using HTTP server integrations // see https://docs.sentry.io/platforms/rust/data-management/data-collected for more info send_default_pii: true, + // Capture all HTTP request bodies, regardless of size + max_request_body_size: sentry::MaxRequestBodySize::Always, ..Default::default() }, )); - std::env::set_var("RUST_BACKTRACE", "1"); actix_web::rt::System::new().block_on(async { HttpServer::new(|| { App::new() - .wrap(sentry_actix::Sentry::new()) + .wrap( + sentry::integrations::actix::Sentry::builder() + .capture_server_errors(true) // Capture server errors + .start_transaction(true) // Start a transaction (Sentry root span) for each request + .finish(), + ) .service(failing) }) .bind("127.0.0.1:3001")? @@ -61,7 +73,20 @@ fn main() -> io::Result<()> { } ``` -## Reusing the Hub +## Verify + +The snippet above sets up a service that always fails, so you can test that everything is working as soon as you set it up. + +Send a request to the application. The response error will be captured by Sentry. + +```bash +curl http://localhost:3001/ +``` + + + +Learn more about manually capturing an error or message in our Usage documentation. + + -When using the actix integration, a new per-request Hub will be created from the main Hub, and will be set automatically as the current Hub (`Hub::current()`). -No manual intervention in needed. +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. \ No newline at end of file From 684c9d4c1a63d3f52dea6c05e6625bbfd7db2106 Mon Sep 17 00:00:00 2001 From: Lorenzo Cian Date: Fri, 23 May 2025 17:16:12 +0200 Subject: [PATCH 2/3] Update index.mdx --- docs/platforms/rust/guides/actix-web/index.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/platforms/rust/guides/actix-web/index.mdx b/docs/platforms/rust/guides/actix-web/index.mdx index 5cae1738585259..32084398e5bfaf 100644 --- a/docs/platforms/rust/guides/actix-web/index.mdx +++ b/docs/platforms/rust/guides/actix-web/index.mdx @@ -44,6 +44,8 @@ fn main() -> io::Result<()> { "___PUBLIC_DSN___", sentry::ClientOptions { release: sentry::release_name!(), + // Capture all traces and spans. Set to a lower value in production. + traces_sample_rate: 1.0, // Capture user IPs and potentially sensitive headers when using HTTP server integrations // see https://docs.sentry.io/platforms/rust/data-management/data-collected for more info send_default_pii: true, @@ -89,4 +91,4 @@ Learn more about manually capturing an error or message in our -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. \ No newline at end of file +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. From 81e99626b7e0657ae5b961b8c879b606dd69ccd3 Mon Sep 17 00:00:00 2001 From: Lorenzo Cian Date: Fri, 23 May 2025 17:56:20 +0200 Subject: [PATCH 3/3] Update index.mdx --- docs/platforms/rust/guides/actix-web/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platforms/rust/guides/actix-web/index.mdx b/docs/platforms/rust/guides/actix-web/index.mdx index 32084398e5bfaf..ded74699b897a6 100644 --- a/docs/platforms/rust/guides/actix-web/index.mdx +++ b/docs/platforms/rust/guides/actix-web/index.mdx @@ -44,7 +44,7 @@ fn main() -> io::Result<()> { "___PUBLIC_DSN___", sentry::ClientOptions { release: sentry::release_name!(), - // Capture all traces and spans. Set to a lower value in production. + // Capture all traces and spans. Set to a lower value in production traces_sample_rate: 1.0, // Capture user IPs and potentially sensitive headers when using HTTP server integrations // see https://docs.sentry.io/platforms/rust/data-management/data-collected for more info