spawn_access_control/
metrics.rs1#[cfg(feature = "metrics")]
2use prometheus::{
3 IntCounter, IntGauge, Histogram, HistogramOpts,
4 Registry, Opts
5};
6
7#[cfg(feature = "metrics")]
8pub struct MetricsCollector {
9 registry: Registry,
10 access_counter: IntCounter,
11 active_users: IntGauge,
12 response_times: Histogram,
13}
14
15#[cfg(feature = "metrics")]
16impl MetricsCollector {
17 pub fn new() -> Self {
18 let registry = Registry::new();
19
20 let access_counter = IntCounter::new(
21 "spawn_access_total",
22 "Total number of access attempts"
23 ).unwrap();
24
25 let active_users = IntGauge::new(
26 "spawn_active_users",
27 "Number of currently active users"
28 ).unwrap();
29
30 let response_times = Histogram::with_opts(
31 HistogramOpts::new(
32 "spawn_response_times",
33 "Response times in seconds"
34 )
35 ).unwrap();
36
37 registry.register(Box::new(access_counter.clone())).unwrap();
38 registry.register(Box::new(active_users.clone())).unwrap();
39 registry.register(Box::new(response_times.clone())).unwrap();
40
41 Self {
42 registry,
43 access_counter,
44 active_users,
45 response_times,
46 }
47 }
48
49 pub fn record_access(&self) {
50 self.access_counter.inc();
51 }
52
53 pub fn record_response_time(&self, duration_ms: f64) {
54 self.response_times.observe(duration_ms);
55 }
56}
57
58#[cfg(not(feature = "metrics"))]
60pub struct MetricsCollector;
61
62#[cfg(not(feature = "metrics"))]
63impl MetricsCollector {
64 pub fn new() -> Self {
65 Self
66 }
67
68 pub fn record_access(&self) {}
69 pub fn record_response_time(&self, _duration_ms: f64) {}
70}