pub struct Scope { /* private fields */ }client only.Expand description
Holds contextual data for the current scope.
The scope is an object that can be cloned efficiently and stores data that is locally relevant to an event. For instance the scope will hold recorded breadcrumbs and similar information.
The scope can be interacted with in two ways:
- the scope is routinely updated with information by functions such as
add_breadcrumbwhich will modify the currently top-most scope. - the topmost scope can also be configured through the
configure_scopemethod.
Note that the scope can only be modified but not inspected. Only the client can use the scope to extract information currently.
Implementations§
Source§impl Scope
impl Scope
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the scope.
By default a scope will inherit all values from the higher scope. In some situations this might not be what a user wants. Calling this method will wipe all data contained within.
Deletes current breadcrumbs from the scope.
Sourcepub fn set_level(&mut self, level: Option<Level>)
pub fn set_level(&mut self, level: Option<Level>)
Sets a level override.
Examples found in repository?
3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9
10 sentry::with_scope(
11 |scope| {
12 scope.set_level(Some(sentry::Level::Warning));
13 scope.set_fingerprint(Some(["a-message"].as_ref()));
14 scope.set_tag("foo", "bar");
15 },
16 || {
17 panic!("Shit's on fire yo. 🔥 🚒");
18 },
19 );
20}Sourcepub fn set_fingerprint(&mut self, fingerprint: Option<&[&str]>)
pub fn set_fingerprint(&mut self, fingerprint: Option<&[&str]>)
Sets the fingerprint.
Examples found in repository?
3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9 sentry::configure_scope(|scope| {
10 scope.set_fingerprint(Some(["a-message"].as_ref()));
11 scope.set_tag("foo", "bar");
12 });
13
14 sentry::capture_message("This is recorded as a warning now", sentry::Level::Warning);
15}More examples
3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9
10 sentry::with_scope(
11 |scope| {
12 scope.set_level(Some(sentry::Level::Warning));
13 scope.set_fingerprint(Some(["a-message"].as_ref()));
14 scope.set_tag("foo", "bar");
15 },
16 || {
17 panic!("Shit's on fire yo. 🔥 🚒");
18 },
19 );
20}3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9
10 sentry::configure_scope(|scope| {
11 scope.add_event_processor(|mut event| {
12 event.request = Some(sentry::protocol::Request {
13 url: Some("https://example.com/".parse().unwrap()),
14 method: Some("GET".into()),
15 ..Default::default()
16 });
17 Some(event)
18 });
19 });
20
21 sentry::configure_scope(|scope| {
22 scope.set_fingerprint(Some(["a-message"].as_ref()));
23 scope.set_tag("foo", "bar");
24 });
25
26 let id = sentry::capture_message("An HTTP request failed.", sentry::Level::Error);
27 println!("sent event {id}");
28}Sourcepub fn set_transaction(&mut self, transaction: Option<&str>)
pub fn set_transaction(&mut self, transaction: Option<&str>)
Sets the transaction.
Sourcepub fn set_tag<V>(&mut self, key: &str, value: V)where
V: ToString,
pub fn set_tag<V>(&mut self, key: &str, value: V)where
V: ToString,
Sets a tag to a specific value.
Examples found in repository?
3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9
10 {
11 let _guard = sentry::Hub::current().push_scope();
12 sentry::configure_scope(|scope| {
13 scope.set_tag("foo", "bar");
14 });
15 panic!("Holy shit everything is on fire!");
16 }
17}More examples
3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9 sentry::configure_scope(|scope| {
10 scope.set_fingerprint(Some(["a-message"].as_ref()));
11 scope.set_tag("foo", "bar");
12 });
13
14 sentry::capture_message("This is recorded as a warning now", sentry::Level::Warning);
15}3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9
10 sentry::with_scope(
11 |scope| {
12 scope.set_level(Some(sentry::Level::Warning));
13 scope.set_fingerprint(Some(["a-message"].as_ref()));
14 scope.set_tag("foo", "bar");
15 },
16 || {
17 panic!("Shit's on fire yo. 🔥 🚒");
18 },
19 );
20}3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9
10 sentry::configure_scope(|scope| {
11 scope.add_event_processor(|mut event| {
12 event.request = Some(sentry::protocol::Request {
13 url: Some("https://example.com/".parse().unwrap()),
14 method: Some("GET".into()),
15 ..Default::default()
16 });
17 Some(event)
18 });
19 });
20
21 sentry::configure_scope(|scope| {
22 scope.set_fingerprint(Some(["a-message"].as_ref()));
23 scope.set_tag("foo", "bar");
24 });
25
26 let id = sentry::capture_message("An HTTP request failed.", sentry::Level::Error);
27 println!("sent event {id}");
28}Sourcepub fn remove_tag(&mut self, key: &str)
pub fn remove_tag(&mut self, key: &str)
Removes a tag.
If the tag is not set, does nothing.
Sourcepub fn set_context<C>(&mut self, key: &str, value: C)
pub fn set_context<C>(&mut self, key: &str, value: C)
Sets a context for a key.
Sourcepub fn remove_context(&mut self, key: &str)
pub fn remove_context(&mut self, key: &str)
Removes a context for a key.
Sourcepub fn remove_extra(&mut self, key: &str)
pub fn remove_extra(&mut self, key: &str)
Removes a extra.
Sourcepub fn add_event_processor<F>(&mut self, f: F)
pub fn add_event_processor<F>(&mut self, f: F)
Add an event processor to the scope.
Examples found in repository?
3fn main() {
4 let _sentry = sentry::init(
5 sentry::ClientOptions::new()
6 .maybe_release(sentry::release_name!())
7 .debug(true),
8 );
9
10 sentry::configure_scope(|scope| {
11 scope.add_event_processor(|mut event| {
12 event.request = Some(sentry::protocol::Request {
13 url: Some("https://example.com/".parse().unwrap()),
14 method: Some("GET".into()),
15 ..Default::default()
16 });
17 Some(event)
18 });
19 });
20
21 sentry::configure_scope(|scope| {
22 scope.set_fingerprint(Some(["a-message"].as_ref()));
23 scope.set_tag("foo", "bar");
24 });
25
26 let id = sentry::capture_message("An HTTP request failed.", sentry::Level::Error);
27 println!("sent event {id}");
28}Sourcepub fn add_attachment(&mut self, attachment: Attachment)
pub fn add_attachment(&mut self, attachment: Attachment)
Adds an attachment to the scope
Sourcepub fn clear_attachments(&mut self)
pub fn clear_attachments(&mut self)
Clears attachments from the scope
Sourcepub fn apply_to_event(&self, event: Event<'static>) -> Option<Event<'static>>
pub fn apply_to_event(&self, event: Event<'static>) -> Option<Event<'static>>
Applies the contained scoped data to fill an event.
Sourcepub fn apply_to_transaction(&self, transaction: &mut Transaction<'static>)
pub fn apply_to_transaction(&self, transaction: &mut Transaction<'static>)
Applies the contained scoped data to fill a transaction.
Sourcepub fn apply_to_log(&self, log: &mut Log)
Available on crate feature logs only.
pub fn apply_to_log(&self, log: &mut Log)
logs only.Applies the contained scoped data to a log, setting the trace_id and certain default
attributes.
Sourcepub fn set_span(&mut self, span: Option<TransactionOrSpan>)
pub fn set_span(&mut self, span: Option<TransactionOrSpan>)
Set the given TransactionOrSpan as the active span for this scope.
Examples found in repository?
9fn main() {
10 let _sentry = sentry::init(
11 sentry::ClientOptions::new()
12 .maybe_release(sentry::release_name!())
13 .traces_sample_rate(1.0)
14 .debug(true),
15 );
16
17 let transaction =
18 sentry::start_transaction(sentry::TransactionContext::new("transaction", "root span"));
19 let tx_request = Request {
20 url: Some("https://honk.beep".parse().unwrap()),
21 method: Some("GET".to_string()),
22 ..Request::default()
23 };
24 transaction.set_request(tx_request);
25 sentry::configure_scope(|scope| scope.set_span(Some(transaction.clone().into())));
26
27 main_span1();
28
29 thread::sleep(Duration::from_millis(100));
30
31 transaction.finish();
32 sentry::configure_scope(|scope| scope.set_span(None));
33}
34
35fn main_span1() {
36 wrap_in_span("span1", "", || {
37 thread::sleep(Duration::from_millis(50));
38
39 let transaction_ctx = sentry::TransactionContext::continue_from_span(
40 "background transaction",
41 "root span",
42 sentry::configure_scope(|scope| scope.get_span()),
43 );
44 thread::spawn(move || {
45 let transaction = sentry::start_transaction(transaction_ctx);
46 sentry::configure_scope(|scope| scope.set_span(Some(transaction.clone().into())));
47
48 thread::sleep(Duration::from_millis(50));
49
50 thread_span1();
51
52 transaction.finish();
53 sentry::configure_scope(|scope| scope.set_span(None));
54 });
55 thread::sleep(Duration::from_millis(100));
56
57 main_span2()
58 });
59}
60
61fn thread_span1() {
62 wrap_in_span("span1", "", || {
63 thread::sleep(Duration::from_millis(200));
64 })
65}
66
67fn main_span2() {
68 wrap_in_span("span2", "", || {
69 sentry::capture_message(
70 "A message that should have a trace context",
71 sentry::Level::Info,
72 );
73 thread::sleep(Duration::from_millis(200));
74 })
75}
76
77fn wrap_in_span<F, R>(op: &str, description: &str, f: F) -> R
78where
79 F: FnOnce() -> R,
80{
81 let parent = sentry::configure_scope(|scope| scope.get_span());
82 let span1: sentry::TransactionOrSpan = match &parent {
83 Some(parent) => parent.start_child(op, description).into(),
84 None => {
85 let ctx = sentry::TransactionContext::new(description, op);
86 sentry::start_transaction(ctx).into()
87 }
88 };
89 let span_request = Request {
90 url: Some("https://beep.beep".parse().unwrap()),
91 method: Some("GET".to_string()),
92 ..Request::default()
93 };
94 span1.set_request(span_request);
95 sentry::configure_scope(|scope| scope.set_span(Some(span1.clone())));
96
97 let rv = f();
98
99 span1.finish();
100 sentry::configure_scope(|scope| scope.set_span(parent));
101
102 rv
103}Sourcepub fn get_span(&self) -> Option<TransactionOrSpan>
pub fn get_span(&self) -> Option<TransactionOrSpan>
Returns the currently active span.
Examples found in repository?
35fn main_span1() {
36 wrap_in_span("span1", "", || {
37 thread::sleep(Duration::from_millis(50));
38
39 let transaction_ctx = sentry::TransactionContext::continue_from_span(
40 "background transaction",
41 "root span",
42 sentry::configure_scope(|scope| scope.get_span()),
43 );
44 thread::spawn(move || {
45 let transaction = sentry::start_transaction(transaction_ctx);
46 sentry::configure_scope(|scope| scope.set_span(Some(transaction.clone().into())));
47
48 thread::sleep(Duration::from_millis(50));
49
50 thread_span1();
51
52 transaction.finish();
53 sentry::configure_scope(|scope| scope.set_span(None));
54 });
55 thread::sleep(Duration::from_millis(100));
56
57 main_span2()
58 });
59}
60
61fn thread_span1() {
62 wrap_in_span("span1", "", || {
63 thread::sleep(Duration::from_millis(200));
64 })
65}
66
67fn main_span2() {
68 wrap_in_span("span2", "", || {
69 sentry::capture_message(
70 "A message that should have a trace context",
71 sentry::Level::Info,
72 );
73 thread::sleep(Duration::from_millis(200));
74 })
75}
76
77fn wrap_in_span<F, R>(op: &str, description: &str, f: F) -> R
78where
79 F: FnOnce() -> R,
80{
81 let parent = sentry::configure_scope(|scope| scope.get_span());
82 let span1: sentry::TransactionOrSpan = match &parent {
83 Some(parent) => parent.start_child(op, description).into(),
84 None => {
85 let ctx = sentry::TransactionContext::new(description, op);
86 sentry::start_transaction(ctx).into()
87 }
88 };
89 let span_request = Request {
90 url: Some("https://beep.beep".parse().unwrap()),
91 method: Some("GET".to_string()),
92 ..Request::default()
93 };
94 span1.set_request(span_request);
95 sentry::configure_scope(|scope| scope.set_span(Some(span1.clone())));
96
97 let rv = f();
98
99 span1.finish();
100 sentry::configure_scope(|scope| scope.set_span(parent));
101
102 rv
103}Sourcepub fn iter_trace_propagation_headers(
&self,
) -> impl Iterator<Item = (&'static str, String)>
pub fn iter_trace_propagation_headers( &self, ) -> impl Iterator<Item = (&'static str, String)>
Returns the headers needed for distributed tracing.