sentry_tower/
lib.rs

1//! Adds support for automatic hub binding for each request received by the Tower server (or client,
2//! though usefulness is limited in this case).
3//!
4//! This allows breadcrumbs collected during the request handling to land in a specific hub, and
5//! avoid having them mixed across requests should a new hub be bound at each request.
6//!
7//! # Examples
8//!
9//! ```rust
10//! # use tower::ServiceBuilder;
11//! # use std::time::Duration;
12//! # type Request = String;
13//! use sentry_tower::NewSentryLayer;
14//!
15//! // Compose a Tower service where each request gets its own Sentry hub
16//! let service = ServiceBuilder::new()
17//!     .layer(NewSentryLayer::<Request>::new_from_top())
18//!     .timeout(Duration::from_secs(30))
19//!     .service(tower::service_fn(|req: Request| format!("hello {}", req)));
20//! ```
21//!
22//! More customization can be achieved through the `new` function, such as passing a [`Hub`]
23//! directly.
24//!
25//! ```rust
26//! # use tower::ServiceBuilder;
27//! # use std::{sync::Arc, time::Duration};
28//! # type Request = String;
29//! use sentry::Hub;
30//! use sentry_tower::SentryLayer;
31//!
32//! // Create a hub dedicated to web requests
33//! let hub = Arc::new(Hub::with(|hub| Hub::new_from_top(hub)));
34//!
35//! // Compose a Tower service
36//! let service = ServiceBuilder::new()
37//!     .layer(SentryLayer::<_, _, Request>::new(hub))
38//!     .timeout(Duration::from_secs(30))
39//!     .service(tower::service_fn(|req: Request| format!("hello {}", req)));
40//! ```
41//!
42//! The layer can also accept a closure to return a hub depending on the incoming request.
43//!
44//! ```rust
45//! # use tower::ServiceBuilder;
46//! # use std::{sync::Arc, time::Duration};
47//! # type Request = String;
48//! use sentry::Hub;
49//! use sentry_tower::SentryLayer;
50//!
51//! // Compose a Tower service
52//! let hello = Arc::new(Hub::with(|hub| Hub::new_from_top(hub)));
53//! let other = Arc::new(Hub::with(|hub| Hub::new_from_top(hub)));
54//!
55//! let service = ServiceBuilder::new()
56//!     .layer(SentryLayer::new(|req: &Request| match req.as_str() {
57//!         "hello" => hello.clone(),
58//!         _ => other.clone(),
59//!     }))
60//!     .timeout(Duration::from_secs(30))
61//!     .service(tower::service_fn(|req: Request| format!("{} world", req)));
62//! ```
63//!
64//! When using Tonic, the layer can be used directly by the Tonic stack:
65//!
66//! ```rust,no_run
67//! # use anyhow::{anyhow, Result};
68//! # use sentry_anyhow::capture_anyhow;
69//! # use tonic::{Request, Response, Status, transport::Server};
70//! # mod hello_world {
71//! #     include!("helloworld.rs");
72//! # }
73//! use hello_world::{greeter_server::*, *};
74//! use sentry_tower::NewSentryLayer;
75//!
76//! struct GreeterService;
77//!
78//! #[tonic::async_trait]
79//! impl Greeter for GreeterService {
80//!     async fn say_hello(
81//!         &self,
82//!         req: Request<HelloRequest>,
83//!     ) -> Result<Response<HelloReply>, Status> {
84//!         let HelloRequest { name } = req.into_inner();
85//!         if name == "world" {
86//!             capture_anyhow(&anyhow!("Trying to greet a planet"));
87//!             return Err(Status::invalid_argument("Cannot greet a planet"));
88//!         }
89//!         Ok(Response::new(HelloReply {
90//!             message: format!("Hello {}", name),
91//!         }))
92//!     }
93//! }
94//!
95//! # #[tokio::main]
96//! # async fn main() -> Result<()> {
97//! Server::builder()
98//!     .layer(NewSentryLayer::new_from_top())
99//!     .add_service(GreeterServer::new(GreeterService))
100//!     .serve("127.0.0.1:50051".parse().unwrap())
101//!     .await?;
102//! #     Ok(())
103//! # }
104//! ```
105//!
106//! ## Usage with `tower-http`
107//!
108//! The `http` feature of the `sentry-tower` crate offers another layer which will attach
109//! request details onto captured events, and optionally start a new performance monitoring
110//! transaction based on the incoming HTTP headers.  When using the tower integration via
111//! `sentry::integrations::tower`, this feature can also be enabled using the `tower-http`
112//! feature of the `sentry` crate instead of the `tower` feature.
113//!
114//! The created transaction will automatically use the request URI as its name.
115//! This is sometimes not desirable in case the request URI contains unique IDs
116//! or similar. In this case, users should manually override the transaction name
117//! in the request handler using the [`Scope::set_transaction`](sentry_core::Scope::set_transaction)
118//! method.
119//!
120//! When combining both layers, take care of the ordering of both. For example
121//! with [`tower::ServiceBuilder`], always define the `Hub` layer before the `Http`
122//! one, like so:
123//!
124//! ```rust
125//! # #[cfg(feature = "http")] {
126//! # type Request = http::Request<String>;
127//! let layer = tower::ServiceBuilder::new()
128//!     .layer(sentry_tower::NewSentryLayer::<Request>::new_from_top())
129//!     .layer(sentry_tower::SentryHttpLayer::with_transaction());
130//! # }
131//! ```
132//!
133//! [`tower::ServiceBuilder`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html
134
135#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
136#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]
137#![warn(missing_docs)]
138
139use std::marker::PhantomData;
140use std::sync::Arc;
141use std::task::{Context, Poll};
142
143use sentry_core::{Hub, SentryFuture, SentryFutureExt};
144use tower_layer::Layer;
145use tower_service::Service;
146
147#[cfg(feature = "http")]
148mod http;
149#[cfg(feature = "http")]
150pub use crate::http::*;
151
152/// Provides a hub for each request
153pub trait HubProvider<H, Request>
154where
155    H: Into<Arc<Hub>>,
156{
157    /// Returns a hub to be bound to the request
158    fn hub(&self, request: &Request) -> H;
159}
160
161impl<H, F, Request> HubProvider<H, Request> for F
162where
163    F: Fn(&Request) -> H,
164    H: Into<Arc<Hub>>,
165{
166    fn hub(&self, request: &Request) -> H {
167        (self)(request)
168    }
169}
170
171impl<Request> HubProvider<Arc<Hub>, Request> for Arc<Hub> {
172    fn hub(&self, _request: &Request) -> Arc<Hub> {
173        self.clone()
174    }
175}
176
177/// Provides a new hub made from the currently active hub for each request
178#[derive(Clone, Copy)]
179pub struct NewFromTopProvider;
180
181impl<Request> HubProvider<Arc<Hub>, Request> for NewFromTopProvider {
182    fn hub(&self, _request: &Request) -> Arc<Hub> {
183        // The Clippy lint here is a false positive, the suggestion to write
184        // `Hub::with(Hub::new_from_top)` does not compiles:
185        //     143 |         Hub::with(Hub::new_from_top).into()
186        //         |         ^^^^^^^^^ implementation of `std::ops::FnOnce` is not general enough
187        #[allow(clippy::redundant_closure)]
188        Hub::with(|hub| Hub::new_from_top(hub)).into()
189    }
190}
191
192/// Tower layer that binds a specific Sentry hub for each request made.
193pub struct SentryLayer<P, H, Request>
194where
195    P: HubProvider<H, Request>,
196    H: Into<Arc<Hub>>,
197{
198    provider: P,
199    _hub: PhantomData<(H, fn() -> Request)>,
200}
201
202impl<S, P, H, Request> Layer<S> for SentryLayer<P, H, Request>
203where
204    P: HubProvider<H, Request> + Clone,
205    H: Into<Arc<Hub>>,
206{
207    type Service = SentryService<S, P, H, Request>;
208
209    fn layer(&self, service: S) -> Self::Service {
210        SentryService {
211            service,
212            provider: self.provider.clone(),
213            _hub: PhantomData,
214        }
215    }
216}
217
218impl<P, H, Request> Clone for SentryLayer<P, H, Request>
219where
220    P: HubProvider<H, Request> + Clone,
221    H: Into<Arc<Hub>>,
222{
223    fn clone(&self) -> Self {
224        Self {
225            provider: self.provider.clone(),
226            _hub: PhantomData,
227        }
228    }
229}
230
231impl<P, H, Request> SentryLayer<P, H, Request>
232where
233    P: HubProvider<H, Request> + Clone,
234    H: Into<Arc<Hub>>,
235{
236    /// Build a new layer with the given Layer provider
237    pub fn new(provider: P) -> Self {
238        Self {
239            provider,
240            _hub: PhantomData,
241        }
242    }
243}
244
245/// Tower service that binds a specific Sentry hub for each request made.
246pub struct SentryService<S, P, H, Request>
247where
248    P: HubProvider<H, Request>,
249    H: Into<Arc<Hub>>,
250{
251    service: S,
252    provider: P,
253    _hub: PhantomData<(H, fn() -> Request)>,
254}
255
256impl<S, Request, P, H> Service<Request> for SentryService<S, P, H, Request>
257where
258    S: Service<Request>,
259    P: HubProvider<H, Request>,
260    H: Into<Arc<Hub>>,
261{
262    type Response = S::Response;
263    type Error = S::Error;
264    type Future = SentryFuture<S::Future>;
265
266    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
267        self.service.poll_ready(cx)
268    }
269
270    fn call(&mut self, request: Request) -> Self::Future {
271        let hub = self.provider.hub(&request).into();
272        let fut = Hub::run(hub.clone(), || self.service.call(request));
273        fut.bind_hub(hub)
274    }
275}
276
277impl<S, P, H, Request> Clone for SentryService<S, P, H, Request>
278where
279    S: Clone,
280    P: HubProvider<H, Request> + Clone,
281    H: Into<Arc<Hub>>,
282{
283    fn clone(&self) -> Self {
284        Self {
285            service: self.service.clone(),
286            provider: self.provider.clone(),
287            _hub: PhantomData,
288        }
289    }
290}
291
292impl<S, P, H, Request> SentryService<S, P, H, Request>
293where
294    P: HubProvider<H, Request> + Clone,
295    H: Into<Arc<Hub>>,
296{
297    /// Wrap a Tower service with a Tower layer that binds a Sentry hub for each request made.
298    pub fn new(provider: P, service: S) -> Self {
299        SentryLayer::<P, H, Request>::new(provider).layer(service)
300    }
301}
302
303/// Tower layer that binds a new Sentry hub for each request made
304pub type NewSentryLayer<Request> = SentryLayer<NewFromTopProvider, Arc<Hub>, Request>;
305
306impl<Request> NewSentryLayer<Request> {
307    /// Create a new Sentry layer that binds a new Sentry hub for each request made
308    pub fn new_from_top() -> Self {
309        Self {
310            provider: NewFromTopProvider,
311            _hub: PhantomData,
312        }
313    }
314}
315
316/// Tower service that binds a new Sentry hub for each request made.
317pub type NewSentryService<S, Request> = SentryService<S, NewFromTopProvider, Arc<Hub>, Request>;
318
319impl<S, Request> NewSentryService<S, Request> {
320    /// Wrap a Tower service with a Tower layer that binds a Sentry hub for each request made.
321    pub fn new_from_top(service: S) -> Self {
322        Self {
323            provider: NewFromTopProvider,
324            service,
325            _hub: PhantomData,
326        }
327    }
328}
329
330#[cfg(test)]
331mod tests {
332    use super::*;
333    use std::rc::Rc;
334
335    fn assert_sync<T: Sync>() {}
336
337    #[test]
338    fn test_layer_is_sync_when_request_isnt() {
339        assert_sync::<NewSentryLayer<Rc<()>>>(); // Rc<()> is not Sync
340    }
341
342    #[test]
343    fn test_service_is_sync_when_request_isnt() {
344        assert_sync::<NewSentryService<(), Rc<()>>>(); // Rc<()> is not Sync
345    }
346}