1use prometheus::{
5    HistogramVec, IntCounter, IntCounterVec, IntGauge, IntGaugeVec, Registry,
6    register_histogram_vec_with_registry, register_int_counter_vec_with_registry,
7    register_int_counter_with_registry, register_int_gauge_vec_with_registry,
8    register_int_gauge_with_registry,
9};
10
11const FINE_GRAINED_LATENCY_SEC_BUCKETS: &[f64] = &[
12    0.001, 0.005, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9,
13    1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.5, 3.0, 3.5, 4.0, 5.0, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5,
14    10., 15., 20., 25., 30., 35., 40., 45., 50., 60., 70., 80., 90., 100., 120., 140., 160., 180.,
15    200., 250., 300., 350., 400.,
16];
17
18#[derive(Clone, Debug)]
19pub struct BridgeMetrics {
20    pub(crate) err_build_sui_transaction: IntCounter,
21    pub(crate) err_signature_aggregation: IntCounter,
22    pub(crate) err_signature_aggregation_too_many_failures: IntCounter,
23    pub(crate) err_sui_transaction_submission: IntCounter,
24    pub(crate) err_sui_transaction_submission_too_many_failures: IntCounter,
25    pub(crate) err_sui_transaction_execution: IntCounter,
26    pub(crate) requests_received: IntCounterVec,
27    pub(crate) requests_ok: IntCounterVec,
28    pub(crate) err_requests: IntCounterVec,
29    pub(crate) requests_inflight: IntGaugeVec,
30
31    pub(crate) last_synced_sui_checkpoints: IntGaugeVec,
32    pub(crate) last_finalized_eth_block: IntGauge,
33    pub(crate) last_synced_eth_blocks: IntGaugeVec,
34
35    pub(crate) sui_watcher_received_events: IntCounter,
36    pub(crate) sui_watcher_received_actions: IntCounter,
37    pub(crate) sui_watcher_unrecognized_events: IntCounter,
38    pub(crate) eth_watcher_received_events: IntCounter,
39    pub(crate) eth_watcher_received_actions: IntCounter,
40    pub(crate) eth_watcher_unrecognized_events: IntCounter,
41    pub(crate) action_executor_already_processed_actions: IntCounter,
42    pub(crate) action_executor_signing_queue_received_actions: IntCounter,
43    pub(crate) action_executor_signing_queue_skipped_actions: IntCounter,
44    pub(crate) action_executor_execution_queue_received_actions: IntCounter,
45    pub(crate) action_executor_execution_queue_skipped_actions_due_to_pausing: IntCounter,
46
47    pub(crate) last_observed_actions_seq_num: IntGaugeVec,
48
49    pub(crate) signer_with_cache_hit: IntCounterVec,
50    pub(crate) signer_with_cache_miss: IntCounterVec,
51
52    pub(crate) eth_rpc_queries: IntCounterVec,
53    pub(crate) eth_rpc_queries_latency: HistogramVec,
54
55    pub(crate) gas_coin_balance: IntGauge,
56
57    pub(crate) sui_rpc_errors: IntCounterVec,
58    pub(crate) observed_governance_actions: IntCounterVec,
59    pub(crate) current_bridge_voting_rights: IntGaugeVec,
60
61    pub(crate) auth_agg_ok_responses: IntCounterVec,
62    pub(crate) auth_agg_bad_responses: IntCounterVec,
63
64    pub(crate) sui_eth_token_transfer_approved: IntCounter,
65    pub(crate) sui_eth_token_transfer_claimed: IntCounter,
66    pub(crate) eth_sui_token_transfer_approved: IntCounter,
67    pub(crate) eth_sui_token_transfer_claimed: IntCounter,
68}
69
70impl BridgeMetrics {
71    pub fn new(registry: &Registry) -> Self {
72        Self {
73            err_build_sui_transaction: register_int_counter_with_registry!(
74                "bridge_err_build_sui_transaction",
75                "Total number of errors of building sui transactions",
76                registry,
77            )
78            .unwrap(),
79            err_signature_aggregation: register_int_counter_with_registry!(
80                "bridge_err_signature_aggregation",
81                "Total number of errors of aggregating validators signatures",
82                registry,
83            )
84            .unwrap(),
85            err_signature_aggregation_too_many_failures: register_int_counter_with_registry!(
86                "bridge_err_signature_aggregation_too_many_failures",
87                "Total number of continuous failures during validator signature aggregation",
88                registry,
89            )
90            .unwrap(),
91            err_sui_transaction_submission: register_int_counter_with_registry!(
92                "bridge_err_sui_transaction_submission",
93                "Total number of errors of submitting sui transactions",
94                registry,
95            )
96            .unwrap(),
97            err_sui_transaction_submission_too_many_failures: register_int_counter_with_registry!(
98                "bridge_err_sui_transaction_submission_too_many_failures",
99                "Total number of continuous failures to submitting sui transactions",
100                registry,
101            )
102            .unwrap(),
103            err_sui_transaction_execution: register_int_counter_with_registry!(
104                "bridge_err_sui_transaction_execution",
105                "Total number of failures of sui transaction execution",
106                registry,
107            )
108            .unwrap(),
109            requests_received: register_int_counter_vec_with_registry!(
110                "bridge_requests_received",
111                "Total number of requests received in Server, by request type",
112                &["type"],
113                registry,
114            )
115            .unwrap(),
116            requests_ok: register_int_counter_vec_with_registry!(
117                "bridge_requests_ok",
118                "Total number of ok requests, by request type",
119                &["type"],
120                registry,
121            )
122            .unwrap(),
123            err_requests: register_int_counter_vec_with_registry!(
124                "bridge_err_requests",
125                "Total number of erred requests, by request type",
126                &["type"],
127                registry,
128            )
129            .unwrap(),
130            requests_inflight: register_int_gauge_vec_with_registry!(
131                "bridge_requests_inflight",
132                "Total number of inflight requests, by request type",
133                &["type"],
134                registry,
135            )
136            .unwrap(),
137            sui_watcher_received_events: register_int_counter_with_registry!(
138                "bridge_sui_watcher_received_events",
139                "Total number of received events in sui watcher",
140                registry,
141            )
142            .unwrap(),
143            eth_watcher_received_events: register_int_counter_with_registry!(
144                "bridge_eth_watcher_received_events",
145                "Total number of received events in eth watcher",
146                registry,
147            )
148            .unwrap(),
149            sui_watcher_received_actions: register_int_counter_with_registry!(
150                "bridge_sui_watcher_received_actions",
151                "Total number of received actions in sui watcher",
152                registry,
153            )
154            .unwrap(),
155            eth_watcher_received_actions: register_int_counter_with_registry!(
156                "bridge_eth_watcher_received_actions",
157                "Total number of received actions in eth watcher",
158                registry,
159            )
160            .unwrap(),
161            sui_watcher_unrecognized_events: register_int_counter_with_registry!(
162                "bridge_sui_watcher_unrecognized_events",
163                "Total number of unrecognized events in sui watcher",
164                registry,
165            )
166            .unwrap(),
167            eth_watcher_unrecognized_events: register_int_counter_with_registry!(
168                "bridge_eth_watcher_unrecognized_events",
169                "Total number of unrecognized events in eth watcher",
170                registry,
171            )
172            .unwrap(),
173            action_executor_already_processed_actions: register_int_counter_with_registry!(
174                "bridge_action_executor_already_processed_actions",
175                "Total number of already processed actions action executor",
176                registry,
177            )
178            .unwrap(),
179            action_executor_signing_queue_received_actions: register_int_counter_with_registry!(
180                "bridge_action_executor_signing_queue_received_actions",
181                "Total number of received actions in action executor signing queue",
182                registry,
183            )
184            .unwrap(),
185            action_executor_signing_queue_skipped_actions: register_int_counter_with_registry!(
186                "bridge_action_executor_signing_queue_skipped_actions",
187                "Total number of skipped actions in action executor signing queue",
188                registry,
189            )
190            .unwrap(),
191            action_executor_execution_queue_received_actions: register_int_counter_with_registry!(
192                "bridge_action_executor_execution_queue_received_actions",
193                "Total number of received actions in action executor execution queue",
194                registry,
195            )
196            .unwrap(),
197            action_executor_execution_queue_skipped_actions_due_to_pausing: register_int_counter_with_registry!(
198                "bridge_action_executor_execution_queue_skipped_actions_due_to_pausing",
199                "Total number of skipped actions in action executor execution queue because of pausing",
200                registry,
201            )
202            .unwrap(),
203            gas_coin_balance: register_int_gauge_with_registry!(
204                "bridge_gas_coin_balance",
205                "Current balance of gas coin, in mist",
206                registry,
207            )
208            .unwrap(),
209            eth_rpc_queries: register_int_counter_vec_with_registry!(
210                "bridge_eth_rpc_queries",
211                "Total number of queries issued to eth provider, by request type",
212                &["type"],
213                registry,
214            )
215            .unwrap(),
216            eth_rpc_queries_latency: register_histogram_vec_with_registry!(
217                "bridge_eth_rpc_queries_latency",
218                "Latency of queries issued to eth provider, by request type",
219                &["type"],
220                FINE_GRAINED_LATENCY_SEC_BUCKETS.to_vec(),
221                registry,
222            )
223            .unwrap(),
224            last_synced_sui_checkpoints: register_int_gauge_vec_with_registry!(
225                "bridge_last_synced_sui_checkpoints",
226                "The latest sui checkpoints synced for each module",
227                &["module_name"],
228                registry,
229            )
230            .unwrap(),
231            last_synced_eth_blocks: register_int_gauge_vec_with_registry!(
232                "bridge_last_synced_eth_blocks",
233                "The latest synced eth blocks synced for each contract",
234                &["contract_address"],
235                registry,
236            )
237            .unwrap(),
238            last_finalized_eth_block: register_int_gauge_with_registry!(
239                "bridge_last_finalized_eth_block",
240                "The latest finalized eth block observed",
241                registry,
242            )
243            .unwrap(),
244            last_observed_actions_seq_num: register_int_gauge_vec_with_registry!(
245                "bridge_last_observed_actions_seq_num",
246                "The latest observed action sequence number per chain_id and action_type",
247                &["chain_id", "action_type"],
248                registry,
249            )
250            .unwrap(),
251            signer_with_cache_hit: register_int_counter_vec_with_registry!(
252                "bridge_signer_with_cache_hit",
253                "Total number of hit in signer's cache, by verifier type",
254                &["type"],
255                registry,
256            )
257            .unwrap(),
258            signer_with_cache_miss: register_int_counter_vec_with_registry!(
259                "bridge_signer_with_cache_miss",
260                "Total number of miss in signer's cache, by verifier type",
261                &["type"],
262                registry,
263            )
264            .unwrap(),
265            sui_rpc_errors: register_int_counter_vec_with_registry!(
266                "bridge_sui_rpc_errors",
267                "Total number of errors from sui RPC, by RPC method",
268                &["method"],
269                registry,
270            )
271            .unwrap(),
272            observed_governance_actions: register_int_counter_vec_with_registry!(
273                "bridge_observed_governance_actions",
274                "Total number of observed governance actions",
275                &["action_type", "chain_id"],
276                registry,
277            )
278            .unwrap(),
279            current_bridge_voting_rights: register_int_gauge_vec_with_registry!(
280                "current_bridge_voting_rights",
281                "Current voting power in the bridge committee",
282                &["authority"],
283                registry
284            )
285            .unwrap(),
286            auth_agg_ok_responses: register_int_counter_vec_with_registry!(
287                "bridge_auth_agg_ok_responses",
288                "Total number of ok response from auth agg",
289                &["authority"],
290                registry,
291            )
292            .unwrap(),
293            auth_agg_bad_responses: register_int_counter_vec_with_registry!(
294                "bridge_auth_agg_bad_responses",
295                "Total number of bad response from auth agg",
296                &["authority"],
297                registry,
298            )
299            .unwrap(),
300            sui_eth_token_transfer_approved: register_int_counter_with_registry!(
301                "bridge_sui_eth_token_transfer_approved",
302                "Total number of approved sui to eth token transfers (since metric introduced). \
303                Should be used to track rates rather than absolute values.",
304                registry,
305            )
306            .unwrap(),
307            sui_eth_token_transfer_claimed: register_int_counter_with_registry!(
308                "bridge_sui_eth_token_transfer_claimed",
309                "Total number of claimed sui to eth token transfers (since metric introduced). \
310                Should be used to track rates rather than absolute values.",
311                registry,
312            )
313            .unwrap(),
314            eth_sui_token_transfer_approved: register_int_counter_with_registry!(
315                "bridge_eth_sui_token_transfer_approved",
316                "Total number of approved eth to sui token transfers (since metric introduced). \
317                Should be used to track rates rather than absolute values.",
318                registry,
319            )
320            .unwrap(),
321            eth_sui_token_transfer_claimed: register_int_counter_with_registry!(
322                "bridge_eth_sui_token_transfer_claimed",
323                "Total number of claimed eth to sui token transfers (since metric introduced). \
324                Should be used to track rates rather than absolute values.",
325                registry,
326            )
327            .unwrap(),
328        }
329    }
330
331    pub fn new_for_testing() -> Self {
332        let registry = Registry::new();
333        Self::new(®istry)
334    }
335}