Skip to content

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.

pub

fn poll_requests_total() -> &'static CounterVec

Broker 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
})
}

pub

fn poll_duration_seconds() -> &'static HistogramVec

Broker 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
})
}

pub

fn kubernetes_operations_total() -> &'static CounterVec

Kubernetes 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 HistogramVec

Kubernetes 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
})
}

pub

fn heartbeat_sent_total() -> &'static IntCounter

Heartbeat 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 Gauge

Last 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
})
}

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}"))
}