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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Metrics for GPU plotter

use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::gauge::Gauge;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
use prometheus_client::registry::{Registry, Unit};
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicI64, AtomicU64};

/// Metrics for GPU plotter
#[derive(Debug)]
pub(super) struct GpuPlotterMetrics {
    pub(super) sector_downloading_time: Histogram,
    pub(super) sector_encoding_time: Histogram,
    pub(super) sector_plotting_time: Histogram,
    pub(super) sector_downloading: Counter<u64, AtomicU64>,
    pub(super) sector_downloaded: Counter<u64, AtomicU64>,
    pub(super) sector_encoding: Counter<u64, AtomicU64>,
    pub(super) sector_encoded: Counter<u64, AtomicU64>,
    pub(super) sector_plotting: Counter<u64, AtomicU64>,
    pub(super) sector_plotted: Counter<u64, AtomicU64>,
    pub(super) sector_plotting_error: Counter<u64, AtomicU64>,
    pub(super) plotting_capacity_used: Gauge<i64, AtomicI64>,
}

impl GpuPlotterMetrics {
    /// Create new instance
    pub(super) fn new(
        registry: &mut Registry,
        subtype: &str,
        total_capacity: NonZeroUsize,
    ) -> Self {
        let registry = registry
            .sub_registry_with_prefix("plotter")
            .sub_registry_with_label(("kind".into(), format!("gpu-{subtype}").into()));

        let sector_downloading_time = Histogram::new(exponential_buckets(0.1, 2.0, 15));
        registry.register_with_unit(
            "sector_downloading_time",
            "Sector downloading time",
            Unit::Seconds,
            sector_downloading_time.clone(),
        );

        let sector_encoding_time = Histogram::new(exponential_buckets(0.1, 2.0, 15));
        registry.register_with_unit(
            "sector_encoding_time",
            "Sector encoding time",
            Unit::Seconds,
            sector_encoding_time.clone(),
        );

        let sector_plotting_time = Histogram::new(exponential_buckets(0.1, 2.0, 15));
        registry.register_with_unit(
            "sector_plotting_time",
            "Sector plotting time",
            Unit::Seconds,
            sector_plotting_time.clone(),
        );

        let sector_downloading = Counter::default();
        registry.register_with_unit(
            "sector_downloading_counter",
            "Number of sectors being downloaded",
            Unit::Other("Sectors".to_string()),
            sector_downloading.clone(),
        );

        let sector_downloaded = Counter::default();
        registry.register_with_unit(
            "sector_downloaded_counter",
            "Number of downloaded sectors",
            Unit::Other("Sectors".to_string()),
            sector_downloaded.clone(),
        );

        let sector_encoding = Counter::default();
        registry.register_with_unit(
            "sector_encoding_counter",
            "Number of sectors being encoded",
            Unit::Other("Sectors".to_string()),
            sector_encoding.clone(),
        );

        let sector_encoded = Counter::default();
        registry.register_with_unit(
            "sector_encoded_counter",
            "Number of encoded sectors",
            Unit::Other("Sectors".to_string()),
            sector_encoded.clone(),
        );

        let sector_plotting = Counter::default();
        registry.register_with_unit(
            "sector_plotting_counter",
            "Number of sectors being plotted",
            Unit::Other("Sectors".to_string()),
            sector_plotting.clone(),
        );

        let sector_plotted = Counter::default();
        registry.register_with_unit(
            "sector_plotted_counter",
            "Number of plotted sectors",
            Unit::Other("Sectors".to_string()),
            sector_plotted.clone(),
        );

        let sector_plotting_error = Counter::default();
        registry.register_with_unit(
            "sector_plotting_error_counter",
            "Number of sector plotting failures",
            Unit::Other("Sectors".to_string()),
            sector_plotting_error.clone(),
        );

        let plotting_capacity_total = Gauge::<i64, AtomicI64>::default();
        plotting_capacity_total.set(total_capacity.get() as i64);
        registry.register_with_unit(
            "plotting_capacity_total",
            "Plotting capacity total",
            Unit::Other("Sectors".to_string()),
            plotting_capacity_total,
        );

        let plotting_capacity_used = Gauge::default();
        registry.register_with_unit(
            "plotting_capacity_used",
            "Plotting capacity used",
            Unit::Other("Sectors".to_string()),
            plotting_capacity_used.clone(),
        );

        Self {
            sector_downloading_time,
            sector_encoding_time,
            sector_plotting_time,
            sector_downloading,
            sector_downloaded,
            sector_encoding,
            sector_encoded,
            sector_plotting,
            sector_plotted,
            sector_plotting_error,
            plotting_capacity_used,
        }
    }
}