koprogo_api/infrastructure/storage/
metrics.rs1use once_cell::sync::Lazy;
2use prometheus::{register_histogram_vec, register_int_counter_vec, HistogramVec, IntCounterVec};
3use std::time::Duration;
4
5static STORAGE_OPERATION_TOTAL: Lazy<IntCounterVec> = Lazy::new(|| {
6 register_int_counter_vec!(
7 "storage_operation_total",
8 "Total number of storage operations",
9 &["provider", "operation", "result"]
10 )
11 .expect("failed to register storage_operation_total counter")
12});
13
14static STORAGE_OPERATION_DURATION_SECONDS: Lazy<HistogramVec> = Lazy::new(|| {
15 register_histogram_vec!(
16 "storage_operation_duration_seconds",
17 "Storage operation duration in seconds",
18 &["provider", "operation"]
19 )
20 .expect("failed to register storage_operation_duration_seconds histogram")
21});
22
23pub fn record_storage_operation(
24 provider: &'static str,
25 operation: &'static str,
26 duration: Duration,
27 result: Result<(), &str>,
28) {
29 let status = match result {
30 Ok(_) => "success",
31 Err(_) => "error",
32 };
33
34 STORAGE_OPERATION_TOTAL
35 .with_label_values(&[provider, operation, status])
36 .inc();
37
38 STORAGE_OPERATION_DURATION_SECONDS
39 .with_label_values(&[provider, operation])
40 .observe(duration.as_secs_f64());
41}