fedimint_server/
metrics.rs

1pub(crate) mod jsonrpsee;
2
3use std::sync::LazyLock;
4
5use fedimint_core::backup::ClientBackupKeyPrefix;
6use fedimint_core::db::{Database, IDatabaseTransactionOpsCoreTyped};
7use fedimint_metrics::prometheus::{
8    register_histogram_vec_with_registry, register_int_gauge_vec_with_registry,
9    register_int_gauge_with_registry, HistogramVec, IntCounterVec, IntGauge, IntGaugeVec,
10};
11use fedimint_metrics::{
12    histogram_opts, opts, register_histogram_with_registry, register_int_counter_vec_with_registry,
13    Histogram, REGISTRY,
14};
15use futures::StreamExt as _;
16
17pub static TX_ELEMS_BUCKETS: LazyLock<Vec<f64>> = LazyLock::new(|| {
18    vec![
19        1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0, 1000.0, 2000.0, 5000.0,
20    ]
21});
22pub(crate) static CONSENSUS_TX_PROCESSED_INPUTS: LazyLock<Histogram> = LazyLock::new(|| {
23    register_histogram_with_registry!(
24        histogram_opts!(
25            "consensus_tx_processed_inputs",
26            "Number of inputs processed in a transaction",
27            TX_ELEMS_BUCKETS.clone()
28        ),
29        REGISTRY
30    )
31    .unwrap()
32});
33pub(crate) static CONSENSUS_TX_PROCESSED_OUTPUTS: LazyLock<Histogram> = LazyLock::new(|| {
34    register_histogram_with_registry!(
35        histogram_opts!(
36            "consensus_tx_processed_outputs",
37            "Number of outputs processed in a transaction",
38            TX_ELEMS_BUCKETS.clone()
39        ),
40        REGISTRY
41    )
42    .unwrap()
43});
44pub(crate) static CONSENSUS_ITEMS_PROCESSED_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
45    register_int_counter_vec_with_registry!(
46        opts!(
47            "consensus_items_processed_total",
48            "Number of consensus items processed in the consensus",
49        ),
50        &["peer_id"],
51        REGISTRY
52    )
53    .unwrap()
54});
55pub(crate) static CONSENSUS_ITEM_PROCESSING_DURATION_SECONDS: LazyLock<HistogramVec> =
56    LazyLock::new(|| {
57        register_histogram_vec_with_registry!(
58            histogram_opts!(
59                "consensus_item_processing_duration_seconds",
60                "Duration of processing a consensus item",
61            ),
62            &["peer_id"],
63            REGISTRY
64        )
65        .unwrap()
66    });
67pub(crate) static CONSENSUS_ITEM_PROCESSING_MODULE_AUDIT_DURATION_SECONDS: LazyLock<HistogramVec> =
68    LazyLock::new(|| {
69        register_histogram_vec_with_registry!(
70            histogram_opts!(
71                "consensus_item_processing_module_audit_duration_seconds",
72                "Duration of processing a consensus item",
73            ),
74            &["module_id", "module_kind"],
75            REGISTRY
76        )
77        .unwrap()
78    });
79
80pub(crate) static CONSENSUS_ORDERING_LATENCY_SECONDS: LazyLock<Histogram> = LazyLock::new(|| {
81    register_histogram_with_registry!(
82        histogram_opts!(
83            "consensus_ordering_latency_seconds",
84            "Duration of ordering a batch of consensus items",
85        ),
86        REGISTRY
87    )
88    .unwrap()
89});
90
91pub(crate) static JSONRPC_API_REQUEST_DURATION_SECONDS: LazyLock<HistogramVec> =
92    LazyLock::new(|| {
93        register_histogram_vec_with_registry!(
94            histogram_opts!(
95                "jsonrpc_api_request_duration_seconds",
96                "Duration of processing an rpc request",
97            ),
98            &["method"],
99            REGISTRY
100        )
101        .unwrap()
102    });
103pub(crate) static JSONRPC_API_REQUEST_RESPONSE_CODE: LazyLock<IntCounterVec> =
104    LazyLock::new(|| {
105        register_int_counter_vec_with_registry!(
106            opts!(
107                "jsonrpc_api_request_response_code_total",
108                "Count of response counts and types",
109            ),
110            &["method", "code", "type"],
111            REGISTRY
112        )
113        .unwrap()
114    });
115pub(crate) static CONSENSUS_SESSION_COUNT: LazyLock<IntGauge> = LazyLock::new(|| {
116    register_int_gauge_with_registry!(
117        opts!(
118            "consensus_session_count",
119            "Fedimint consensus session count",
120        ),
121        REGISTRY
122    )
123    .unwrap()
124});
125pub(crate) static CONSENSUS_PEER_CONTRIBUTION_SESSION_IDX: LazyLock<IntGaugeVec> =
126    LazyLock::new(|| {
127        register_int_gauge_vec_with_registry!(
128            opts!(
129                "consensus_peer_contribution_session_idx",
130                "Latest contribution session idx by peer_id",
131            ),
132            &["self_id", "peer_id"],
133            REGISTRY
134        )
135        .unwrap()
136    });
137pub(crate) static BACKUP_WRITE_SIZE_BYTES: LazyLock<Histogram> = LazyLock::new(|| {
138    register_histogram_with_registry!(
139        histogram_opts!(
140            "backup_write_size_bytes",
141            "Size of every backup being written",
142            vec![1.0, 10., 100., 1_000., 5_000., 10_000., 50_000., 100_000., 1_000_000.]
143        ),
144        REGISTRY
145    )
146    .unwrap()
147});
148pub(crate) static STORED_BACKUPS_COUNT: LazyLock<IntGauge> = LazyLock::new(|| {
149    register_int_gauge_with_registry!(
150        opts!("stored_backups_count", "Total amount of backups stored",),
151        REGISTRY
152    )
153    .unwrap()
154});
155pub(crate) static PEER_CONNECT_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
156    register_int_counter_vec_with_registry!(
157        opts!("peer_connect_total", "Number of times peer (re/)connected",),
158        &["self_id", "peer_id", "direction"],
159        REGISTRY
160    )
161    .unwrap()
162});
163pub(crate) static PEER_DISCONNECT_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
164    register_int_counter_vec_with_registry!(
165        opts!(
166            "peer_disconnect_total",
167            "Number of times peer (re/)connected",
168        ),
169        &["self_id", "peer_id"],
170        REGISTRY
171    )
172    .unwrap()
173});
174pub(crate) static PEER_MESSAGES_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
175    register_int_counter_vec_with_registry!(
176        opts!("peer_messages_total", "Messages with the peer",),
177        &["self_id", "peer_id", "direction"],
178        REGISTRY
179    )
180    .unwrap()
181});
182
183/// Initialize gauges or other metrics that need eager initialization on start,
184/// e.g. because they are triggered infrequently.
185pub(crate) async fn initialize_gauge_metrics(db: &Database) {
186    STORED_BACKUPS_COUNT.set(
187        db.begin_transaction_nc()
188            .await
189            .find_by_prefix(&ClientBackupKeyPrefix)
190            .await
191            .count()
192            .await as i64,
193    );
194}