Skip to main content

with_scope

Function with_scope 

Source
pub fn with_scope<C, F, R>(scope_config: C, callback: F) -> R
where C: FnOnce(&mut Scope), F: FnOnce() -> R,
Expand description

Temporarily pushes a scope for a single call optionally reconfiguring it.

This function takes two arguments: the first is a callback that is passed a scope and can reconfigure it. The second is callback that then executes in the context of that scope.

This is useful when extra data should be send with a single capture call for instance a different level or tags:

ยงExamples

use sentry::protocol::Level;

sentry::with_scope(
    |scope| scope.set_level(Some(Level::Warning)),
    || sentry::capture_message("some message", Level::Info),
);

assert_eq!(captured_event.level, Level::Warning);
Examples found in repository?
examples/send-with-extra.rs (lines 8-17)
1fn main() {
2    let _sentry = sentry::init(
3        sentry::ClientOptions::new()
4            .maybe_release(sentry::release_name!())
5            .debug(true),
6    );
7
8    sentry::with_scope(
9        |scope| {
10            scope.set_level(Some(sentry::Level::Warning));
11            scope.set_fingerprint(Some(["a-message"].as_ref()));
12            scope.set_tag("foo", "bar");
13        },
14        || {
15            panic!("Shit's on fire yo. ๐Ÿ”ฅ ๐Ÿš’");
16        },
17    );
18}