Skip to content

Commit 3a3306d

Browse files
committed
refactor: move gather_default_metrics to metrics crate
1 parent 9f31039 commit 3a3306d

5 files changed

Lines changed: 8 additions & 49 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/metrics/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ license.workspace = true
77

88
[dependencies]
99
prometheus.workspace = true
10+
thiserror.workspace = true

crates/common/metrics/src/lib.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Metrics utilities and prometheus re-exports for ethlambda.
22
3-
use std::time::Instant;
3+
pub mod gather;
4+
pub mod timing;
45

56
// Re-export prometheus types and macros we use
67
pub use prometheus::{
@@ -9,23 +10,6 @@ pub use prometheus::{
910
register_int_gauge, register_int_gauge_vec,
1011
};
1112

12-
/// A guard that records elapsed time to a histogram when dropped.
13-
pub struct TimingGuard {
14-
histogram: &'static Histogram,
15-
start: Instant,
16-
}
17-
18-
impl TimingGuard {
19-
pub fn new(histogram: &'static Histogram) -> Self {
20-
Self {
21-
histogram,
22-
start: Instant::now(),
23-
}
24-
}
25-
}
26-
27-
impl Drop for TimingGuard {
28-
fn drop(&mut self) {
29-
self.histogram.observe(self.start.elapsed().as_secs_f64());
30-
}
31-
}
13+
// Re-export commonly used items
14+
pub use gather::{GatherError, gather_default_metrics};
15+
pub use timing::TimingGuard;

crates/net/rpc/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ version.workspace = true
1313
axum = "0.8.1"
1414
tokio.workspace = true
1515
ethlambda-metrics.workspace = true
16-
thiserror.workspace = true
1716
tracing.workspace = true

crates/net/rpc/src/metrics.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::net::SocketAddr;
22

33
use axum::{Router, http::HeaderValue, response::IntoResponse, routing::get};
4-
use ethlambda_metrics::*;
5-
use thiserror::Error;
4+
use ethlambda_metrics::gather_default_metrics;
65
use tracing::warn;
76

87
pub async fn start_prometheus_metrics_api(address: SocketAddr) -> Result<(), std::io::Error> {
@@ -32,27 +31,3 @@ pub(crate) async fn get_metrics() -> impl IntoResponse {
3231
response.headers_mut().insert("content-type", content_type);
3332
response
3433
}
35-
36-
#[derive(Debug, Error)]
37-
pub enum Error {
38-
#[error("Prometheus error: {0}")]
39-
Prometheus(#[from] PrometheusError),
40-
#[error("UTF-8 conversion error: {0}")]
41-
FromUtf8(#[from] std::string::FromUtf8Error),
42-
}
43-
44-
/// Returns all metrics currently registered in Prometheus' default registry.
45-
///
46-
/// Both profiling and RPC metrics register with this default registry, and the
47-
/// metrics API surfaces them by calling this helper.
48-
pub fn gather_default_metrics() -> Result<String, Error> {
49-
let encoder = TextEncoder::new();
50-
let metric_families = gather();
51-
52-
let mut buffer = Vec::new();
53-
encoder.encode(&metric_families, &mut buffer)?;
54-
55-
let res = String::from_utf8(buffer)?;
56-
57-
Ok(res)
58-
}

0 commit comments

Comments
 (0)