-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmetrics.rs
More file actions
54 lines (43 loc) · 1.68 KB
/
metrics.rs
File metadata and controls
54 lines (43 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use prometheus::{self, opts, register_gauge};
use warp::{reject::Rejection, reply::Reply, Filter};
#[derive(Clone, Debug)]
pub struct PaymentsPollerMetrics {
pub last_processed_block: prometheus::Gauge,
}
impl PaymentsPollerMetrics {
pub fn start(metrics_port: u16) -> anyhow::Result<Self> {
let registry = prometheus::Registry::new();
let last_processed_block = register_gauge!(opts!(
"last_processed_block",
"Last processed block by poller"
))?;
registry.register(Box::new(last_processed_block.clone()))?;
let metrics_route = warp::path!("metrics")
.and(warp::any().map(move || registry.clone()))
.and_then(PaymentsPollerMetrics::metrics_handler);
tokio::task::spawn(async move {
warp::serve(metrics_route)
.run(([0, 0, 0, 0], metrics_port))
.await;
});
Ok(Self {
last_processed_block,
})
}
pub async fn metrics_handler(registry: prometheus::Registry) -> Result<impl Reply, Rejection> {
use prometheus::Encoder;
let encoder = prometheus::TextEncoder::new();
let mut buffer = Vec::new();
if let Err(e) = encoder.encode(®istry.gather(), &mut buffer) {
eprintln!("could not encode prometheus metrics: {e}");
};
let res = String::from_utf8(buffer.clone())
.inspect_err(|e| eprintln!("prometheus metrics could not be parsed correctly: {e}"))
.unwrap_or_default();
buffer.clear();
Ok(res)
}
pub fn register_last_processed_block(&self, value: u64) {
self.last_processed_block.set(value as f64);
}
}