Skip to content

brokkr-broker::metrics

Rust

Metrics Module

This module provides Prometheus metrics for the Brokkr Broker. It exposes metrics about HTTP requests and system state.

pub

fn record_database_query(duration_seconds: Option<f64>, failed: bool)

Records one finished database query.

Parameters:

Name Type Description
duration_seconds Option<f64> Time from query start to finish, or None when the start was not seen (the query is still counted)
failed bool Whether diesel reported an error for the query
Source
pub fn record_database_query(duration_seconds: Option<f64>, failed: bool) {
if failed {
DATABASE_QUERIES_ERROR.inc();
} else {
DATABASE_QUERIES_OK.inc();
}
if let Some(seconds) = duration_seconds {
DATABASE_QUERY_DURATION_SECONDS.observe(seconds);
}
}

pub

fn set_database_pool_state(connections: u32, idle: u32, max_size: u32)

Sets the pool gauges from an r2d2 pool state snapshot.

Source
pub fn set_database_pool_state(connections: u32, idle: u32, max_size: u32) {
let in_use = connections.saturating_sub(idle);
DATABASE_POOL_CONNECTIONS
.with_label_values(&["idle"])
.set(i64::from(idle));
DATABASE_POOL_CONNECTIONS
.with_label_values(&["in_use"])
.set(i64::from(in_use));
DATABASE_POOL_MAX_CONNECTIONS.set(i64::from(max_size));
}

pub

fn ws_connected_agents() -> &'static IntGauge

Convenience accessors keep call sites short and avoid the static names leaking into call-site readability.

Source
pub fn ws_connected_agents() -> &'static IntGauge {
&WS_CONNECTED_AGENTS
}

pub

fn ws_messages_total(
direction: &str,
variant: &str,
) -> prometheus::IntCounter
Source
pub fn ws_messages_total(direction: &str, variant: &str) -> prometheus::IntCounter {
WS_MESSAGES_TOTAL.with_label_values(&[direction, variant])
}

pub

fn ws_live_subscribers() -> &'static IntGauge
Source
pub fn ws_live_subscribers() -> &'static IntGauge {
&WS_LIVE_SUBSCRIBERS
}

pub

fn fleet_live_subscribers() -> &'static IntGauge
Source
pub fn fleet_live_subscribers() -> &'static IntGauge {
&FLEET_LIVE_SUBSCRIBERS
}

pub

fn ws_log_eviction_runs_total() -> &'static IntCounter
Source
pub fn ws_log_eviction_runs_total() -> &'static IntCounter {
&WS_LOG_EVICTION_RUNS_TOTAL
}

pub

fn ws_telemetry_evicted_total(table: &str) -> prometheus::IntCounter
Source
pub fn ws_telemetry_evicted_total(table: &str) -> prometheus::IntCounter {
WS_TELEMETRY_EVICTED_TOTAL.with_label_values(&[table])
}

fn record_​webhook_​decrypt_​failure

Section titled “fn record_​webhook_​decrypt_​failure”

pub

fn record_webhook_decrypt_failure(field: &str, path: &str)

Records a webhook decryption failure.

Parameters:

Name Type Description
field &str The encrypted field that failed: url or auth_header
path &str Which delivery path hit it: broker or agent
Source
pub fn record_webhook_decrypt_failure(field: &str, path: &str) {
WEBHOOK_DECRYPT_FAILURES_TOTAL
.with_label_values(&[field, path])
.inc();
}

pub

fn init()

Initializes all metrics by forcing lazy static evaluation

This ensures all metric definitions are registered with the registry before attempting to encode/export them. Should be called once at startup.

Source
pub fn init() {
// Force initialization of all lazy static metrics by accessing them
let _ = &*HTTP_REQUESTS_TOTAL;
let _ = &*HTTP_REQUEST_DURATION_SECONDS;
let _ = &*WS_CONNECTED_AGENTS;
let _ = &*WS_MESSAGES_TOTAL;
let _ = &*WS_LIVE_SUBSCRIBERS;
let _ = &*FLEET_LIVE_SUBSCRIBERS;
let _ = &*WS_LOG_EVICTION_RUNS_TOTAL;
let _ = &*WS_TELEMETRY_EVICTED_TOTAL;
let _ = &*WEBHOOK_DECRYPT_FAILURES_TOTAL;
let _ = &*ACTIVE_AGENTS;
let _ = &*AGENT_HEARTBEAT_AGE_SECONDS;
let _ = &*STACKS_TOTAL;
let _ = &*DEPLOYMENT_OBJECTS_TOTAL;
// Resolve both label children so both result series export as 0 before
// the first query: the console can then tell a new broker from an old one.
let _ = &*DATABASE_QUERIES_OK;
let _ = &*DATABASE_QUERIES_ERROR;
let _ = &*DATABASE_QUERY_DURATION_SECONDS;
let _ = &*DATABASE_POOL_CONNECTIONS;
let _ = &*DATABASE_POOL_MAX_CONNECTIONS;
}

pub

fn encode_metrics() -> 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() -> String {
// Ensure all metrics are initialized before encoding
init();
let encoder = TextEncoder::new();
let metric_families = REGISTRY.gather();
let mut buffer = vec![];
encoder
.encode(&metric_families, &mut buffer)
.expect("Failed to encode metrics");
String::from_utf8(buffer).expect("Failed to convert metrics to UTF-8")
}

pub

fn record_http_request(
endpoint: &str,
method: &str,
status: u16,
duration_seconds: f64,
)

Records an HTTP request metric

Parameters:

Name Type Description
endpoint &str The request path/endpoint
method &str The HTTP method (GET, POST, etc.)
status u16 The response status code
duration_seconds f64 The request duration in seconds
Source
pub fn record_http_request(endpoint: &str, method: &str, status: u16, duration_seconds: f64) {
// Normalize endpoint to avoid high cardinality from path parameters
let normalized_endpoint = normalize_endpoint(endpoint);
let status_str = status.to_string();
HTTP_REQUESTS_TOTAL
.with_label_values(&[&normalized_endpoint, method, &status_str])
.inc();
HTTP_REQUEST_DURATION_SECONDS
.with_label_values(&[&normalized_endpoint, method])
.observe(duration_seconds);
}

pub

fn set_active_agents(count: i64)

Updates the active agents gauge

Source
pub fn set_active_agents(count: i64) {
ACTIVE_AGENTS.set(count);
}

pub

fn set_stacks_total(count: i64)

Updates the total stacks gauge

Source
pub fn set_stacks_total(count: i64) {
STACKS_TOTAL.set(count);
}

pub

fn set_deployment_objects_total(count: i64)

Updates the total deployment objects gauge

Source
pub fn set_deployment_objects_total(count: i64) {
DEPLOYMENT_OBJECTS_TOTAL.set(count);
}

pub

fn set_agent_heartbeat_age(
agent_id: &str,
agent_name: &str,
age_seconds: f64,
)

Updates the heartbeat age for a specific agent

Source
pub fn set_agent_heartbeat_age(agent_id: &str, agent_name: &str, age_seconds: f64) {
AGENT_HEARTBEAT_AGE_SECONDS
.with_label_values(&[agent_id, agent_name])
.set(age_seconds);
}
Name Type Value Description
DATABASE_QUERY_BUCKETS [f64; 13] [0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.... Bucket bounds of DATABASE_QUERY_DURATION_SECONDS, in seconds.
They run from 0.5 ms to 5 s. Most broker queries are point reads and small writes that finish in under 10 ms, so the low end is finer than the HTTP histogram’s; the top end catches lock waits and slow scans.