|
| 1 | +// Prometheus |
| 2 | +use prometheus::{opts, register_int_counter, register_int_gauge, IntCounter, IntGauge}; |
| 3 | + |
| 4 | +use warp::{Rejection, Reply}; |
| 5 | + |
| 6 | +use once_cell::sync::Lazy; |
| 7 | +use std::{thread, time}; |
| 8 | + |
| 9 | +// Prometheus setup |
| 10 | +pub static BATCHER_STARTED: Lazy<IntCounter> = |
| 11 | + Lazy::new(|| register_int_counter!(opts!("batcher_started", "Batcher Started")).unwrap()); |
| 12 | + |
| 13 | +pub static OPEN_CONNECTIONS: Lazy<IntGauge> = |
| 14 | + Lazy::new(|| register_int_gauge!(opts!("open_connections", "Open Connections")).unwrap()); |
| 15 | + |
| 16 | +pub static RECEIVED_PROOFS: Lazy<IntCounter> = |
| 17 | + Lazy::new(|| register_int_counter!(opts!("received_proofs", "Received Proofs")).unwrap()); |
| 18 | + |
| 19 | +pub static SENT_BATCHES: Lazy<IntCounter> = |
| 20 | + Lazy::new(|| register_int_counter!(opts!("sent_batches", "Sent Batches")).unwrap()); |
| 21 | + |
| 22 | +pub static REVERTED_BATCHES: Lazy<IntCounter> = |
| 23 | + Lazy::new(|| register_int_counter!(opts!("reverted_batches", "Reverted Batches")).unwrap()); |
| 24 | + |
| 25 | +pub static GAS_PRICE_USED_ON_LATEST_BATCH: Lazy<IntGauge> = Lazy::new(|| { |
| 26 | + register_int_gauge!(opts!("gas_price_used_on_latest_batch", "Gas Price")).unwrap() |
| 27 | +}); |
| 28 | + |
| 29 | +// so Prometheus can collect our metrics. |
| 30 | +pub async fn metrics_handler() -> Result<impl Reply, Rejection> { |
| 31 | + use prometheus::Encoder; |
| 32 | + let encoder = prometheus::TextEncoder::new(); |
| 33 | + |
| 34 | + let mut buffer = Vec::new(); |
| 35 | + if let Err(e) = encoder.encode(&prometheus::gather(), &mut buffer) { |
| 36 | + eprintln!("could not encode prometheus metrics: {}", e); |
| 37 | + }; |
| 38 | + let res = match String::from_utf8(buffer.clone()) { |
| 39 | + Ok(v) => v, |
| 40 | + Err(e) => { |
| 41 | + eprintln!("prometheus metrics could not be from_utf8'd: {}", e); |
| 42 | + String::default() |
| 43 | + } |
| 44 | + }; |
| 45 | + buffer.clear(); |
| 46 | + |
| 47 | + Ok(res) |
| 48 | +} |
| 49 | + |
| 50 | +pub fn init_variables() { |
| 51 | + BATCHER_STARTED.reset(); |
| 52 | + |
| 53 | + OPEN_CONNECTIONS.set(0); |
| 54 | + |
| 55 | + RECEIVED_PROOFS.reset(); |
| 56 | + |
| 57 | + SENT_BATCHES.reset(); |
| 58 | + |
| 59 | + REVERTED_BATCHES.reset(); |
| 60 | + |
| 61 | + GAS_PRICE_USED_ON_LATEST_BATCH.set(0); |
| 62 | +} |
| 63 | + |
| 64 | +pub fn batcher_started() { |
| 65 | + thread::sleep(time::Duration::from_secs(10)); |
| 66 | + BATCHER_STARTED.inc(); |
| 67 | +} |
0 commit comments