Skip to main content

performance_demo/
performance-demo.rs

1#![expect(missing_docs, reason = "predates lint enforcement")]
2
3use std::thread;
4use std::time::Duration;
5
6use sentry::protocol::Request;
7
8// cargo run --example performance-demo
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}