Skip to main content

dynamo_runtime/metrics/
work_handler_pool.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Worker-pool saturation metrics for the shared TCP server (backend side).
5//!
6//! These metrics expose queue buildup between `work_tx.send()` and dispatcher
7//! pickup, and permit starvation in the bounded worker pool.
8
9use 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
19/// Current items sitting in the bounded mpsc work queue awaiting dispatcher
20/// pickup. Incremented on successful `work_tx.send()` and decremented immediately
21/// after `work_rx.recv()`. Permit-acquire wait is NOT counted here — see
22/// `WORK_HANDLER_PERMIT_WAIT_SECONDS`.
23pub 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
31/// Configured capacity of the bounded work queue. Static; set once at server init.
32pub 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
40/// Requests rejected before TCP worker dispatch because the bounded work queue
41/// was full or the dispatcher channel was closed.
42pub 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
50/// Time spent waiting to acquire a worker-pool permit. Normal operation is
51/// sub-millisecond; saturation pushes p99 into seconds.
52pub 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
65/// Current number of active worker-pool tasks (permits in use).
66pub 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
74/// Configured worker-pool capacity (total permits). Static; set once at server init.
75pub 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
83/// Guards idempotency for the `MetricsRegistry` registration path.
84static METRICS_REGISTERED: OnceCell<()> = OnceCell::new();
85
86/// Register worker-pool saturation metrics with the given registry. Idempotent.
87pub 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}