Skip to content

Commit fc64df8

Browse files
author
Floris Bruynooghe
authored
docs: Document integrations and the Hub better (#291)
This adds more documentation for how to use integrations and uses much more intra-doc links. The Hub documentation as well as the anyhow integration documentation have also seen various improvements.
1 parent 13ed3d5 commit fc64df8

File tree

8 files changed

+123
-32
lines changed

8 files changed

+123
-32
lines changed

sentry-anyhow/src/lib.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
//! Adds support for capturing Sentry errors from `anyhow::Error`.
1+
//! Adds support for capturing Sentry errors from [`anyhow::Error`].
2+
//!
3+
//! This integration adds a new event *source*, which allows you to create events directly
4+
//! from an [`anyhow::Error`] struct. As it is only an event source it only needs to be
5+
//! enabled using the `anyhow` cargo feature, it does not need to be enabled in the call to
6+
//! [`sentry::init`](https://docs.rs/sentry/*/sentry/fn.init.html).
7+
//!
8+
//! This integration does not need to be installed, instead it provides an extra function to
9+
//! capture [`anyhow::Error`], optionally exposing it as a method on the
10+
//! [`sentry::Hub`](https://docs.rs/sentry/*/sentry/struct.Hub.html) using the
11+
//! [`AnyhowHubExt`] trait.
12+
//!
13+
//! Like a plain [`std::error::Error`] being captured, [`anyhow::Error`] is captured with a
14+
//! chain of all error sources, if present. See
15+
//! [`sentry::capture_error`](https://docs.rs/sentry/*/sentry/fn.capture_error.html) for
16+
//! details of this.
217
//!
318
//! # Example
419
//!
@@ -13,6 +28,8 @@
1328
//! capture_anyhow(&err);
1429
//! }
1530
//! ```
31+
//!
32+
//! [`anyhow::Error`]: https://docs.rs/anyhow/*/anyhow/struct.Error.html
1633
1734
#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
1835
#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]
@@ -22,16 +39,27 @@
2239
use sentry_core::types::Uuid;
2340
use sentry_core::Hub;
2441

25-
/// Captures an `anyhow::Error`.
42+
/// Captures an [`anyhow::Error`].
43+
///
44+
/// This will capture an anyhow error as a sentry event if a
45+
/// [`sentry::Client`](../../struct.Client.html) is initialised, otherwise it will be a
46+
/// no-op. The event is dispatched to the thread-local hub, with semantics as described in
47+
/// [`Hub::current`].
2648
///
2749
/// See [module level documentation](index.html) for more information.
50+
///
51+
/// [`anyhow::Error`]: https://docs.rs/anyhow/*/anyhow/struct.Error.html
2852
pub fn capture_anyhow(e: &anyhow::Error) -> Uuid {
2953
Hub::with_active(|hub| hub.capture_anyhow(e))
3054
}
3155

32-
/// Hub extension methods for working with `anyhow`.
56+
/// Hub extension methods for working with [`anyhow`].
57+
///
58+
/// [`anyhow`]: https://docs.rs/anyhow
3359
pub trait AnyhowHubExt {
3460
/// Captures an [`anyhow::Error`] on a specific hub.
61+
///
62+
/// [`anyhow::Error`]: https://docs.rs/anyhow/*/anyhow/struct.Error.html
3563
fn capture_anyhow(&self, e: &anyhow::Error) -> Uuid;
3664
}
3765

sentry-core/src/clientoptions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ pub struct ClientOptions {
5858
pub in_app_exclude: Vec<&'static str>,
5959
// Integration options
6060
/// A list of integrations to enable.
61+
///
62+
/// See [`sentry::integrations`](integrations/index.html#installing-integrations) for
63+
/// how to use this to enable extra integrations.
6164
pub integrations: Vec<Arc<dyn Integration>>,
6265
/// Whether to add default integrations.
66+
///
67+
/// See [`sentry::integrations`](integrations/index.html#default-integrations) for
68+
/// details how this works and interacts with manually installed integrations.
6369
pub default_integrations: bool,
6470
// Hooks
6571
/// Callback that is executed before event sending.

sentry-core/src/hub.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,29 @@ impl HubImpl {
6464
/// The central object that can manages scopes and clients.
6565
///
6666
/// This can be used to capture events and manage the scope. This object is
67-
/// internally synchronized so it can be used from multiple threads if needed.
67+
/// [`Send`][std::marker::Send] and [`Sync`][std::marker::Sync] so it can be used from
68+
/// multiple threads if needed.
6869
///
69-
/// Each thread has its own thread-local (`Hub::current()`) hub, which is
70-
/// automatically derived from the main hub (`Hub::main()`).
70+
/// Each thread has its own thread-local ( see [`Hub::current`]) hub, which is
71+
/// automatically derived from the main hub ([`Hub::main`]).
7172
///
7273
/// In most situations developers do not need to interface with the hub directly. Instead
7374
/// toplevel convenience functions are expose that will automatically dispatch
74-
/// to the thread-local (`Hub::current()`) hub. In some situations this might not be
75+
/// to the thread-local ([`Hub::current`]) hub. In some situations this might not be
7576
/// possible in which case it might become necessary to manually work with the
7677
/// hub. This is for instance the case when working with async code.
7778
///
78-
/// Hubs that are wrapped in `Arc`s can be bound to the current thread with
79+
/// Hubs that are wrapped in [`Arc`]s can be bound to the current thread with
7980
/// the `run` static method.
8081
///
8182
/// Most common operations:
8283
///
83-
/// * `Hub::new`: creates a brand new hub
84-
/// * `Hub::current`: returns the thread local hub
85-
/// * `Hub::with`: invoke a callback with the thread local hub
86-
/// * `Hub::with_active`: like `Hub::with` but does not invoke the callback if
84+
/// * [`Hub::new`]: creates a brand new hub
85+
/// * [`Hub::current`]: returns the thread local hub
86+
/// * [`Hub::with`]: invoke a callback with the thread local hub
87+
/// * [`Hub::with_active`]: like `Hub::with` but does not invoke the callback if
8788
/// the client is not in a supported state or not bound
88-
/// * `Hub::new_from_top`: creates a new hub with just the top scope of another hub.
89+
/// * [`Hub::new_from_top`]: creates a new hub with just the top scope of another hub.
8990
#[derive(Debug)]
9091
pub struct Hub {
9192
#[cfg(feature = "client")]
@@ -115,31 +116,36 @@ impl Hub {
115116
})
116117
}
117118

118-
/// Returns the current hub.
119+
/// Returns the current, thread-local hub.
119120
///
120-
/// By default each thread gets a different thread local hub. If an
121-
/// atomically reference counted hub is available it can override this
122-
/// one here by calling `Hub::run` with a closure.
121+
/// Invoking this will return the current thread-local hub. The first
122+
/// time it is called on a thread, a new thread-local hub will be
123+
/// created based on the topmost scope of the hub on the main thread as
124+
/// returned by [`Hub::main`]. If the main thread did not yet have a
125+
/// hub it will be created when invoking this function.
126+
///
127+
/// To have control over which hub is installed as the current
128+
/// thread-local hub, use [`Hub::run`].
123129
///
124130
/// This method is unavailable if the client implementation is disabled.
125-
/// When using the minimal API set use `Hub::with_active` instead.
131+
/// When using the minimal API set use [`Hub::with_active`] instead.
126132
#[cfg(feature = "client")]
127133
pub fn current() -> Arc<Hub> {
128134
Hub::with(Arc::clone)
129135
}
130136

131137
/// Returns the main thread's hub.
132138
///
133-
/// This is similar to `current` but instead of picking the current
134-
/// thread's hub it returns the main thread's hub instead.
139+
/// This is similar to [`Hub::current`] but instead of picking the
140+
/// current thread's hub it returns the main thread's hub instead.
135141
#[cfg(feature = "client")]
136142
pub fn main() -> Arc<Hub> {
137143
PROCESS_HUB.0.clone()
138144
}
139145

140146
/// Invokes the callback with the default hub.
141147
///
142-
/// This is a slightly more efficient version than `Hub::current()` and
148+
/// This is a slightly more efficient version than [`Hub::current`] and
143149
/// also unavailable in minimal mode.
144150
#[cfg(feature = "client")]
145151
pub fn with<F, R>(f: F) -> R
@@ -149,7 +155,7 @@ impl Hub {
149155
if USE_PROCESS_HUB.with(Cell::get) {
150156
f(&PROCESS_HUB.0)
151157
} else {
152-
// not on safety: this is safe because even though we change the Arc
158+
// note on safety: this is safe because even though we change the Arc
153159
// by temorary binding we guarantee that the original Arc stays alive.
154160
// For more information see: run
155161
THREAD_HUB.with(|stack| unsafe {
@@ -159,7 +165,7 @@ impl Hub {
159165
}
160166
}
161167

162-
/// Like `Hub::with` but only calls the function if a client is bound.
168+
/// Like [`Hub::with`] but only calls the function if a client is bound.
163169
///
164170
/// This is useful for integrations that want to do efficiently nothing if there is no
165171
/// client bound. Additionally this internally ensures that the client can be safely
@@ -181,6 +187,13 @@ impl Hub {
181187
}
182188

183189
/// Binds a hub to the current thread for the duration of the call.
190+
///
191+
/// During the execution of `f` the given hub will be installed as the
192+
/// thread-local hub. So any call to [`Hub::current`] during this time
193+
/// will return the provided hub.
194+
///
195+
/// Once the function is finished executing, including after it
196+
/// paniced, the original hub is re-installed if one was present.
184197
#[cfg(feature = "client")]
185198
pub fn run<F: FnOnce() -> R, R>(hub: Arc<Hub>, f: F) -> R {
186199
let mut restore_process_hub = false;
@@ -331,11 +344,11 @@ impl Hub {
331344

332345
/// End the current Release Health Session.
333346
///
334-
/// See the global [`end_session`](crate::end_session_with)
335-
/// for more documentation.
347+
/// See the global [`sentry::end_session`](crate::end_session) for more documentation.
336348
pub fn end_session(&self) {
337349
self.end_session_with_status(SessionStatus::Exited)
338350
}
351+
339352
/// End the current Release Health Session with the given [`SessionStatus`].
340353
///
341354
/// See the global [`end_session_with_status`](crate::end_session_with_status)

sentry-core/src/scope/real.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ impl Scope {
188188
}
189189

190190
/// Removes a tag.
191+
///
192+
/// If the tag is not set, does nothing.
191193
pub fn remove_tag(&mut self, key: &str) {
192194
self.tags.remove(key);
193195
}

sentry-core/src/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Release Health Sessions
22
//!
3-
//! https://develop.sentry.dev/sdk/sessions/
3+
//! <https://develop.sentry.dev/sdk/sessions/>
44
55
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
66
use std::thread::JoinHandle;

sentry-panic/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]
1919
#![warn(missing_docs)]
2020
#![deny(unsafe_code)]
21-
#![warn(missing_doc_code_examples)]
2221

2322
use std::panic::{self, PanicInfo};
2423
use std::sync::Once;

sentry/src/defaults.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use crate::{ClientOptions, Integration};
1515
/// also sets the `dsn`, `release`, `environment`, and proxy settings based on
1616
/// environment variables.
1717
///
18-
/// When the `default_integrations` option is set to `true` (by default), the
19-
/// following integrations will be added *before* any manually defined
20-
/// integrations, depending on enabled feature flags:
18+
/// When the [`ClientOptions::default_integrations`] option is set to
19+
/// `true` (the default), the following integrations will be added *before*
20+
/// any manually defined integrations, depending on enabled feature flags:
2121
///
2222
/// 1. [`AttachStacktraceIntegration`] (`feature = "backtrace"`)
2323
/// 2. [`DebugImagesIntegration`] (`feature = "debug-images"`)

sentry/src/lib.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,51 @@ pub use crate::init::{init, ClientInitGuard};
9090

9191
/// Available Sentry Integrations.
9292
///
93-
/// See the [`apply_defaults`] function for more information on which integrations are
94-
/// used by default.
93+
/// Integrations extend the functionality of the SDK for some common frameworks and
94+
/// libraries. Integrations come two primary kinds: as event *sources* or as event
95+
/// *processors*.
96+
///
97+
/// Integrations which are *sources*, like e.g. the
98+
/// [`sentry::integrations::anyhow`](integrations::anyhow) integration, usually provide one
99+
/// or more functions to create new events. They will usually provide their own extension
100+
/// trait exposing a new method on the [`Hub`].
101+
///
102+
/// Integrations which *process* events in some way usually implement the
103+
/// [`Itegration`](crate::Integration) trait and need to be installed when sentry is
104+
/// initialised.
105+
///
106+
/// # Installing Integrations
107+
///
108+
/// Processing integrations which implement [`Integration`](crate::Integration) need to be
109+
/// installed when sentry is initialised. This is done using the
110+
/// [`ClientOptions::add_integration`](crate::ClientOptions::add_integration) function, which you can
111+
/// use to add extra integrations.
112+
///
113+
/// For example if you disabled the default integrations (see below) but still wanted the
114+
/// [`sentry::integrations::debug_images`](integrations::debug_images) integration enabled,
115+
/// you could do this as such:
116+
///
117+
/// ```
118+
/// # #[cfg(feature = "debug-images")] {
119+
/// use sentry::ClientOptions;
120+
/// use sentry::integrations::debug_images::DebugImagesIntegration;
121+
///
122+
/// let options = ClientOptions {
123+
/// default_integrations: false,
124+
/// ..Default::default()
125+
/// }.add_integration(DebugImagesIntegration::new());
126+
/// let _guard = sentry::init(options);
127+
/// # }
128+
/// ```
129+
///
130+
/// # Default Integrations
131+
///
132+
/// The [`ClientOptions::default_integrations`](crate::ClientOptions::default_integrations)
133+
/// option is a boolean field that when enabled will enable a number of default integrations
134+
/// **before** any integrations provided by
135+
/// [`ClientOptions::integrations`](crate::ClientOptions::integrations) are installed. This
136+
/// is done using the [`apply_defaults`] function, which should be consulted for more
137+
/// details and the list of which integrations are enabled by default.
95138
///
96139
/// [`apply_defaults`]: ../fn.apply_defaults.html
97140
pub mod integrations {

0 commit comments

Comments
 (0)