brokkr-broker::metrics Rust
Functions
brokkr-broker::metrics::init
pub
#![allow(unused)]
fn main() {
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
#![allow(unused)]
fn main() {
pub fn init() {
// Force initialization of all lazy static metrics by accessing them
let _ = &*HTTP_REQUESTS_TOTAL;
let _ = &*HTTP_REQUEST_DURATION_SECONDS;
let _ = &*DATABASE_QUERIES_TOTAL;
let _ = &*DATABASE_QUERY_DURATION_SECONDS;
let _ = &*ACTIVE_AGENTS;
let _ = &*AGENT_HEARTBEAT_AGE_SECONDS;
let _ = &*STACKS_TOTAL;
let _ = &*DEPLOYMENT_OBJECTS_TOTAL;
}
}
brokkr-broker::metrics::encode_metrics
pub
#![allow(unused)]
fn main() {
fn encode_metrics () -> String
}
Encodes all registered metrics in Prometheus text format
Returns:
Returns a String containing all metrics in Prometheus exposition format
Source
#![allow(unused)]
fn main() {
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")
}
}
brokkr-broker::metrics::record_http_request
pub
#![allow(unused)]
fn main() {
fn record_http_request (endpoint : & str , method : & str , status : u16 , duration_seconds : f64)
}
Records an HTTP request metric
Parameters:
| Name | Type | Description |
|---|---|---|
endpoint | - | The request path/endpoint |
method | - | The HTTP method (GET, POST, etc.) |
status | - | The response status code |
duration_seconds | - | The request duration in seconds |
Source
#![allow(unused)]
fn main() {
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);
}
}
brokkr-broker::metrics::normalize_endpoint
private
#![allow(unused)]
fn main() {
fn normalize_endpoint (path : & str) -> String
}
Normalizes an endpoint path to reduce cardinality Replaces UUIDs and numeric IDs with placeholders
Source
#![allow(unused)]
fn main() {
fn normalize_endpoint(path: &str) -> String {
let parts: Vec<&str> = path.split('/').collect();
let normalized: Vec<String> = parts
.iter()
.map(|part| {
// Check if it's a UUID (36 chars with hyphens) or purely numeric
if (part.len() == 36 && part.chars().filter(|c| *c == '-').count() == 4)
|| (!part.is_empty() && part.chars().all(|c| c.is_ascii_digit()))
{
":id".to_string()
} else {
(*part).to_string()
}
})
.collect();
normalized.join("/")
}
}
brokkr-broker::metrics::record_db_query
pub
#![allow(unused)]
fn main() {
fn record_db_query (query_type : & str , duration_seconds : f64)
}
Records a database query metric
Parameters:
| Name | Type | Description |
|---|---|---|
query_type | - | The type of query (select, insert, update, delete) |
duration_seconds | - | The query duration in seconds |
Source
#![allow(unused)]
fn main() {
pub fn record_db_query(query_type: &str, duration_seconds: f64) {
DATABASE_QUERIES_TOTAL
.with_label_values(&[query_type])
.inc();
DATABASE_QUERY_DURATION_SECONDS
.with_label_values(&[query_type])
.observe(duration_seconds);
}
}
brokkr-broker::metrics::set_active_agents
pub
#![allow(unused)]
fn main() {
fn set_active_agents (count : i64)
}
Updates the active agents gauge
Source
#![allow(unused)]
fn main() {
pub fn set_active_agents(count: i64) {
ACTIVE_AGENTS.set(count);
}
}
brokkr-broker::metrics::set_stacks_total
pub
#![allow(unused)]
fn main() {
fn set_stacks_total (count : i64)
}
Updates the total stacks gauge
Source
#![allow(unused)]
fn main() {
pub fn set_stacks_total(count: i64) {
STACKS_TOTAL.set(count);
}
}
brokkr-broker::metrics::set_deployment_objects_total
pub
#![allow(unused)]
fn main() {
fn set_deployment_objects_total (count : i64)
}
Updates the total deployment objects gauge
Source
#![allow(unused)]
fn main() {
pub fn set_deployment_objects_total(count: i64) {
DEPLOYMENT_OBJECTS_TOTAL.set(count);
}
}
brokkr-broker::metrics::set_agent_heartbeat_age
pub
#![allow(unused)]
fn main() {
fn set_agent_heartbeat_age (agent_id : & str , agent_name : & str , age_seconds : f64)
}
Updates the heartbeat age for a specific agent
Source
#![allow(unused)]
fn main() {
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);
}
}