performance/
performance.rs1#[macro_use]
2extern crate rocket;
3
4use rocket::{Build, Rocket};
5use sentry::TransactionContext;
6use std::sync::Arc;
7use std::thread;
8use std::time::Duration;
9
10use rocket_sentry::RocketSentry;
11
12#[get("/performance")]
13fn performance() -> String {
14 let duration = Duration::from_millis(500);
15 thread::sleep(duration);
16 format!("Waited {duration:?}")
17}
18
19#[get("/performance/<id>")]
20fn performance_with_id(id: u16) -> String {
21 let duration = Duration::from_secs(id.into());
23 thread::sleep(duration);
24 format!("Waited {duration:?} for id {id}")
25}
26
27#[get("/performance?<param1>&<param2>")]
28fn performance_with_parameter(param1: String, param2: u32) -> String {
29 let duration = Duration::from_millis(250);
30 thread::sleep(duration);
31 format!("Waited {duration:?} for param {param1} - {param2}")
32}
33
34#[get("/performance/skip")]
35fn performance_skipped() -> String {
36 let duration = Duration::from_millis(100);
37 thread::sleep(duration);
38 format!("Waited {duration:?}\nTransaction will be dropped")
39}
40
41#[get("/performance/random")]
42fn performance_rng() -> String {
43 let duration = Duration::from_millis(100);
44 thread::sleep(duration);
45 format!("Waited {duration:?}\nTransaction MIGHT be dropped")
46}
47
48#[launch]
49fn rocket() -> Rocket<Build> {
50 let rocket_instance = rocket::build();
51 let default_rate = rocket_instance
53 .figment()
54 .extract_inner::<f32>("sentry_traces_sample_rate")
55 .unwrap();
56 let traces_sampler = move |ctx: &TransactionContext| -> f32 {
57 match ctx.name() {
58 "GET /performance/skip" => {
59 log::warn!("Dropping performance transaction");
60 0.
61 }
62 "GET /performance/random" => {
63 log::warn!("Sending performance transaction half the time");
64 0.
65 }
66 _ => {
67 log::warn!("Sending performance transaction using default rate");
68 default_rate
69 }
70 }
71 };
72 let rocket_sentry = RocketSentry::builder()
73 .traces_sampler(Arc::new(traces_sampler))
74 .build();
75 rocket_instance.attach(rocket_sentry).mount(
76 "/",
77 routes![
78 performance,
79 performance_with_id,
80 performance_with_parameter,
81 performance_skipped,
82 performance_rng,
83 ],
84 )
85}