Skip to main content

reth_node_metrics/
process.rs

1//! This exposes the process CLI arguments over prometheus.
2use metrics::gauge;
3
4/// Registers the process CLI arguments as a prometheus metric.
5///
6/// This captures the flags passed to the binary via [`std::env::args`] and emits them as a
7/// `process_cli_args` gauge set to `1` with an `args` label containing flag names only.
8///
9/// Values are stripped to avoid leaking secrets (e.g. `--p2p.secret KEY` becomes `--p2p.secret`).
10pub fn register_process_metrics() {
11    let args: String = std::env::args()
12        .filter(|arg| arg.starts_with('-'))
13        .map(|arg| {
14            // Strip values from `--flag=value` style arguments
15            match arg.split_once('=') {
16                Some((flag, _)) => flag.to_string(),
17                None => arg,
18            }
19        })
20        .collect::<Vec<_>>()
21        .join(" ");
22
23    let gauge = gauge!("process_cli_args", "args" => args);
24    gauge.set(1);
25}
26
27#[cfg(test)]
28mod tests {
29    #[test]
30    fn test_args_stripping() {
31        // Simulates what the filter+map does
32        let raw = vec![
33            "reth",
34            "node",
35            "--http",
36            "--p2p.secret",
37            "MYSECRET",
38            "--chain=mainnet",
39            "--rpc.port",
40            "8545",
41            "-v",
42        ];
43
44        let result: Vec<_> = raw
45            .into_iter()
46            .filter(|arg| arg.starts_with('-'))
47            .map(|arg| match arg.split_once('=') {
48                Some((flag, _)) => flag.to_string(),
49                None => arg.to_string(),
50            })
51            .collect();
52
53        assert_eq!(result, vec!["--http", "--p2p.secret", "--chain", "--rpc.port", "-v"]);
54    }
55}