spawn_access_control/
metrics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#[cfg(feature = "metrics")]
use prometheus::{
    IntCounter, IntGauge, Histogram, HistogramOpts,
    Registry, Opts
};

#[cfg(feature = "metrics")]
pub struct MetricsCollector {
    registry: Registry,
    access_counter: IntCounter,
    active_users: IntGauge,
    response_times: Histogram,
}

#[cfg(feature = "metrics")]
impl MetricsCollector {
    pub fn new() -> Self {
        let registry = Registry::new();
        
        let access_counter = IntCounter::new(
            "spawn_access_total",
            "Total number of access attempts"
        ).unwrap();

        let active_users = IntGauge::new(
            "spawn_active_users",
            "Number of currently active users"
        ).unwrap();

        let response_times = Histogram::with_opts(
            HistogramOpts::new(
                "spawn_response_times",
                "Response times in seconds"
            )
        ).unwrap();

        registry.register(Box::new(access_counter.clone())).unwrap();
        registry.register(Box::new(active_users.clone())).unwrap();
        registry.register(Box::new(response_times.clone())).unwrap();

        Self {
            registry,
            access_counter,
            active_users,
            response_times,
        }
    }

    pub fn record_access(&self) {
        self.access_counter.inc();
    }

    pub fn record_response_time(&self, duration_ms: f64) {
        self.response_times.observe(duration_ms);
    }
}

// Metrics özelliği devre dışıysa boş implementasyon
#[cfg(not(feature = "metrics"))]
pub struct MetricsCollector;

#[cfg(not(feature = "metrics"))]
impl MetricsCollector {
    pub fn new() -> Self {
        Self
    }

    pub fn record_access(&self) {}
    pub fn record_response_time(&self, _duration_ms: f64) {}
}