20 releases

0.31.7 Sep 14, 2023
0.31.5 Jun 16, 2023
0.30.0 Feb 24, 2023
0.29.1 Dec 1, 2022
0.25.0 Mar 2, 2022

#420 in Debugging

Download history 5522/week @ 2023-06-02 5556/week @ 2023-06-09 4718/week @ 2023-06-16 5107/week @ 2023-06-23 4787/week @ 2023-06-30 6523/week @ 2023-07-07 4500/week @ 2023-07-14 4565/week @ 2023-07-21 5260/week @ 2023-07-28 6411/week @ 2023-08-04 7344/week @ 2023-08-11 8140/week @ 2023-08-18 7835/week @ 2023-08-25 5437/week @ 2023-09-01 6506/week @ 2023-09-08 7137/week @ 2023-09-15

28,878 downloads per month
Used in 2 crates (via sentry)


3.5K SLoC


Sentry Rust SDK: sentry-tower

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.


use sentry_tower::NewSentryLayer;

// Compose a Tower service where each request gets its own Sentry hub
let service = ServiceBuilder::new()
    .service(tower::service_fn(|req: Request| format!("hello {}", req)));

More customization can be achieved through the new function, such as passing a [Hub] directly.

use sentry::Hub;
use sentry_tower::SentryLayer;

// Create a hub dedicated to web requests
let hub = Arc::new(Hub::with(|hub| Hub::new_from_top(hub)));

// Compose a Tower service
let service = ServiceBuilder::new()
    .layer(SentryLayer::<_, _, Request>::new(hub))
    .service(tower::service_fn(|req: Request| format!("hello {}", req)));

The layer can also accept a closure to return a hub depending on the incoming request.

use sentry::Hub;
use sentry_tower::SentryLayer;

// Compose a Tower service
let hello = Arc::new(Hub::with(|hub| Hub::new_from_top(hub)));
let other = Arc::new(Hub::with(|hub| Hub::new_from_top(hub)));

let service = ServiceBuilder::new()
    .layer(SentryLayer::new(|req: &Request| match req.as_str() {
        "hello" => hello.clone(),
        _ => other.clone(),
    .service(tower::service_fn(|req: Request| format!("{} world", req)));

When using Tonic, the layer can be used directly by the Tonic stack:

use hello_world::{greeter_server::*, *};
use sentry_tower::NewSentryLayer;

struct GreeterService;

impl Greeter for GreeterService {
    async fn say_hello(
        req: Request<HelloRequest>,
    ) -> Result<Response<HelloReply>, Status> {
        let HelloRequest { name } = req.into_inner();
        if name == "world" {
            capture_anyhow(&anyhow!("Trying to greet a planet"));
            return Err(Status::invalid_argument("Cannot greet a planet"));
        Ok(Response::new(HelloReply {
            message: format!("Hello {}", name),


Usage with 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.

The created transaction will automatically use the request URI as its name. This is sometimes not desirable in case the request URI contains unique IDs or similar. In this case, users should manually override the transaction name in the request handler using the Scope::set_transaction method.

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:

let layer = tower::ServiceBuilder::new()


License: Apache-2.0


~151K SLoC