brokkr-agent::metrics
Rust
Metrics Module
This module provides Prometheus metrics for the Brokkr Agent. It exposes metrics about broker polling, Kubernetes operations, and agent health.
Functions
Section titled “Functions”fn poll_requests_total
Section titled “fn poll_requests_total”pub
fn poll_requests_total() -> &'static CounterVecBroker poll request counter Labels: status (success/error)
Source
pub fn poll_requests_total() -> &'static CounterVec { static COUNTER: OnceLock<CounterVec> = OnceLock::new(); COUNTER.get_or_init(|| { let opts = Opts::new( "brokkr_agent_poll_requests_total", "Total number of broker poll requests", ); let counter = CounterVec::new(opts, &["status"]).expect("Failed to create poll requests counter"); registry() .register(Box::new(counter.clone())) .expect("Failed to register poll requests counter"); counter })}fn poll_duration_seconds
Section titled “fn poll_duration_seconds”pub
fn poll_duration_seconds() -> &'static HistogramVecBroker poll duration histogram
Source
pub fn poll_duration_seconds() -> &'static HistogramVec { static HISTOGRAM: OnceLock<HistogramVec> = OnceLock::new(); HISTOGRAM.get_or_init(|| { let opts = HistogramOpts::new( "brokkr_agent_poll_duration_seconds", "Broker poll request latency distribution in seconds", ) .buckets(vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0]); let histogram = HistogramVec::new(opts, &[]).expect("Failed to create poll duration histogram"); registry() .register(Box::new(histogram.clone())) .expect("Failed to register poll duration histogram"); histogram })}fn kubernetes_operations_total
Section titled “fn kubernetes_operations_total”pub
fn kubernetes_operations_total() -> &'static CounterVecKubernetes operations counter Labels: operation (apply/delete/get/list)
Source
pub fn kubernetes_operations_total() -> &'static CounterVec { static COUNTER: OnceLock<CounterVec> = OnceLock::new(); COUNTER.get_or_init(|| { let opts = Opts::new( "brokkr_agent_kubernetes_operations_total", "Total number of Kubernetes API operations by type", ); let counter = CounterVec::new(opts, &["operation"]) .expect("Failed to create Kubernetes operations counter"); registry() .register(Box::new(counter.clone())) .expect("Failed to register Kubernetes operations counter"); counter })}fn kubernetes_operation_duration_seconds
Section titled “fn kubernetes_operation_duration_seconds”pub
fn kubernetes_operation_duration_seconds() -> &'static HistogramVecKubernetes operation duration histogram Labels: operation (apply/delete/get/list)
Source
pub fn kubernetes_operation_duration_seconds() -> &'static HistogramVec { static HISTOGRAM: OnceLock<HistogramVec> = OnceLock::new(); HISTOGRAM.get_or_init(|| { let opts = HistogramOpts::new( "brokkr_agent_kubernetes_operation_duration_seconds", "Kubernetes operation latency distribution in seconds", ) .buckets(vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]); let histogram = HistogramVec::new(opts, &["operation"]) .expect("Failed to create Kubernetes operation duration histogram"); registry() .register(Box::new(histogram.clone())) .expect("Failed to register Kubernetes operation duration histogram"); histogram })}fn heartbeat_sent_total
Section titled “fn heartbeat_sent_total”pub
fn heartbeat_sent_total() -> &'static IntCounterHeartbeat sent counter
Source
pub fn heartbeat_sent_total() -> &'static IntCounter { static COUNTER: OnceLock<IntCounter> = OnceLock::new(); COUNTER.get_or_init(|| { let opts = Opts::new( "brokkr_agent_heartbeat_sent_total", "Total number of heartbeats sent to broker", ); let counter = IntCounter::with_opts(opts).expect("Failed to create heartbeat counter"); registry() .register(Box::new(counter.clone())) .expect("Failed to register heartbeat counter"); counter })}fn last_successful_poll_timestamp
Section titled “fn last_successful_poll_timestamp”pub
fn last_successful_poll_timestamp() -> &'static GaugeLast successful poll timestamp (Unix timestamp)
Source
pub fn last_successful_poll_timestamp() -> &'static Gauge { static GAUGE: OnceLock<Gauge> = OnceLock::new(); GAUGE.get_or_init(|| { let opts = Opts::new( "brokkr_agent_last_successful_poll_timestamp", "Unix timestamp of last successful broker poll", ); let gauge = Gauge::with_opts(opts).expect("Failed to create last poll timestamp gauge"); registry() .register(Box::new(gauge.clone())) .expect("Failed to register last poll timestamp gauge"); gauge })}fn encode_metrics
Section titled “fn encode_metrics”pub
fn encode_metrics() -> Result<String, String>Encodes all registered metrics in Prometheus text format
Returns:
Returns a String containing all metrics in Prometheus exposition format
Source
pub fn encode_metrics() -> Result<String, String> { let encoder = TextEncoder::new(); let metric_families = registry().gather(); let mut buffer = vec![]; encoder .encode(&metric_families, &mut buffer) .map_err(|e| format!("failed to encode metrics: {e}"))?; String::from_utf8(buffer).map_err(|e| format!("metrics are not valid UTF-8: {e}"))}