use prometheus::{HistogramOpts, HistogramVec, IntCounter, IntCounterVec, Registry};
use std::sync::Arc;
#[derive(Clone)]
pub struct Metrics {
pub registry: Arc<Registry>,
pub requests_total: IntCounterVec,
pub request_duration_seconds: HistogramVec,
pub sync_operations_total: IntCounterVec,
pub library_operations_total: IntCounterVec,
pub rate_limit_hits: IntCounter,
pub auth_failures: IntCounter,
}
impl Metrics {
pub fn new() -> Result<Self, prometheus::Error> {
let registry = Arc::new(Registry::new());
let requests_total = IntCounterVec::new(
prometheus::opts!("snip_sync_requests_total", "Total number of requests"),
&["method"],
)?;
let request_duration_seconds = HistogramVec::new(
HistogramOpts::new(
"snip_sync_request_duration_seconds",
"Request duration in seconds",
)
.buckets(vec![
0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
]),
&["method"],
)?;
let sync_operations_total = IntCounterVec::new(
prometheus::opts!(
"snip_sync_sync_operations_total",
"Total number of sync operations"
),
&["direction"],
)?;
let library_operations_total = IntCounterVec::new(
prometheus::opts!(
"snip_sync_library_operations_total",
"Total number of library operations (create, list, delete)"
),
&["operation"],
)?;
let rate_limit_hits = IntCounter::new(
"snip_sync_rate_limit_hits_total",
"Total number of rate limit hits",
)?;
let auth_failures = IntCounter::new(
"snip_sync_auth_failures_total",
"Total number of authentication failures",
)?;
let reg = registry.clone();
reg.register(Box::new(requests_total.clone()))?;
let reg = registry.clone();
reg.register(Box::new(request_duration_seconds.clone()))?;
let reg = registry.clone();
reg.register(Box::new(sync_operations_total.clone()))?;
let reg = registry.clone();
reg.register(Box::new(library_operations_total.clone()))?;
let reg = registry.clone();
reg.register(Box::new(rate_limit_hits.clone()))?;
let reg = registry.clone();
reg.register(Box::new(auth_failures.clone()))?;
Ok(Self {
registry,
requests_total,
request_duration_seconds,
sync_operations_total,
library_operations_total,
rate_limit_hits,
auth_failures,
})
}
pub fn fallback() -> Self {
let registry = Arc::new(Registry::new());
Self {
registry,
requests_total: IntCounterVec::new(
prometheus::opts!("snip_sync_fallback_requests", "fallback"),
&["method"],
)
.unwrap(),
request_duration_seconds: HistogramVec::new(
HistogramOpts::new("snip_sync_fallback_duration", "fallback"),
&["method"],
)
.unwrap(),
sync_operations_total: IntCounterVec::new(
prometheus::opts!("snip_sync_fallback_sync_ops", "fallback"),
&["direction"],
)
.unwrap(),
library_operations_total: IntCounterVec::new(
prometheus::opts!("snip_sync_fallback_lib_ops", "fallback"),
&["operation"],
)
.unwrap(),
rate_limit_hits: IntCounter::new("snip_sync_fallback_rate_limit", "fallback").unwrap(),
auth_failures: IntCounter::new("snip_sync_fallback_auth_fail", "fallback").unwrap(),
}
}
}
impl Default for Metrics {
fn default() -> Self {
Self::fallback()
}
}