Adds support for automatic hub binding for each request received by the Tower server (or client, though usefulness is limited in this case).
This allows breadcrumbs collected during the request handling to land in a specific hub, and avoid having them mixed across requests should a new hub be bound at each request.
```rust use sentry_tower::NewSentryLayer;
// Compose a Tower service where each request gets its own Sentry hub
let service = ServiceBuilder::new()
.layer(NewSentryLayer::
More customization can be achieved through the new
function, such as passing a [Hub
]
directly.
```rust use sentry::Hub; use sentry_tower::SentryLayer;
// Create a hub dedicated to web requests let hub = Arc::new(Hub::with(|hub| Hub::newfromtop(hub)));
// Compose a Tower service let service = ServiceBuilder::new() .layer(SentryLayer::<_, _, Request>::new(hub)) .timeout(Duration::fromsecs(30)) .service(tower::servicefn(|req: Request| format!("hello {}", req))); ```
The layer can also accept a closure to return a hub depending on the incoming request.
```rust use sentry::Hub; use sentry_tower::SentryLayer;
// Compose a Tower service let hello = Arc::new(Hub::with(|hub| Hub::newfromtop(hub))); let other = Arc::new(Hub::with(|hub| Hub::newfromtop(hub)));
let service = ServiceBuilder::new() .layer(SentryLayer::new(|req: &Request| match req.asstr() { "hello" => hello.clone(), _ => other.clone(), })) .timeout(Duration::fromsecs(30)) .service(tower::service_fn(|req: Request| format!("{} world", req))); ```
When using Tonic, the layer can be used directly by the Tonic stack:
```rust use sentrytower::NewSentryLayer; use helloworld::{, greeter_server::};
struct GreeterService;
impl Greeter for GreeterService {
async fn sayhello(&self, req: Request
Server::builder() .layer(NewSentryLayer::newfromtop()) .add_service(GreeterServer::new(GreeterService)) .serve("127.0.0.1:50051".parse().unwrap()) .await?; ```
tower-http
The http
feature offers another layer which will attach request details
onto captured events, and optionally start a new performance monitoring
transaction based on the incoming HTTP headers.
When combining both layers, take care of the ordering of both. For example
with [tower::ServiceBuilder
], always define the Hub
layer before the Http
one, like so:
rust
let layer = tower::ServiceBuilder::new()
.layer(sentry_tower::NewSentryLayer::<Request>::new_from_top())
.layer(sentry_tower::SentryHttpLayer::with_transaction());
License: Apache-2.0