Skip to main content

soil_cli/params/
prometheus_params.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use clap::Args;
8use soil_service::config::PrometheusConfig;
9use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
10
11/// Parameters used to config prometheus.
12#[derive(Debug, Clone, Args)]
13pub struct PrometheusParams {
14	/// Specify Prometheus exporter TCP Port.
15	#[arg(long, value_name = "PORT")]
16	pub prometheus_port: Option<u16>,
17	/// Expose Prometheus exporter on all interfaces.
18	///
19	/// Default is local.
20	#[arg(long)]
21	pub prometheus_external: bool,
22	/// Do not expose a Prometheus exporter endpoint.
23	///
24	/// Prometheus metric endpoint is enabled by default.
25	#[arg(long)]
26	pub no_prometheus: bool,
27}
28
29impl PrometheusParams {
30	/// Creates [`PrometheusConfig`].
31	pub fn prometheus_config(
32		&self,
33		default_listen_port: u16,
34		chain_id: String,
35	) -> Option<PrometheusConfig> {
36		if self.no_prometheus {
37			None
38		} else {
39			let interface: IpAddr = if self.prometheus_external {
40				Ipv6Addr::UNSPECIFIED.into()
41			} else {
42				Ipv4Addr::LOCALHOST.into()
43			};
44
45			Some(PrometheusConfig::new_with_default_registry(
46				SocketAddr::new(
47					interface.into(),
48					self.prometheus_port.unwrap_or(default_listen_port),
49				),
50				chain_id,
51			))
52		}
53	}
54}