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::{Ipv4Addr, 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 =
52 if self.prometheus_external { Ipv4Addr::UNSPECIFIED } else { Ipv4Addr::LOCALHOST };
53
54 Some(PrometheusConfig::new_with_default_registry(
55 SocketAddr::new(
56 interface.into(),
57 self.prometheus_port.unwrap_or(default_listen_port),
58 ),
59 chain_id,
60 ))
61 }
62 }
63}