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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use once_cell::sync::OnceCell;
use prometheus::{HistogramVec, IntCounterVec, IntGaugeVec, Registry};

#[derive(Clone)]
pub struct WmsMetrics {
    pub wms_requests_counter: IntCounterVec,
    pub fcgi_client_pool_available: Vec<IntGaugeVec>,
    pub fcgi_client_wait_seconds: Vec<HistogramVec>,
    pub fcgi_cache_count: Vec<IntGaugeVec>,
    pub fcgi_cache_hit: Vec<IntGaugeVec>,
}

impl Default for WmsMetrics {
    fn default() -> Self {
        WmsMetrics {
            wms_requests_counter: IntCounterVec::new(
                prometheus::core::Opts::new("dummy", "dummy"),
                &[],
            )
            .unwrap(),
            fcgi_client_pool_available: Vec::new(),
            fcgi_client_wait_seconds: Vec::new(),
            fcgi_cache_count: Vec::new(),
            fcgi_cache_hit: Vec::new(),
        }
    }
}

pub fn wms_metrics(num_fcgi_processes: usize) -> &'static WmsMetrics {
    static METRICS: OnceCell<WmsMetrics> = OnceCell::new();
    METRICS.get_or_init(|| {
        let opts = prometheus::opts!("requests_total", "Total number of WMS requests")
            .namespace("bbox_wms");
        let wms_requests_counter =
            IntCounterVec::new(opts, &["endpoint", "backend", "fcgino"]).unwrap();
        let fcgi_cache_count = (0..num_fcgi_processes)
            .map(|fcgino| {
                let opts = prometheus::opts!(
                    format!("fcgi_cache_count_{fcgino}"),
                    "FCGI project cache size"
                )
                .namespace("bbox_wms");
                IntGaugeVec::new(opts, &["backend"]).unwrap()
            })
            .collect();
        let fcgi_client_pool_available = (0..num_fcgi_processes)
            .map(|fcgino| {
                let opts = prometheus::opts!(
                    format!("fcgi_client_pool_available_{fcgino}"),
                    "FCGI clients available in pool"
                )
                .namespace("bbox_wms");
                IntGaugeVec::new(opts, &["backend"]).unwrap()
            })
            .collect();
        let fcgi_client_wait_seconds = (0..num_fcgi_processes)
            .map(|fcgino| {
                let opts = prometheus::opts!(
                    format!("fcgi_client_wait_seconds_{fcgino}"),
                    "FCGI client wait time"
                )
                .namespace("bbox_wms");
                HistogramVec::new(opts.into(), &["backend"]).unwrap()
            })
            .collect();
        let fcgi_cache_hit = (0..num_fcgi_processes)
            .map(|fcgino| {
                let opts =
                    prometheus::opts!(format!("fcgi_cache_hit_{fcgino}"), "FCGI project cache hit")
                        .namespace("bbox_wms");
                IntGaugeVec::new(opts, &["backend"]).unwrap()
            })
            .collect();
        WmsMetrics {
            wms_requests_counter,
            fcgi_client_pool_available,
            fcgi_client_wait_seconds,
            fcgi_cache_count,
            fcgi_cache_hit,
        }
    })
}

pub fn register_metrics(prometheus: &Registry, metrics: &WmsMetrics) {
    // We use the Prometheus API, using
    // https://docs.rs/opentelemetry-prometheus/
    // would be more portable
    prometheus
        .register(Box::new(metrics.wms_requests_counter.clone()))
        .unwrap();
    for no in 0..metrics.fcgi_cache_count.len() {
        prometheus
            .register(Box::new(metrics.fcgi_client_pool_available[no].clone()))
            .unwrap();
        prometheus
            .register(Box::new(metrics.fcgi_client_wait_seconds[no].clone()))
            .unwrap();
        prometheus
            .register(Box::new(metrics.fcgi_cache_count[no].clone()))
            .unwrap();
        prometheus
            .register(Box::new(metrics.fcgi_cache_hit[no].clone()))
            .unwrap();
    }
}