Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ vergen = "9.0.6"
vergen-git2 = "1.0.7"
clap = { version = "4.5.53", features = ["derive", "env", "string"] }

# Metrics
metrics = { version = "0.24.3", default-features = false }
prometheus = { version = "0.14.0", default-features = false }
metrics-process = { version = "2.4.2", default-features = false }
metrics-exporter-prometheus = { version = "0.18.1", default-features = false }

# misc
url = "2.5.7"
lru = "0.16.2"
Expand All @@ -232,7 +238,6 @@ serde = "1.0.228"
rustls = "0.23.35"
httpmock = "0.8.2"
tracing = "0.1.43"
metrics = "0.24.3"
arc-swap = "1.7.1"
once_cell = "1.21.3"
itertools = "0.14.0"
Expand All @@ -250,7 +255,6 @@ either = { version = "1.15.0", default-features = false }
tokio-util = { version = "0.7.4", features = ["codec"] }
warp = "0.3.7"
flate2 = "1.0.37"
prometheus = "0.13.4"
ctor = "0.2"
dashmap = "6.1"
hex = "0.4"
Expand Down
9 changes: 7 additions & 2 deletions crates/shared/cli-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ workspace = true

[dependencies]
reth.workspace = true
tokio = { workspace = true, features = ["full"] }
eyre.workspace = true
clap = { workspace = true, features = ["derive"] }
tracing.workspace = true

tokio = { workspace = true, features = ["full"] }
clap = { workspace = true, features = ["derive"] }

# Metrics
metrics-process.workspace = true
metrics-exporter-prometheus = { workspace = true, features = ["http-listener"] }

[target.'cfg(unix)'.dependencies]
libc = "0.2"
3 changes: 3 additions & 0 deletions crates/shared/cli-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub use backtrace::Backtracing;
mod args;
pub use args::GlobalArgs;

mod prometheus;
pub use prometheus::PrometheusServer;

mod logging;
pub use logging::{LogFormat, LogRotation, LoggingArgs};

Expand Down
47 changes: 47 additions & 0 deletions crates/shared/cli-utils/src/prometheus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Utilities for spinning up a prometheus metrics server.

use std::{
net::{IpAddr, SocketAddr},
thread::{self, sleep},
time::Duration,
};

use metrics_exporter_prometheus::{BuildError, PrometheusBuilder};
use metrics_process::Collector;
use tracing::info;

/// A wrapper type that initializes a Prometheus metrics server.
#[derive(Debug, Clone, Copy)]
pub struct PrometheusServer;

impl PrometheusServer {
/// Initialize a Prometheus metrics server on the given address and port.
///
/// Optionally, an interval in seconds can be provided to specify how often system metrics
/// are collected. If not provided, defaults to 5 seconds.
pub fn init(addr: IpAddr, metrics_port: u16, interval: Option<u64>) -> Result<(), BuildError> {
let prometheus_addr = SocketAddr::from((addr, metrics_port));
let builder = PrometheusBuilder::new().with_http_listener(prometheus_addr);

builder.install()?;

// Initialise collector for system metrics e.g. CPU, memory, etc.
let collector = Collector::default();
collector.describe();

thread::spawn(move || {
loop {
collector.collect();
sleep(Duration::from_secs(interval.unwrap_or(5)));
}
});

info!(
target: "prometheus",
"Serving metrics at: http://{}",
prometheus_addr
);

Ok(())
}
}
Loading