sentry_core/
lib.rs

1//! This crate provides the core of the [Sentry] SDK, which can be used to log
2//! events and errors.
3//!
4//! `sentry-core` is meant for integration authors and third-party library authors
5//! that want to instrument their code for sentry.
6//!
7//! Regular users who wish to integrate sentry into their applications should
8//! instead use the [`sentry`] crate, which comes with a default transport and
9//! a large set of integrations for various third-party libraries.
10//!
11//! # Core Concepts
12//!
13//! This crate follows the [Unified API] guidelines and is centered around
14//! the concepts of [`Client`], [`Hub`] and [`Scope`], as well as the extension
15//! points via the [`Integration`], [`Transport`] and [`TransportFactory`] traits.
16//!
17//! # Parallelism, Concurrency and Async
18//!
19//! The main concurrency primitive is the [`Hub`]. In general, all concurrent
20//! code, no matter if multithreaded parallelism or futures concurrency, needs
21//! to run with its own copy of a [`Hub`]. Even though the [`Hub`] is internally
22//! synchronized, using it concurrently may lead to unexpected results up to
23//! panics.
24//!
25//! For threads or tasks that are running concurrently or outlive the current
26//! execution context, a new [`Hub`] needs to be created and bound for the computation.
27//!
28//! ```rust
29//! # let rt = tokio::runtime::Runtime::new().unwrap();
30//! # rt.block_on(async {
31//! use rayon::prelude::*;
32//! use sentry::{Hub, SentryFutureExt};
33//! use std::sync::Arc;
34//!
35//! // Parallel multithreaded code:
36//! let outer_hub = Hub::current();
37//! let results: Vec<_> = [1_u32, 2, 3]
38//!     .into_par_iter()
39//!     .map(|num| {
40//!         let thread_hub = Arc::new(Hub::new_from_top(&outer_hub));
41//!         Hub::run(thread_hub, || num * num)
42//!     })
43//!     .collect();
44//!
45//! assert_eq!(&results, &[1, 4, 9]);
46//!
47//! // Concurrent futures code:
48//! let futures = [1_u32, 2, 3]
49//!     .into_iter()
50//!     .map(|num| async move { num * num }.bind_hub(Hub::new_from_top(Hub::current())));
51//! let results = futures::future::join_all(futures).await;
52//!
53//! assert_eq!(&results, &[1, 4, 9]);
54//! # });
55//! ```
56//!
57//! For tasks that are not concurrent and do not outlive the current execution
58//! context, no *new* [`Hub`] needs to be created, but the current [`Hub`] has
59//! to be bound.
60//!
61//! ```rust
62//! # let rt = tokio::runtime::Runtime::new().unwrap();
63//! # rt.block_on(async {
64//! use sentry::{Hub, SentryFutureExt};
65//!
66//! // Spawned thread that is being joined:
67//! let hub = Hub::current();
68//! let result = std::thread::spawn(|| Hub::run(hub, || 1_u32)).join();
69//!
70//! assert_eq!(result.unwrap(), 1);
71//!
72//! // Spawned future that is being awaited:
73//! let result = tokio::spawn(async { 1_u32 }.bind_hub(Hub::current())).await;
74//!
75//! assert_eq!(result.unwrap(), 1);
76//! # });
77//! ```
78//!
79//! # Minimal API
80//!
81//! By default, this crate comes with a so-called "minimal" mode. This mode will
82//! provide all the APIs needed to instrument code with sentry, and to write
83//! sentry integrations, but it will blackhole a lot of operations.
84//!
85//! In minimal mode some types are restricted in functionality. For instance
86//! the [`Client`] is not available and the [`Hub`] does not retain all API
87//! functionality.
88//!
89//! # Features
90//!
91//! - `feature = "client"`: Activates the [`Client`] type and certain
92//!   [`Hub`] functionality.
93//! - `feature = "test"`: Activates the [`test`] module, which can be used to
94//!   write integration tests. It comes with a test transport which can capture
95//!   all sent events for inspection.
96//!
97//! [Sentry]: https://sentry.io/
98//! [`sentry`]: https://crates.io/crates/sentry
99//! [Unified API]: https://develop.sentry.dev/sdk/unified-api/
100//! [`test`]: test/index.html
101
102#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
103#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]
104#![warn(missing_docs)]
105
106// macros; these need to be first to be used by other modules
107#[macro_use]
108mod macros;
109
110mod api;
111mod breadcrumbs;
112mod clientoptions;
113mod constants;
114mod error;
115mod futures;
116mod hub;
117mod integration;
118mod intodsn;
119mod performance;
120mod scope;
121mod transport;
122
123// public api or exports from this crate
124pub use crate::api::*;
125pub use crate::breadcrumbs::IntoBreadcrumbs;
126pub use crate::clientoptions::{BeforeCallback, ClientOptions, SessionMode};
127pub use crate::error::{capture_error, event_from_error, parse_type_from_debug};
128pub use crate::futures::{SentryFuture, SentryFutureExt};
129pub use crate::hub::Hub;
130pub use crate::integration::Integration;
131pub use crate::intodsn::IntoDsn;
132pub use crate::performance::*;
133pub use crate::scope::{Scope, ScopeGuard};
134pub use crate::transport::{Transport, TransportFactory};
135#[cfg(feature = "logs")]
136mod logger; // structured logging macros exported with `#[macro_export]`
137
138// client feature
139#[cfg(feature = "client")]
140mod client;
141#[cfg(feature = "client")]
142mod hub_impl;
143#[cfg(all(feature = "client", feature = "logs"))]
144mod logs;
145#[cfg(feature = "client")]
146mod session;
147
148#[cfg(feature = "client")]
149pub use crate::clientoptions::MaxRequestBodySize;
150
151#[cfg(feature = "client")]
152pub use crate::{client::Client, hub_impl::SwitchGuard as HubSwitchGuard};
153
154// test utilities
155#[cfg(feature = "test")]
156pub mod test;
157
158// public api from other crates
159#[doc(inline)]
160pub use sentry_types as types;
161pub use sentry_types::protocol::v7 as protocol;
162pub use sentry_types::protocol::v7::{Breadcrumb, Envelope, Level, User};
163
164// utilities reused across integrations
165pub mod utils;