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    HistogramVec, IntCounterVec, IntGauge, IntGaugeVec, register_histogram_vec_with_registry,
9    register_int_gauge_vec_with_registry, register_int_gauge_with_registry,
10};
11use fedimint_metrics::{
12    Histogram, REGISTRY, histogram_opts, opts, register_histogram_with_registry,
13    register_int_counter_vec_with_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![
143                1.0, 10., 100., 1_000., 5_000., 10_000., 50_000., 100_000., 1_000_000.
144            ]
145        ),
146        REGISTRY
147    )
148    .unwrap()
149});
150pub(crate) static STORED_BACKUPS_COUNT: LazyLock<IntGauge> = LazyLock::new(|| {
151    register_int_gauge_with_registry!(
152        opts!("stored_backups_count", "Total amount of backups stored",),
153        REGISTRY
154    )
155    .unwrap()
156});
157pub(crate) static PEER_CONNECT_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
158    register_int_counter_vec_with_registry!(
159        opts!("peer_connect_total", "Number of times peer (re/)connected",),
160        &["self_id", "peer_id", "direction"],
161        REGISTRY
162    )
163    .unwrap()
164});
165pub(crate) static PEER_DISCONNECT_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
166    register_int_counter_vec_with_registry!(
167        opts!(
168            "peer_disconnect_total",
169            "Number of times peer (re/)connected",
170        ),
171        &["self_id", "peer_id"],
172        REGISTRY
173    )
174    .unwrap()
175});
176pub(crate) static PEER_MESSAGES_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
177    register_int_counter_vec_with_registry!(
178        opts!("peer_messages_total", "Messages with the peer",),
179        &["self_id", "peer_id", "direction"],
180        REGISTRY
181    )
182    .unwrap()
183});
184
185/// Initialize gauges or other metrics that need eager initialization on start,
186/// e.g. because they are triggered infrequently.
187pub(crate) async fn initialize_gauge_metrics(db: &Database) {
188    STORED_BACKUPS_COUNT.set(
189        db.begin_transaction_nc()
190            .await
191            .find_by_prefix(&ClientBackupKeyPrefix)
192            .await
193            .count()
194            .await as i64,
195    );
196}