dynamo_runtime/metrics/
work_handler_pool.rs1use once_cell::sync::{Lazy, OnceCell};
10use prometheus::{Histogram, HistogramOpts, IntCounter, IntGauge};
11
12use super::prometheus_names::{name_prefix, work_handler};
13use crate::MetricsRegistry;
14
15fn work_handler_metric_name(suffix: &str) -> String {
16 format!("{}_{}", name_prefix::WORK_HANDLER, suffix)
17}
18
19pub static WORK_HANDLER_QUEUE_DEPTH: Lazy<IntGauge> = Lazy::new(|| {
24 IntGauge::new(
25 work_handler_metric_name(work_handler::QUEUE_DEPTH),
26 "Current items in the bounded work queue awaiting dispatcher pickup",
27 )
28 .expect("work_handler_queue_depth gauge")
29});
30
31pub static WORK_HANDLER_QUEUE_CAPACITY: Lazy<IntGauge> = Lazy::new(|| {
33 IntGauge::new(
34 work_handler_metric_name(work_handler::QUEUE_CAPACITY),
35 "Configured capacity of the bounded work queue",
36 )
37 .expect("work_handler_queue_capacity gauge")
38});
39
40pub static WORK_HANDLER_ENQUEUE_REJECTED_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
43 IntCounter::new(
44 work_handler_metric_name(work_handler::ENQUEUE_REJECTED_TOTAL),
45 "Requests rejected before TCP worker dispatch because the work queue was full or closed",
46 )
47 .expect("work_handler_enqueue_rejected_total counter")
48});
49
50pub static WORK_HANDLER_PERMIT_WAIT_SECONDS: Lazy<Histogram> = Lazy::new(|| {
53 Histogram::with_opts(
54 HistogramOpts::new(
55 work_handler_metric_name(work_handler::PERMIT_WAIT_SECONDS),
56 "Time spent waiting for a worker-pool permit (seconds)",
57 )
58 .buckets(vec![
59 0.0001, 0.001, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0, 30.0, 60.0,
60 ]),
61 )
62 .expect("work_handler_permit_wait_seconds histogram")
63});
64
65pub static WORK_HANDLER_POOL_ACTIVE_TASKS: Lazy<IntGauge> = Lazy::new(|| {
67 IntGauge::new(
68 work_handler_metric_name(work_handler::POOL_ACTIVE_TASKS),
69 "Current number of active worker-pool tasks (permits in use)",
70 )
71 .expect("work_handler_pool_active_tasks gauge")
72});
73
74pub static WORK_HANDLER_POOL_CAPACITY: Lazy<IntGauge> = Lazy::new(|| {
76 IntGauge::new(
77 work_handler_metric_name(work_handler::POOL_CAPACITY),
78 "Configured worker-pool capacity (total permits)",
79 )
80 .expect("work_handler_pool_capacity gauge")
81});
82
83static METRICS_REGISTERED: OnceCell<()> = OnceCell::new();
85
86pub fn ensure_work_handler_pool_metrics_registered(registry: &MetricsRegistry) {
88 let _ = METRICS_REGISTERED.get_or_init(|| {
89 registry.add_metric_or_warn(
90 Box::new(WORK_HANDLER_QUEUE_DEPTH.clone()),
91 "work_handler_queue_depth",
92 );
93 registry.add_metric_or_warn(
94 Box::new(WORK_HANDLER_QUEUE_CAPACITY.clone()),
95 "work_handler_queue_capacity",
96 );
97 registry.add_metric_or_warn(
98 Box::new(WORK_HANDLER_ENQUEUE_REJECTED_TOTAL.clone()),
99 "work_handler_enqueue_rejected_total",
100 );
101 registry.add_metric_or_warn(
102 Box::new(WORK_HANDLER_PERMIT_WAIT_SECONDS.clone()),
103 "work_handler_permit_wait_seconds",
104 );
105 registry.add_metric_or_warn(
106 Box::new(WORK_HANDLER_POOL_ACTIVE_TASKS.clone()),
107 "work_handler_pool_active_tasks",
108 );
109 registry.add_metric_or_warn(
110 Box::new(WORK_HANDLER_POOL_CAPACITY.clone()),
111 "work_handler_pool_capacity",
112 );
113 });
114}