ipfrs_cli/commands/
metrics.rs1use anyhow::Result;
10
11use crate::commands::query::OutputFormat;
12use crate::output::{self, print_header};
13
14pub async fn handle_metrics_show(format: &OutputFormat) -> Result<()> {
23 let text = ipfrs_interface::metrics::encode_metrics()
24 .map_err(|e| anyhow::anyhow!("Failed to encode metrics: {}", e))?;
25
26 if format.is_json() {
27 println!("{{");
29 println!(" \"format\": \"prometheus_text\",");
30 let escaped = text
32 .replace('\\', "\\\\")
33 .replace('"', "\\\"")
34 .replace('\n', "\\n");
35 println!(" \"metrics\": \"{}\"", escaped);
36 println!("}}");
37 } else {
38 print_header("IPFRS Prometheus Metrics");
39 print!("{}", text);
40 }
41
42 Ok(())
43}
44
45pub async fn handle_metrics_reset() -> Result<()> {
51 output::info(
52 "Prometheus counters are monotonically increasing and cannot be reset at runtime.\n\
53 To reset all metrics, restart the IPFRS daemon process.",
54 );
55 Ok(())
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[tokio::test]
63 async fn test_metrics_show_text() {
64 let result = handle_metrics_show(&OutputFormat::Text).await;
66 assert!(result.is_ok(), "metrics show (text) failed: {:?}", result);
67 }
68
69 #[tokio::test]
70 async fn test_metrics_show_json() {
71 let result = handle_metrics_show(&OutputFormat::Json).await;
72 assert!(result.is_ok(), "metrics show (json) failed: {:?}", result);
73 }
74
75 #[tokio::test]
76 async fn test_metrics_reset() {
77 let result = handle_metrics_reset().await;
78 assert!(result.is_ok(), "metrics reset failed: {:?}", result);
79 }
80}