-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmetrics.rs
More file actions
110 lines (96 loc) · 3.4 KB
/
metrics.rs
File metadata and controls
110 lines (96 loc) · 3.4 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
//! Metrics of the thread pool.
use lazy_static::lazy_static;
use prometheus::*;
use std::sync::Mutex;
lazy_static! {
/// Elapsed time of each level in the multilevel task queue.
pub static ref MULTILEVEL_LEVEL_ELAPSED: IntCounterVec = IntCounterVec::new(
new_opts(
"multilevel_level_elapsed",
"elapsed time of each level in the multilevel task queue"
),
&["name", "level"]
)
.unwrap();
/// The chance that a level 0 task is scheduled to run.
pub static ref MULTILEVEL_LEVEL0_CHANCE: GaugeVec = GaugeVec::new(
new_opts(
"multilevel_level0_chance",
"the chance that a level 0 task is scheduled to run"
),
&["name"]
)
.unwrap();
/// The total duration of a task waiting in queue.
pub static ref TASK_WAIT_DURATION: HistogramVec = HistogramVec::new(
new_histogram_opts(
"yatp_task_wait_duration",
"Bucketed histogram of task wait time in queue",
exponential_buckets(0.00001, 2.0, 20).unwrap()
),
&["name"]
)
.unwrap();
/// Total execute duration of one task.
pub static ref TASK_EXEC_DURATION: HistogramVec = HistogramVec::new(
new_histogram_opts(
"yatp_task_exec_duration",
"Bucketed histogram of task total exec time",
exponential_buckets(0.00001, 2.0, 20).unwrap()
),
&["name"]
)
.unwrap();
/// Task's execution time duration one time slice.
pub static ref TASK_POLL_DURATION: HistogramVec = HistogramVec::new(
new_histogram_opts(
"yatp_task_poll_duration",
"Bucketed histogram of task exec time of a single poll per level",
exponential_buckets(0.00001, 2.0, 20).unwrap()
),
&["name", "level"]
)
.unwrap();
/// Histogrm for how many times a task be scheduled before finish.
pub static ref TASK_EXEC_TIMES: HistogramVec = HistogramVec::new(
new_histogram_opts(
"yatp_task_execute_times",
"Bucketed histogram of task exec times",
exponential_buckets(1.0, 2.0, 10).unwrap()
),
&["name"]
)
.unwrap();
/// Total time worker threads are active (not parked), in seconds.
pub static ref WORKER_ACTIVE_SECONDS: CounterVec = CounterVec::new(
new_opts(
"yatp_worker_active_seconds_total",
"total time worker threads are active (not parked) in seconds"
),
&["name"]
)
.unwrap();
static ref NAMESPACE: Mutex<Option<String>> = Mutex::new(None);
}
/// Sets the namespace used in the metrics. This function should be called before
/// the metrics are used or any thread pool is created.
///
/// The namespace is missing by default.
pub fn set_namespace(s: Option<impl Into<String>>) {
*NAMESPACE.lock().unwrap() = s.map(Into::into)
}
fn new_opts(name: &str, help: &str) -> Opts {
let mut opts = Opts::new(name, help);
if let Some(ref namespace) = *NAMESPACE.lock().unwrap() {
opts = opts.namespace(namespace);
}
opts
}
fn new_histogram_opts(name: &str, help: &str, buckets: Vec<f64>) -> HistogramOpts {
let mut opts = HistogramOpts::new(name, help).buckets(buckets);
if let Some(ref namespace) = *NAMESPACE.lock().unwrap() {
opts = opts.namespace(namespace);
}
opts
}