sc_cli/params/prometheus_params.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use clap::Args;
20use sc_service::config::PrometheusConfig;
21use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
22
23/// Parameters used to config prometheus.
24#[derive(Debug, Clone, Args)]
25pub struct PrometheusParams {
26 /// Specify Prometheus exporter TCP Port.
27 #[arg(long, value_name = "PORT")]
28 pub prometheus_port: Option<u16>,
29 /// Expose Prometheus exporter on all interfaces.
30 ///
31 /// Default is local.
32 #[arg(long)]
33 pub prometheus_external: bool,
34 /// Do not expose a Prometheus exporter endpoint.
35 ///
36 /// Prometheus metric endpoint is enabled by default.
37 #[arg(long)]
38 pub no_prometheus: bool,
39}
40
41impl PrometheusParams {
42 /// Creates [`PrometheusConfig`].
43 pub fn prometheus_config(
44 &self,
45 default_listen_port: u16,
46 chain_id: String,
47 ) -> Option<PrometheusConfig> {
48 if self.no_prometheus {
49 None
50 } else {
51 let interface: IpAddr = if self.prometheus_external {
52 Ipv6Addr::UNSPECIFIED.into()
53 } else {
54 Ipv4Addr::LOCALHOST.into()
55 };
56
57 Some(PrometheusConfig::new_with_default_registry(
58 SocketAddr::new(
59 interface.into(),
60 self.prometheus_port.unwrap_or(default_listen_port),
61 ),
62 chain_id,
63 ))
64 }
65 }
66}