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