performance/
performance.rs1#[macro_use]
2extern crate rocket;
3
4use rocket::{Build, Rocket};
5use rocket_sentry::RocketSentry;
6use sentry::{Hub, TransactionContext, TransactionOrSpan};
7use std::sync::Arc;
8use std::thread;
9use std::time::Duration;
10
11#[get("/performance")]
12fn performance() -> String {
13 let duration = Duration::from_millis(500);
14 thread::sleep(duration);
15 format!("Waited {duration:?}")
16}
17
18#[get("/performance/<id>")]
19fn performance_with_id(id: u16) -> String {
20 let duration = Duration::from_secs(id.into());
22 thread::sleep(duration);
23 format!("Waited {duration:?} for id {id}")
24}
25
26#[get("/performance?<param1>&<param2>")]
27fn performance_with_parameter(param1: String, param2: u32) -> String {
28 let duration = Duration::from_millis(250);
29 thread::sleep(duration);
30 format!("Waited {duration:?} for param {param1} - {param2}")
31}
32
33#[get("/performance/skip")]
34fn performance_skipped() -> String {
35 let duration = Duration::from_millis(100);
36 thread::sleep(duration);
37 format!("Waited {duration:?}\nTransaction will be dropped")
38}
39
40#[get("/performance/random")]
41fn performance_rng() -> String {
42 let duration = Duration::from_millis(100);
43 thread::sleep(duration);
44 format!("Waited {duration:?}\nTransaction MIGHT be dropped")
45}
46
47#[get("/performance/multiple_spans")]
48fn performance_with_multiple_spans() -> String {
49 let duration = Duration::from_millis(150);
50 thread::sleep(duration);
51 let parent = Hub::current().configure_scope(|scope| scope.get_span());
52 let op_name = "some operation";
53 let child_1_name = "child 1";
54 let child_1: TransactionOrSpan = match parent {
55 Some(parent) => parent.start_child(op_name, child_1_name).into(),
56 None => {
57 let context = TransactionContext::new(child_1_name, op_name);
58 sentry::start_transaction(context).into()
59 }
60 };
61
62 thread::sleep(duration);
63 sentry::capture_message("some message", sentry::Level::Warning);
64 let child_2 = child_1.start_child(op_name, "child 2");
65 thread::sleep(duration);
66 child_2.finish();
67 child_1.finish();
68 "Waited some time with multiple spans".to_string()
69}
70
71#[launch]
72fn rocket() -> Rocket<Build> {
73 let rocket_instance = rocket::build();
74 let default_rate = rocket_instance
76 .figment()
77 .extract_inner::<f32>("sentry_traces_sample_rate")
78 .unwrap();
79 let traces_sampler = move |ctx: &TransactionContext| -> f32 {
80 match ctx.name() {
81 "GET /performance/skip" => {
82 log::warn!("Dropping performance transaction");
83 0.
84 }
85 "GET /performance/random" => {
86 log::warn!("Sending performance transaction half the time");
87 0.
88 }
89 _ => {
90 log::warn!("Sending performance transaction using default rate");
91 default_rate
92 }
93 }
94 };
95 let rocket_sentry = RocketSentry::builder()
96 .traces_sampler(Arc::new(traces_sampler))
97 .build();
98 rocket_instance.attach(rocket_sentry).mount(
99 "/",
100 routes![
101 performance,
102 performance_with_id,
103 performance_with_parameter,
104 performance_skipped,
105 performance_rng,
106 performance_with_multiple_spans,
107 ],
108 )
109}