use once_cell::sync::{Lazy, OnceCell};
use prometheus::{Histogram, HistogramOpts, IntCounter, IntGauge};
use super::prometheus_names::{name_prefix, work_handler};
use crate::MetricsRegistry;
fn work_handler_metric_name(suffix: &str) -> String {
format!("{}_{}", name_prefix::WORK_HANDLER, suffix)
}
pub static WORK_HANDLER_QUEUE_DEPTH: Lazy<IntGauge> = Lazy::new(|| {
IntGauge::new(
work_handler_metric_name(work_handler::QUEUE_DEPTH),
"Current items in the bounded work queue awaiting dispatcher pickup",
)
.expect("work_handler_queue_depth gauge")
});
pub static WORK_HANDLER_QUEUE_CAPACITY: Lazy<IntGauge> = Lazy::new(|| {
IntGauge::new(
work_handler_metric_name(work_handler::QUEUE_CAPACITY),
"Configured capacity of the bounded work queue",
)
.expect("work_handler_queue_capacity gauge")
});
pub static WORK_HANDLER_ENQUEUE_REJECTED_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
IntCounter::new(
work_handler_metric_name(work_handler::ENQUEUE_REJECTED_TOTAL),
"Times enqueuing work failed because the dispatcher channel was closed",
)
.expect("work_handler_enqueue_rejected_total counter")
});
pub static WORK_HANDLER_PERMIT_WAIT_SECONDS: Lazy<Histogram> = Lazy::new(|| {
Histogram::with_opts(
HistogramOpts::new(
work_handler_metric_name(work_handler::PERMIT_WAIT_SECONDS),
"Time spent waiting for a worker-pool permit (seconds)",
)
.buckets(vec![
0.0001, 0.001, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0, 30.0, 60.0,
]),
)
.expect("work_handler_permit_wait_seconds histogram")
});
pub static WORK_HANDLER_POOL_ACTIVE_TASKS: Lazy<IntGauge> = Lazy::new(|| {
IntGauge::new(
work_handler_metric_name(work_handler::POOL_ACTIVE_TASKS),
"Current number of active worker-pool tasks (permits in use)",
)
.expect("work_handler_pool_active_tasks gauge")
});
pub static WORK_HANDLER_POOL_CAPACITY: Lazy<IntGauge> = Lazy::new(|| {
IntGauge::new(
work_handler_metric_name(work_handler::POOL_CAPACITY),
"Configured worker-pool capacity (total permits)",
)
.expect("work_handler_pool_capacity gauge")
});
static METRICS_REGISTERED: OnceCell<()> = OnceCell::new();
pub fn ensure_work_handler_pool_metrics_registered(registry: &MetricsRegistry) {
let _ = METRICS_REGISTERED.get_or_init(|| {
registry.add_metric_or_warn(
Box::new(WORK_HANDLER_QUEUE_DEPTH.clone()),
"work_handler_queue_depth",
);
registry.add_metric_or_warn(
Box::new(WORK_HANDLER_QUEUE_CAPACITY.clone()),
"work_handler_queue_capacity",
);
registry.add_metric_or_warn(
Box::new(WORK_HANDLER_ENQUEUE_REJECTED_TOTAL.clone()),
"work_handler_enqueue_rejected_total",
);
registry.add_metric_or_warn(
Box::new(WORK_HANDLER_PERMIT_WAIT_SECONDS.clone()),
"work_handler_permit_wait_seconds",
);
registry.add_metric_or_warn(
Box::new(WORK_HANDLER_POOL_ACTIVE_TASKS.clone()),
"work_handler_pool_active_tasks",
);
registry.add_metric_or_warn(
Box::new(WORK_HANDLER_POOL_CAPACITY.clone()),
"work_handler_pool_capacity",
);
});
}