|
| 1 | +use std::{thread, time::Duration}; |
| 2 | + |
1 | 3 | // Prometheus |
2 | 4 | use prometheus::{opts, register_int_counter, register_int_gauge, IntCounter, IntGauge}; |
3 | 5 |
|
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(); |
| 6 | +use warp::{Filter, Rejection, Reply}; |
60 | 7 |
|
61 | | - GAS_PRICE_USED_ON_LATEST_BATCH.set(0); |
| 8 | +#[derive(Clone, Debug)] |
| 9 | +pub struct BatcherMetrics { |
| 10 | + pub open_connections: IntGauge, |
| 11 | + pub received_proofs: IntCounter, |
| 12 | + pub sent_batches: IntCounter, |
| 13 | + pub reverted_batches: IntCounter, |
| 14 | + pub batcher_started: IntCounter, |
| 15 | + pub gas_price_used_on_latest_batch: IntGauge, |
62 | 16 | } |
63 | 17 |
|
64 | | -pub fn batcher_started() { |
65 | | - thread::sleep(time::Duration::from_secs(10)); |
66 | | - BATCHER_STARTED.inc(); |
| 18 | +impl BatcherMetrics { |
| 19 | + pub fn start(metrics_port: u16) -> anyhow::Result<Self> { |
| 20 | + let registry = prometheus::Registry::new(); |
| 21 | + |
| 22 | + let open_connections = register_int_gauge!(opts!("open_connections", "Open Connections"))?; |
| 23 | + let received_proofs = register_int_counter!(opts!("received_proofs", "Received Proofs"))?; |
| 24 | + let sent_batches = register_int_counter!(opts!("sent_batches", "Sent Batches"))?; |
| 25 | + let reverted_batches = |
| 26 | + register_int_counter!(opts!("reverted_batches", "Reverted Batches"))?; |
| 27 | + let batcher_started = register_int_counter!(opts!("batcher_started", "Batcher Started"))?; |
| 28 | + let gas_price_used_on_latest_batch = |
| 29 | + register_int_gauge!(opts!("gas_price_used_on_latest_batch", "Gas Price"))?; |
| 30 | + |
| 31 | + registry.register(Box::new(open_connections.clone()))?; |
| 32 | + registry.register(Box::new(received_proofs.clone()))?; |
| 33 | + registry.register(Box::new(sent_batches.clone()))?; |
| 34 | + registry.register(Box::new(reverted_batches.clone()))?; |
| 35 | + registry.register(Box::new(batcher_started.clone()))?; |
| 36 | + |
| 37 | + let metrics_route = warp::path!("metrics") |
| 38 | + .and(warp::any().map(move || registry.clone())) |
| 39 | + .and_then(BatcherMetrics::metrics_handler); |
| 40 | + |
| 41 | + tokio::task::spawn(async move { |
| 42 | + warp::serve(metrics_route) |
| 43 | + .run(([0, 0, 0, 0], metrics_port)) |
| 44 | + .await; |
| 45 | + }); |
| 46 | + |
| 47 | + Ok(Self { |
| 48 | + open_connections, |
| 49 | + received_proofs, |
| 50 | + sent_batches, |
| 51 | + reverted_batches, |
| 52 | + batcher_started, |
| 53 | + gas_price_used_on_latest_batch, |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + pub async fn metrics_handler(registry: prometheus::Registry) -> Result<impl Reply, Rejection> { |
| 58 | + use prometheus::Encoder; |
| 59 | + let encoder = prometheus::TextEncoder::new(); |
| 60 | + |
| 61 | + let mut buffer = Vec::new(); |
| 62 | + if let Err(e) = encoder.encode(®istry.gather(), &mut buffer) { |
| 63 | + eprintln!("could not encode prometheus metrics: {}", e); |
| 64 | + }; |
| 65 | + let res = String::from_utf8(buffer.clone()) |
| 66 | + .inspect_err(|e| eprintln!("prometheus metrics could not be parsed correctly: {e}")) |
| 67 | + .unwrap_or_default(); |
| 68 | + buffer.clear(); |
| 69 | + |
| 70 | + Ok(res) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn inc_batcher_restart(&self) { |
| 74 | + // Sleep for 2 seconds to allow prometheus to start and set the metrics with default intial values. |
| 75 | + // If prometheus is not ready, the metrics will directly be set to 1 and prometheus will not be able to display the correct increment. |
| 76 | + thread::sleep(Duration::from_secs(2)); |
| 77 | + self.batcher_started.inc(); |
| 78 | + } |
67 | 79 | } |
0 commit comments