sc_cli/params/telemetry_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;
20
21/// Parameters used to config telemetry.
22#[derive(Debug, Clone, Args)]
23pub struct TelemetryParams {
24 /// Disable connecting to the Substrate telemetry server.
25 ///
26 /// Telemetry is on by default on global chains.
27 #[arg(long)]
28 pub no_telemetry: bool,
29
30 /// The URL of the telemetry server to connect to.
31 ///
32 /// This flag can be passed multiple times as a means to specify multiple
33 /// telemetry endpoints. Verbosity levels range from 0-9, with 0 denoting
34 /// the least verbosity.
35 ///
36 /// Expected format is 'URL VERBOSITY', e.g. `--telemetry-url 'wss://foo/bar 0'`.
37 #[arg(long = "telemetry-url", value_name = "URL VERBOSITY", value_parser = parse_telemetry_endpoints)]
38 pub telemetry_endpoints: Vec<(String, u8)>,
39}
40
41#[derive(Debug)]
42enum TelemetryParsingError {
43 MissingVerbosity,
44 VerbosityParsingError(std::num::ParseIntError),
45}
46
47impl std::error::Error for TelemetryParsingError {}
48
49impl std::fmt::Display for TelemetryParsingError {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 match self {
52 TelemetryParsingError::MissingVerbosity => write!(f, "Verbosity level missing"),
53 TelemetryParsingError::VerbosityParsingError(e) => write!(f, "{}", e),
54 }
55 }
56}
57
58fn parse_telemetry_endpoints(s: &str) -> Result<(String, u8), TelemetryParsingError> {
59 let pos = s.find(' ');
60 match pos {
61 None => Err(TelemetryParsingError::MissingVerbosity),
62 Some(pos_) => {
63 let url = s[..pos_].to_string();
64 let verbosity =
65 s[pos_ + 1..].parse().map_err(TelemetryParsingError::VerbosityParsingError)?;
66 Ok((url, verbosity))
67 },
68 }
69}