Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: actix-web 3 support #282

Merged
merged 6 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
rust: [1.40.0]
rust: [1.42.0]

name: checkfast/testfast using ${{ matrix.rust }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

**Breaking Changes**:

- Bump the minimum required Rust version to **1.42.0**.
- The `actix` integration / middleware is now compatible with `actix-web 3`.
- Removed all deprecated exports and deprecated feature flags.
- The `failure` integration / feature is now off-by-default along with its deprecation.
- The `log` and `slog` integrations were re-designed, they now offer types that wrap a `log::Log` or `slog::Drain` and forward log events to the currently active sentry `Hub` based on an optional filter and an optional mapper.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This workspace contains various crates that provide support for logging events a
[![crates.io](https://img.shields.io/crates/v/sentry-actix.svg)](https://crates.io/crates/sentry-actix)
[![docs.rs](https://docs.rs/sentry-actix/badge.svg)](https://docs.rs/sentry-actix)

An integration for the `actix-web (0.7)` framework.
An integration for the `actix-web (3.0+)` framework.

- [sentry-anyhow](./sentry-anyhow)
[![crates.io](https://img.shields.io/crates/v/sentry-anyhow.svg)](https://crates.io/crates/sentry-anyhow)
Expand Down Expand Up @@ -99,7 +99,7 @@ best API and adding new features.
We currently only verify this crate against a recent version of Sentry hosted on [sentry.io](https://sentry.io/) but it
should work with on-prem Sentry versions 8.20 and later.

Additionally, the lowest Rust version we target is _1.40.0_.
Additionally, the lowest Rust version we target is _1.42.0_.

## Resources

Expand Down
9 changes: 5 additions & 4 deletions sentry-actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ with_sentry_default = [
]

[dependencies]
sentry-core = { version = "0.20.1", path = "../sentry-core", default-features = false }
actix-web = { version = "3", default-features = false }
futures-util = "0.3.5"

[dev-dependencies]
sentry = { version = "0.20.1", path = "../sentry", default-features = false }
sentry-failure = { version = "0.20.1", path = "../sentry-failure" }
actix-web = { version = "0.7", default-features = false }
failure = "0.1.3"
fragile = "0.3.0"
52 changes: 23 additions & 29 deletions sentry-actix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,45 @@ such as breadcrumbs do not work unless you bind the actix hub.
use std::env;
use std::io;

use actix_web::{server, App, Error, HttpRequest};
use sentry_actix::SentryMiddleware;
use actix_web::{App, Error, get, HttpRequest, HttpServer};

fn failing(_req: &HttpRequest) -> Result<String, Error> {
#[get("/")]
async fn failing(_req: HttpRequest) -> Result<String, Error> {
Err(io::Error::new(io::ErrorKind::Other, "An error happens here").into())
}

fn main() {
let _guard = sentry::init("https://public@sentry.io/1234");
#[actix_web::main]
async fn main() -> io::Result<()> {
let _guard = sentry::init(());
env::set_var("RUST_BACKTRACE", "1");

server::new(|| {
HttpServer::new(|| {
App::new()
.middleware(SentryMiddleware::new())
.resource("/", |r| r.f(failing))
.wrap(sentry_actix::Sentry::new())
.service(failing)
})
.bind("127.0.0.1:3001")
.unwrap()
.run();
.bind("127.0.0.1:3001")?
.run()
.await?;

Ok(())
}
```

## Reusing the Hub
# Reusing the Hub

If you use this integration the `Hub::current()` returned hub is typically the wrong one.
To get the request specific one you need to use the `ActixWebHubExt` trait:
This integration will automatically update the current Hub instance. For example,
the following will capture a message in the current request's Hub:

```rust
use sentry::{Hub, Level};
use sentry_actix::ActixWebHubExt;

let hub = Hub::from_request(req);
hub.capture_message("Something is not well", Level::Warning);
```

The hub can also be made current:
use actix_web::{Error, get, HttpRequest};
use sentry::Level;

```rust
use sentry::{Hub, Level};
use sentry_actix::ActixWebHubExt;

let hub = Hub::from_request(req);
Hub::run(hub, || {
#[get("/")]
async fn hello_world(_req: HttpRequest) -> Result<String, Error> {
sentry::capture_message("Something is not well", Level::Warning);
});
Ok("Hello World".into())
}
```

## Resources
Expand Down
33 changes: 22 additions & 11 deletions sentry-actix/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
use std::env;
use std::io;

use actix_web::{server, App, Error, HttpRequest};
use sentry_actix::SentryMiddleware;
use actix_web::{get, App, Error, HttpRequest, HttpServer};
use sentry::Level;

fn failing(_req: &HttpRequest) -> Result<String, Error> {
Err(io::Error::new(io::ErrorKind::Other, "Something went really wrong here").into())
#[get("/")]
async fn failing(_req: HttpRequest) -> Result<String, Error> {
Err(io::Error::new(io::ErrorKind::Other, "An error happens here").into())
}

fn main() {
#[get("/hello")]
async fn hello_world(_req: HttpRequest) -> Result<String, Error> {
sentry::capture_message("Something is not well", Level::Warning);
Ok("Hello World".into())
}

#[actix_web::main]
async fn main() -> io::Result<()> {
let _guard = sentry::init(());
env::set_var("RUST_BACKTRACE", "1");

server::new(|| {
HttpServer::new(|| {
App::new()
.middleware(SentryMiddleware::builder().emit_header(true).finish())
.resource("/", |r| r.f(failing))
.wrap(sentry_actix::Sentry::new())
.service(failing)
.service(hello_world)
})
.bind("127.0.0.1:3001")
.unwrap()
.run();
.bind("127.0.0.1:3001")?
.run()
.await?;

Ok(())
}
Loading