use anyhow::Result;
use crate::commands::query::OutputFormat;
use crate::output::{self, print_header};
pub async fn handle_metrics_show(format: &OutputFormat) -> Result<()> {
let text = ipfrs_interface::metrics::encode_metrics()
.map_err(|e| anyhow::anyhow!("Failed to encode metrics: {}", e))?;
if format.is_json() {
println!("{{");
println!(" \"format\": \"prometheus_text\",");
let escaped = text
.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n");
println!(" \"metrics\": \"{}\"", escaped);
println!("}}");
} else {
print_header("IPFRS Prometheus Metrics");
print!("{}", text);
}
Ok(())
}
pub async fn handle_metrics_reset() -> Result<()> {
output::info(
"Prometheus counters are monotonically increasing and cannot be reset at runtime.\n\
To reset all metrics, restart the IPFRS daemon process.",
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_metrics_show_text() {
let result = handle_metrics_show(&OutputFormat::Text).await;
assert!(result.is_ok(), "metrics show (text) failed: {:?}", result);
}
#[tokio::test]
async fn test_metrics_show_json() {
let result = handle_metrics_show(&OutputFormat::Json).await;
assert!(result.is_ok(), "metrics show (json) failed: {:?}", result);
}
#[tokio::test]
async fn test_metrics_reset() {
let result = handle_metrics_reset().await;
assert!(result.is_ok(), "metrics reset failed: {:?}", result);
}
}