Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Next Next commit
refactor: extract TelemetryParams and PrometheusParams
  • Loading branch information
yjhmelody committed Mar 28, 2023
commit c2838d6c3f8fa02f323cd27cec2ddb96b57b5fbf
100 changes: 15 additions & 85 deletions client/cli/src/commands/run_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
arg_enums::RpcMethods,
error::{Error, Result},
params::{
ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, SharedParams,
TransactionPoolParams,
},
CliConfiguration,
};
use crate::{arg_enums::RpcMethods, error::{Error, Result}, params::{
ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, SharedParams,
TransactionPoolParams,
}, CliConfiguration, PrometheusParams, TelemetryParams};
use clap::Parser;
use regex::Regex;
use sc_service::{
Expand Down Expand Up @@ -116,12 +111,6 @@ pub struct RunCmd {
#[arg(long)]
pub rpc_max_subscriptions_per_connection: Option<usize>,

/// Expose Prometheus exporter on all interfaces.
///
/// Default is local.
#[arg(long)]
pub prometheus_external: bool,

/// DEPRECATED, IPC support has been removed.
#[arg(long, value_name = "PATH")]
pub ipc_path: Option<String>,
Expand Down Expand Up @@ -151,36 +140,19 @@ pub struct RunCmd {
#[arg(long, value_name = "ORIGINS", value_parser = parse_cors)]
pub rpc_cors: Option<Cors>,

/// Specify Prometheus exporter TCP Port.
#[arg(long, value_name = "PORT")]
pub prometheus_port: Option<u16>,

/// Do not expose a Prometheus exporter endpoint.
///
/// Prometheus metric endpoint is enabled by default.
#[arg(long)]
pub no_prometheus: bool,

/// The human-readable name for this node.
///
/// The node name will be reported to the telemetry server, if enabled.
#[arg(long, value_name = "NAME")]
pub name: Option<String>,

/// Disable connecting to the Substrate telemetry server.
///
/// Telemetry is on by default on global chains.
#[arg(long)]
pub no_telemetry: bool,
#[allow(missing_docs)]
#[clap(flatten)]
pub telemetry_params: TelemetryParams,

/// The URL of the telemetry server to connect to.
///
/// This flag can be passed multiple times as a means to specify multiple
/// telemetry endpoints. Verbosity levels range from 0-9, with 0 denoting
/// the least verbosity.
/// Expected format is 'URL VERBOSITY', e.g. `--telemetry-url 'wss://foo/bar 0'`.
#[arg(long = "telemetry-url", value_name = "URL VERBOSITY", value_parser = parse_telemetry_endpoints)]
pub telemetry_endpoints: Vec<(String, u8)>,
#[allow(missing_docs)]
#[clap(flatten)]
pub prometheus_params: PrometheusParams,

#[allow(missing_docs)]
#[clap(flatten)]
Expand Down Expand Up @@ -345,11 +317,12 @@ impl CliConfiguration for RunCmd {
&self,
chain_spec: &Box<dyn ChainSpec>,
) -> Result<Option<TelemetryEndpoints>> {
Ok(if self.no_telemetry {
let params = &self.telemetry_params;
Ok(if params.no_telemetry {
None
} else if !self.telemetry_endpoints.is_empty() {
} else if !params.telemetry_endpoints.is_empty() {
Some(
TelemetryEndpoints::new(self.telemetry_endpoints.clone())
TelemetryEndpoints::new(params.telemetry_endpoints.clone())
.map_err(|e| e.to_string())?,
)
} else {
Expand All @@ -374,20 +347,7 @@ impl CliConfiguration for RunCmd {
default_listen_port: u16,
chain_spec: &Box<dyn ChainSpec>,
) -> Result<Option<PrometheusConfig>> {
Ok(if self.no_prometheus {
None
} else {
let interface =
if self.prometheus_external { Ipv4Addr::UNSPECIFIED } else { Ipv4Addr::LOCALHOST };

Some(PrometheusConfig::new_with_default_registry(
SocketAddr::new(
interface.into(),
self.prometheus_port.unwrap_or(default_listen_port),
),
chain_spec.id().into(),
))
})
Ok(self.prometheus_params.prometheus_config(default_listen_port, chain_spec.id().to_string()))
}

fn disable_grandpa(&self) -> Result<bool> {
Expand Down Expand Up @@ -546,36 +506,6 @@ fn rpc_interface(
}
}

#[derive(Debug)]
enum TelemetryParsingError {
MissingVerbosity,
VerbosityParsingError(std::num::ParseIntError),
}

impl std::error::Error for TelemetryParsingError {}

impl std::fmt::Display for TelemetryParsingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TelemetryParsingError::MissingVerbosity => write!(f, "Verbosity level missing"),
TelemetryParsingError::VerbosityParsingError(e) => write!(f, "{}", e),
}
}
}

fn parse_telemetry_endpoints(s: &str) -> std::result::Result<(String, u8), TelemetryParsingError> {
let pos = s.find(' ');
match pos {
None => Err(TelemetryParsingError::MissingVerbosity),
Some(pos_) => {
let url = s[..pos_].to_string();
let verbosity =
s[pos_ + 1..].parse().map_err(TelemetryParsingError::VerbosityParsingError)?;
Ok((url, verbosity))
},
}
}

/// CORS setting
///
/// The type is introduced to overcome `Option<Option<T>>` handling of `clap`.
Expand Down
4 changes: 3 additions & 1 deletion client/cli/src/params/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ mod offchain_worker_params;
mod pruning_params;
mod shared_params;
mod transaction_pool_params;
mod prometheus_params;
mod telemetry_params;

use crate::arg_enums::{CryptoScheme, OutputType};
use clap::Args;
Expand All @@ -38,7 +40,7 @@ use std::{fmt::Debug, str::FromStr};
pub use crate::params::{
database_params::*, import_params::*, keystore_params::*, message_params::*, network_params::*,
node_key_params::*, offchain_worker_params::*, pruning_params::*, shared_params::*,
transaction_pool_params::*,
transaction_pool_params::*, prometheus_params::*, telemetry_params::*,
};

/// Parse Ss58AddressFormat
Expand Down
45 changes: 45 additions & 0 deletions client/cli/src/params/prometheus_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use clap::Args;
use sc_service::config::PrometheusConfig;
use std::net::{Ipv4Addr, SocketAddr};

/// Parameters used to config prometheus.
#[derive(Debug, Clone, Args)]
pub struct PrometheusParams {
/// Specify Prometheus exporter TCP Port.
#[arg(long, value_name = "PORT")]
pub prometheus_port: Option<u16>,
/// Expose Prometheus exporter on all interfaces.
///
/// Default is local.
#[arg(long)]
pub prometheus_external: bool,
/// Do not expose a Prometheus exporter endpoint.
///
/// Prometheus metric endpoint is enabled by default.
#[arg(long)]
pub no_prometheus: bool,
}

impl PrometheusParams {
/// Creates configuration about Prometheus.
pub fn prometheus_config(
&self,
default_listen_port: u16,
chain_id: String,
) -> Option<PrometheusConfig> {
if self.no_prometheus {
None
} else {
let interface =
if self.prometheus_external { Ipv4Addr::UNSPECIFIED } else { Ipv4Addr::LOCALHOST };

Some(PrometheusConfig::new_with_default_registry(
SocketAddr::new(
interface.into(),
self.prometheus_port.unwrap_or(default_listen_port),
),
chain_id,
))
}
}
}
51 changes: 51 additions & 0 deletions client/cli/src/params/telemetry_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use clap::Args;

/// Parameters used to config telemetry.
#[derive(Debug, Clone, Args)]
pub struct TelemetryParams {
/// Disable connecting to the Substrate telemetry server.
///
/// Telemetry is on by default on global chains.
#[arg(long)]
pub no_telemetry: bool,

/// The URL of the telemetry server to connect to.
///
/// This flag can be passed multiple times as a means to specify multiple
/// telemetry endpoints. Verbosity levels range from 0-9, with 0 denoting
/// the least verbosity.
/// Expected format is 'URL VERBOSITY', e.g. `--telemetry-url 'wss://foo/bar 0'`.
#[arg(long = "telemetry-url", value_name = "URL VERBOSITY", value_parser = parse_telemetry_endpoints)]
pub telemetry_endpoints: Vec<(String, u8)>,
}

#[derive(Debug)]
enum TelemetryParsingError {
MissingVerbosity,
VerbosityParsingError(std::num::ParseIntError),
}

impl std::error::Error for TelemetryParsingError {}

impl std::fmt::Display for TelemetryParsingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TelemetryParsingError::MissingVerbosity => write!(f, "Verbosity level missing"),
TelemetryParsingError::VerbosityParsingError(e) => write!(f, "{}", e),
}
}
}


fn parse_telemetry_endpoints(s: &str) -> Result<(String, u8), TelemetryParsingError> {
let pos = s.find(' ');
match pos {
None => Err(TelemetryParsingError::MissingVerbosity),
Some(pos_) => {
let url = s[..pos_].to_string();
let verbosity =
s[pos_ + 1..].parse().map_err(TelemetryParsingError::VerbosityParsingError)?;
Ok((url, verbosity))
},
}
}