pub fn start_transaction(ctx: TransactionContext) -> TransactionExpand description
Start a new Performance Monitoring Transaction.
The transaction needs to be explicitly finished via Transaction::finish,
otherwise it will be discarded.
The transaction itself also represents the root span in the span hierarchy.
Child spans can be started with the Transaction::start_child method.
Examples found in repository?
examples/performance-demo.rs (line 16)
7fn main() {
8 let _sentry = sentry::init(
9 sentry::ClientOptions::new()
10 .maybe_release(sentry::release_name!())
11 .traces_sample_rate(1.0)
12 .debug(true),
13 );
14
15 let transaction =
16 sentry::start_transaction(sentry::TransactionContext::new("transaction", "root span"));
17 let tx_request = Request {
18 url: Some("https://honk.beep".parse().unwrap()),
19 method: Some("GET".to_string()),
20 ..Request::default()
21 };
22 transaction.set_request(tx_request);
23 sentry::configure_scope(|scope| scope.set_span(Some(transaction.clone().into())));
24
25 main_span1();
26
27 thread::sleep(Duration::from_millis(100));
28
29 transaction.finish();
30 sentry::configure_scope(|scope| scope.set_span(None));
31}
32
33fn main_span1() {
34 wrap_in_span("span1", "", || {
35 thread::sleep(Duration::from_millis(50));
36
37 let transaction_ctx = sentry::TransactionContext::continue_from_span(
38 "background transaction",
39 "root span",
40 sentry::configure_scope(|scope| scope.get_span()),
41 );
42 thread::spawn(move || {
43 let transaction = sentry::start_transaction(transaction_ctx);
44 sentry::configure_scope(|scope| scope.set_span(Some(transaction.clone().into())));
45
46 thread::sleep(Duration::from_millis(50));
47
48 thread_span1();
49
50 transaction.finish();
51 sentry::configure_scope(|scope| scope.set_span(None));
52 });
53 thread::sleep(Duration::from_millis(100));
54
55 main_span2()
56 });
57}
58
59fn thread_span1() {
60 wrap_in_span("span1", "", || {
61 thread::sleep(Duration::from_millis(200));
62 })
63}
64
65fn main_span2() {
66 wrap_in_span("span2", "", || {
67 sentry::capture_message(
68 "A message that should have a trace context",
69 sentry::Level::Info,
70 );
71 thread::sleep(Duration::from_millis(200));
72 })
73}
74
75fn wrap_in_span<F, R>(op: &str, description: &str, f: F) -> R
76where
77 F: FnOnce() -> R,
78{
79 let parent = sentry::configure_scope(|scope| scope.get_span());
80 let span1: sentry::TransactionOrSpan = match &parent {
81 Some(parent) => parent.start_child(op, description).into(),
82 None => {
83 let ctx = sentry::TransactionContext::new(description, op);
84 sentry::start_transaction(ctx).into()
85 }
86 };
87 let span_request = Request {
88 url: Some("https://beep.beep".parse().unwrap()),
89 method: Some("GET".to_string()),
90 ..Request::default()
91 };
92 span1.set_request(span_request);
93 sentry::configure_scope(|scope| scope.set_span(Some(span1.clone())));
94
95 let rv = f();
96
97 span1.finish();
98 sentry::configure_scope(|scope| scope.set_span(parent));
99
100 rv
101}