use arc_swap::ArcSwap;
use once_cell::sync::Lazy;
use prometheus::{IntCounterVec, IntGaugeVec, Registry};
pub(crate) const NODE_LABEL: &str = "node";
pub(crate) const CONTROL_PLANE_LABEL: &str = "control_plane";
pub(crate) const TYPE_LABEL: &str = "type";
static REGISTRY: Lazy<ArcSwap<Registry>> = Lazy::new(|| {
ArcSwap::new(std::sync::Arc::new(
Registry::new_custom(Some("quilkin".into()), None).unwrap(),
))
});
pub fn set_registry(registry: std::sync::Arc<Registry>) {
REGISTRY.store(registry);
}
pub fn registry() -> arc_swap::Guard<std::sync::Arc<Registry>> {
REGISTRY.load()
}
pub(crate) fn active_control_planes(control_plane: &str) -> prometheus::IntGauge {
static ACTIVE_CONTROL_PLANES: Lazy<IntGaugeVec> = Lazy::new(|| {
prometheus::register_int_gauge_vec_with_registry! {
prometheus::opts! {
"active_control_planes",
"Total number of active control plane connections",
},
&[CONTROL_PLANE_LABEL],
crate::metrics::registry(),
}
.unwrap()
});
ACTIVE_CONTROL_PLANES.with_label_values(&[control_plane])
}
pub(crate) fn delta_discovery_requests(node: &str, type_url: &str) -> prometheus::IntCounter {
static DELTA_DISCOVERY_REQUESTS: Lazy<IntCounterVec> = Lazy::new(|| {
prometheus::register_int_counter_vec_with_registry! {
prometheus::opts! {
"delta_discovery_requests",
"Total number of xDS delta discovery requests",
},
&[NODE_LABEL, TYPE_LABEL],
crate::metrics::registry(),
}
.unwrap()
});
DELTA_DISCOVERY_REQUESTS.with_label_values(&[node, type_url])
}
pub(crate) fn delta_discovery_responses(
control_plane: &str,
type_url: &str,
) -> prometheus::IntCounter {
pub(crate) static DELTA_DISCOVERY_RESPONSES: Lazy<IntCounterVec> = Lazy::new(|| {
prometheus::register_int_counter_vec_with_registry! {
prometheus::opts! {
"delta_discovery_responses",
"Total number of xDS delta discovery responses",
},
&[CONTROL_PLANE_LABEL, TYPE_LABEL],
crate::metrics::registry(),
}
.unwrap()
});
DELTA_DISCOVERY_RESPONSES.with_label_values(&[control_plane, type_url])
}
pub(crate) fn acks(control_plane: &str, type_url: &str) -> prometheus::IntCounter {
static ACKS: Lazy<IntCounterVec> = Lazy::new(|| {
prometheus::register_int_counter_vec_with_registry! {
prometheus::opts! {
"xds_acks",
"Total number of xDS ACKs",
},
&[CONTROL_PLANE_LABEL, TYPE_LABEL],
crate::metrics::registry(),
}
.unwrap()
});
ACKS.with_label_values(&[control_plane, type_url])
}
pub(crate) fn nacks(control_plane: &str, type_url: &str) -> prometheus::IntCounter {
static NACKS: Lazy<IntCounterVec> = Lazy::new(|| {
prometheus::register_int_counter_vec_with_registry! {
prometheus::opts! {
"xds_nacks",
"Total number of xDS NACKs",
},
&[CONTROL_PLANE_LABEL, TYPE_LABEL],
crate::metrics::registry(),
}
.unwrap()
});
NACKS.with_label_values(&[control_plane, type_url])
}
pub struct StreamConnectionMetrics {
control_plane: String,
}
impl StreamConnectionMetrics {
pub fn new(control_plane: String) -> Self {
self::active_control_planes(&control_plane).inc();
Self { control_plane }
}
}
impl Drop for StreamConnectionMetrics {
fn drop(&mut self) {
self::active_control_planes(&self.control_plane).dec();
}
}