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