Skip to content

Commit cbcaef7

Browse files
authored
Add --metrics flag to enable/disable metrics collection (#2497)
**Motivation** Currently, metrics are always initialized in the application, even if they're not needed. This can cause unnecessary resource usage and potential overhead. By making metrics optional through a command-line flag, users can have more control over their node's resource consumption and behavior. **Description** This PR adds a new --metrics command-line flag that allows users to explicitly enable or disable metrics collection and exposition. When metrics are disabled, the metrics server is not started, saving resources. Key changes: - Add a new metrics_enabled boolean flag to the Options struct - Update the init_metrics function to check this flag before starting the metrics server - Modify both the main ethrex command and the L2 command to conditionally initialize metrics - Update the L2 Makefile to explicitly enable metrics - Update documentation to include information about the new flag
1 parent 81d0efd commit cbcaef7

File tree

7 files changed

+34
-5
lines changed

7 files changed

+34
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,12 @@ Node options:
207207
--network <GENESIS_FILE_PATH>
208208
Alternatively, the name of a known network can be provided instead to use its preset genesis file and include its preset bootnodes. The networks currently supported include holesky, sepolia, hoodi and mainnet.
209209
210+
[env: ETHREX_NETWORK=]
211+
210212
--datadir <DATABASE_DIRECTORY>
211213
If the datadir is the word `memory`, ethrex will use the `InMemory Engine`.
212214
215+
[env: ETHREX_DATADIR=]
213216
[default: ethrex]
214217
215218
--force
@@ -219,14 +222,19 @@ Node options:
219222
[default: 0.0.0.0]
220223
221224
--metrics.port <PROMETHEUS_METRICS_PORT>
225+
[env: ETHREX_METRICS_PORT=]
222226
[default: 9090]
223227
228+
--metrics
229+
Enable metrics collection and exposition
230+
224231
--dev
225232
If set it will be considered as `true`. The Binary has to be built with the `dev` feature enabled.
226233
227234
--evm <EVM_BACKEND>
228235
Has to be `levm` or `revm`
229236
237+
[env: ETHREX_EVM=]
230238
[default: revm]
231239
232240
--log.level <LOG_LEVEL>
@@ -266,11 +274,13 @@ RPC options:
266274
--http.addr <ADDRESS>
267275
Listening address for the http rpc server.
268276
277+
[env: ETHREX_HTTP_ADDR=]
269278
[default: localhost]
270279
271280
--http.port <PORT>
272281
Listening port for the http rpc server.
273282
283+
[env: ETHREX_HTTP_PORT=]
274284
[default: 8545]
275285
276286
--authrpc.addr <ADDRESS>

cmd/ethrex/cli.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ pub struct Options {
8080
env = "ETHREX_METRICS_PORT"
8181
)]
8282
pub metrics_port: String,
83+
#[arg(
84+
long = "metrics",
85+
action = ArgAction::SetTrue,
86+
help = "Enable metrics collection and exposition",
87+
help_heading = "Node options"
88+
)]
89+
pub metrics_enabled: bool,
8390
#[arg(
8491
long = "dev",
8592
action = ArgAction::SetTrue,
@@ -201,6 +208,7 @@ impl Default for Options {
201208
syncmode: Default::default(),
202209
metrics_addr: "0.0.0.0".to_owned(),
203210
metrics_port: Default::default(),
211+
metrics_enabled: Default::default(),
204212
dev: Default::default(),
205213
evm: Default::default(),
206214
force: false,

cmd/ethrex/ethrex.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ async fn main() -> eyre::Result<()> {
5858
)
5959
.await;
6060

61-
init_metrics(&opts, tracker.clone());
61+
if opts.metrics_enabled {
62+
init_metrics(&opts, tracker.clone());
63+
}
6264

6365
cfg_if::cfg_if! {
6466
if #[cfg(feature = "dev")] {

cmd/ethrex/initializers.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ pub fn init_tracing(opts: &Options) {
5656
}
5757

5858
pub fn init_metrics(opts: &Options, tracker: TaskTracker) {
59+
tracing::info!(
60+
"Starting metrics server on {}:{}",
61+
opts.metrics_addr,
62+
opts.metrics_port
63+
);
5964
let metrics_api = ethrex_metrics::api::start_prometheus_metrics_api(
6065
opts.metrics_addr.clone(),
6166
opts.metrics_port.clone(),

cmd/ethrex/l2.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ impl Command {
152152
)
153153
.await;
154154

155-
// TODO: Add a --metrics flag to enable metrics.
156-
init_metrics(&opts.node_opts, tracker.clone());
155+
// Initialize metrics if enabled
156+
if opts.node_opts.metrics_enabled {
157+
init_metrics(&opts.node_opts, tracker.clone());
158+
}
157159

158160
if opts.node_opts.p2p_enabled {
159161
init_network(

crates/blockchain/metrics/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ If a new dashboard is designed just for the L1 or L2, it can be mounted only in
55

66
To run the node with metrics, the next steps should be followed:
77
1. Build the `ethrex` binary with the `metrics` feature enabled.
8-
2. Set the `--metrics.port` cli arg of the ethrex binary to match the port defined in `metrics/provisioning/prometheus/prometheus*.yaml`
9-
3. Run the docker containers, example with the L2:
8+
2. Enable metrics by using the `--metrics` flag when starting the node.
9+
3. Set the `--metrics.port` cli arg of the ethrex binary to match the port defined in `metrics/provisioning/prometheus/prometheus*.yaml`
10+
4. Run the docker containers, example with the L2:
1011

1112
```sh
1213
docker compose -f docker-compose-metrics.yaml -f docker-compose-metrics-l2.override.yaml up

crates/l2/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ init-l2-no-metrics: ## 🚀 Initializes an L2 Lambda ethrex Client
148148
--network ${L2_GENESIS_FILE_PATH} \
149149
--http.port ${L2_PORT} \
150150
--http.addr 0.0.0.0 \
151+
--metrics \
151152
--metrics.port ${L2_PROMETHEUS_METRICS_PORT} \
152153
--evm levm \
153154
--datadir ${ethrex_L2_DEV_LIBMDBX}

0 commit comments

Comments
 (0)