diff --git a/Cargo.lock b/Cargo.lock index 79872ffc6e764..4ce423c53d1d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5752,6 +5752,7 @@ dependencies = [ "sp-std", "sp-trie", "sp-version", + "substrate-prometheus-endpoint", "substrate-test-runtime-client", "tempfile", "tracing", @@ -5815,6 +5816,7 @@ dependencies = [ "sp-runtime", "sp-state-machine", "sp-trie", + "substrate-prometheus-endpoint", "substrate-test-runtime-client", "tempfile", ] diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index 58a7ab933eeb9..19906dd6a1bf5 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -155,6 +155,7 @@ impl BenchDb { None, None, ExecutionExtensions::new(profile.into_execution_strategies(), None), + None, ).expect("Should not fail"); (client, backend) diff --git a/client/Cargo.toml b/client/Cargo.toml index 710cdbd128f94..61199f04da970 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -34,6 +34,7 @@ sp-blockchain = { version = "2.0.0-alpha.2", path = "../primitives/blockchain" } sp-state-machine = { version = "0.8.0-alpha.2", path = "../primitives/state-machine" } sc-telemetry = { version = "2.0.0-alpha.2", path = "telemetry" } sp-trie = { version = "2.0.0-alpha.2", path = "../primitives/trie" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0-alpha.2", path = "../utils/prometheus" } tracing = "0.1.10" [dev-dependencies] diff --git a/client/cli/src/commands/runcmd.rs b/client/cli/src/commands/runcmd.rs index f29bc3c743b64..0ad6fefcce8cb 100644 --- a/client/cli/src/commands/runcmd.rs +++ b/client/cli/src/commands/runcmd.rs @@ -24,7 +24,7 @@ use regex::Regex; use chrono::prelude::*; use sc_service::{ AbstractService, Configuration, ChainSpecExtension, RuntimeGenesis, ChainSpec, Roles, - config::KeystoreConfig, + config::{KeystoreConfig, PrometheusConfig}, }; use sc_telemetry::TelemetryEndpoints; @@ -423,11 +423,12 @@ impl RunCmd { // Override prometheus if self.no_prometheus { - config.prometheus_port = None; - } else if config.prometheus_port.is_none() { + config.prometheus_config = None; + } else if config.prometheus_config.is_none() { let prometheus_interface: &str = if self.prometheus_external { "0.0.0.0" } else { "127.0.0.1" }; - config.prometheus_port = Some( - parse_address(&format!("{}:{}", prometheus_interface, 9615), self.prometheus_port)?); + config.prometheus_config = Some(PrometheusConfig::new_with_default_registry( + parse_address(&format!("{}:{}", prometheus_interface, 9615), self.prometheus_port)?, + )); } config.tracing_targets = self.import_params.tracing_targets.clone().into(); diff --git a/client/db/Cargo.toml b/client/db/Cargo.toml index f92a5c48e28ef..8eaae24e5207c 100644 --- a/client/db/Cargo.toml +++ b/client/db/Cargo.toml @@ -30,6 +30,7 @@ sc-state-db = { version = "0.8.0-alpha.2", path = "../state-db" } sp-trie = { version = "2.0.0-alpha.2", path = "../../primitives/trie" } sp-consensus = { version = "0.8.0-alpha.2", path = "../../primitives/consensus/common" } sp-blockchain = { version = "2.0.0-alpha.2", path = "../../primitives/blockchain" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0-alpha.2", path = "../../utils/prometheus" } [dev-dependencies] sp-keyring = { version = "2.0.0-alpha.2", path = "../../primitives/keyring" } diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index d4c157986f589..eed1fc0e1d99c 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -84,6 +84,7 @@ use crate::storage_cache::{CachingState, SharedCache, new_shared_cache}; use crate::stats::StateUsageStats; use log::{trace, debug, warn}; pub use sc_state_db::PruningMode; +use prometheus_endpoint::Registry; #[cfg(any(feature = "kvdb-rocksdb", test))] pub use bench::BenchmarkingState; @@ -291,6 +292,7 @@ pub fn new_client( fork_blocks: ForkBlocks, bad_blocks: BadBlocks, execution_extensions: ExecutionExtensions, + prometheus_registry: Option, ) -> Result<( sc_client::Client< Backend, @@ -317,6 +319,7 @@ pub fn new_client( fork_blocks, bad_blocks, execution_extensions, + prometheus_registry, )?, backend, )) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index db60c94997f38..bfb979c19e07c 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -17,7 +17,7 @@ use crate::{Service, NetworkStatus, NetworkState, error::Error, DEFAULT_PROTOCOL_ID, MallocSizeOfWasm}; use crate::{TaskManagerBuilder, start_rpc_servers, build_network_future, TransactionPoolAdapter}; use crate::status_sinks; -use crate::config::{Configuration, DatabaseConfig, KeystoreConfig}; +use crate::config::{Configuration, DatabaseConfig, KeystoreConfig, PrometheusConfig}; use sc_client_api::{ self, BlockchainEvents, @@ -128,7 +128,6 @@ pub struct ServiceBuilder>>, marker: PhantomData<(TBl, TRtApi)>, background_tasks: Vec<(&'static str, BackgroundTask)>, - prometheus_registry: Option } /// Full client type. @@ -262,6 +261,7 @@ fn new_full_parts( fork_blocks, bad_blocks, extensions, + config.prometheus_config.as_ref().map(|config| config.registry.clone()), )? }; @@ -308,7 +308,6 @@ where TGen: RuntimeGenesis, TCSExt: Extension { remote_backend: None, background_tasks: Default::default(), marker: PhantomData, - prometheus_registry: None, }) } @@ -378,6 +377,7 @@ where TGen: RuntimeGenesis, TCSExt: Extension { backend.clone(), config.expect_chain_spec(), executor, + config.prometheus_config.as_ref().map(|config| config.registry.clone()), )?); Ok(ServiceBuilder { @@ -396,7 +396,6 @@ where TGen: RuntimeGenesis, TCSExt: Extension { remote_backend: Some(remote_blockchain), background_tasks: Default::default(), marker: PhantomData, - prometheus_registry: None, }) } } @@ -470,7 +469,6 @@ impl Self { - Self { - config: self.config, - client: self.client, - backend: self.backend, - tasks_builder: self.tasks_builder, - keystore: self.keystore, - fetcher: self.fetcher, - select_chain: self.select_chain, - import_queue: self.import_queue, - finality_proof_request_builder: self.finality_proof_request_builder, - finality_proof_provider: self.finality_proof_provider, - transaction_pool: self.transaction_pool, - rpc_extensions: self.rpc_extensions, - remote_backend: self.remote_backend, - background_tasks: self.background_tasks, - marker: self.marker, - prometheus_registry: Some(registry), - } - } } /// Implemented on `ServiceBuilder`. Allows running block commands, such as import/export/validate @@ -845,7 +816,6 @@ ServiceBuilder< rpc_extensions, remote_backend, background_tasks, - prometheus_registry, } = self; sp_session::generate_initial_session_keys( @@ -897,14 +867,6 @@ ServiceBuilder< let block_announce_validator = Box::new(sp_consensus::block_validation::DefaultBlockAnnounceValidator::new(client.clone())); - let prometheus_registry_and_port = match config.prometheus_port { - Some(port) => match prometheus_registry { - Some(registry) => Some((registry, port)), - None => Some((Registry::new_custom(Some("substrate".into()), None)?, port)) - }, - None => None - }; - let network_params = sc_network::config::Params { roles: config.roles, executor: { @@ -922,7 +884,7 @@ ServiceBuilder< import_queue, protocol_id, block_announce_validator, - metrics_registry: prometheus_registry_and_port.as_ref().map(|(r, _)| r.clone()) + metrics_registry: config.prometheus_config.as_ref().map(|config| config.registry.clone()) }; let has_bootnodes = !network_params.network_config.boot_nodes.is_empty(); @@ -1035,7 +997,7 @@ ServiceBuilder< } // Prometheus metrics - let metrics = if let Some((registry, port)) = prometheus_registry_and_port.clone() { + let metrics = if let Some(PrometheusConfig { port, registry }) = config.prometheus_config.clone() { let metrics = ServiceMetrics::register(®istry)?; metrics.node_roles.set(u64::from(config.roles.bits())); spawn_handle.spawn( @@ -1297,7 +1259,7 @@ ServiceBuilder< _telemetry_on_connect_sinks: telemetry_connection_sinks.clone(), keystore, marker: PhantomData::, - prometheus_registry: prometheus_registry_and_port.map(|(r, _)| r) + prometheus_registry: config.prometheus_config.map(|config| config.registry) }) } } diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 974dac588def9..6400712cad3b9 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -27,6 +27,7 @@ use sc_chain_spec::{ChainSpec, NoExtension}; use sp_core::crypto::Protected; use target_info::Target; use sc_telemetry::TelemetryEndpoints; +use prometheus_endpoint::Registry; /// Executable version. Used to pass version information from the root crate. #[derive(Clone)] @@ -93,8 +94,8 @@ pub struct Configuration { pub rpc_ws_max_connections: Option, /// CORS settings for HTTP & WS servers. `None` if all origins are allowed. pub rpc_cors: Option>, - /// Prometheus endpoint Port. `None` if disabled. - pub prometheus_port: Option, + /// Prometheus endpoint configuration. `None` if disabled. + pub prometheus_config: Option, /// Telemetry service URL. `None` if disabled. pub telemetry_endpoints: Option, /// External WASM transport for the telemetry. If `Some`, when connection to a telemetry @@ -165,6 +166,28 @@ pub enum DatabaseConfig { Custom(Arc), } +/// Configuration of the Prometheus endpoint. +#[derive(Clone)] +pub struct PrometheusConfig { + /// Port to use. + pub port: SocketAddr, + /// A metrics registry to use. Useful for setting the metric prefix. + pub registry: Registry, +} + +impl PrometheusConfig { + /// Create a new config using the default registry. + /// + /// The default registry prefixes metrics with `substrate`. + pub fn new_with_default_registry(port: SocketAddr) -> Self { + Self { + port, + registry: Registry::new_custom(Some("substrate".into()), None) + .expect("this can only fail if the prefix is empty") + } + } +} + impl Default for Configuration { /// Create a default config fn default() -> Self { @@ -190,7 +213,7 @@ impl Default for Configuration { rpc_ws: None, rpc_ws_max_connections: None, rpc_cors: Some(vec![]), - prometheus_port: None, + prometheus_config: None, telemetry_endpoints: None, telemetry_external_transport: None, default_heap_pages: None, diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index 5f679b82b39a4..03cccbe4a053a 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -199,7 +199,7 @@ fn node_config ( rpc_ws: None, rpc_ws_max_connections: None, rpc_cors: None, - prometheus_port: None, + prometheus_config: None, telemetry_endpoints: None, telemetry_external_transport: None, default_heap_pages: None, diff --git a/client/src/client.rs b/client/src/client.rs index 5a0093308e628..a763866d319e4 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -78,6 +78,7 @@ pub use sc_client_api::{ CallExecutor, }; use sp_blockchain::Error; +use prometheus_endpoint::Registry; use crate::{ call_executor::LocalCallExecutor, @@ -171,6 +172,7 @@ pub fn new_in_mem( executor: E, genesis_storage: &S, keystore: Option, + prometheus_registry: Option, ) -> sp_blockchain::Result, LocalCallExecutor, E>, @@ -181,7 +183,7 @@ pub fn new_in_mem( S: BuildStorage, Block: BlockT, { - new_with_backend(Arc::new(in_mem::Backend::new()), executor, genesis_storage, keystore) + new_with_backend(Arc::new(in_mem::Backend::new()), executor, genesis_storage, keystore, prometheus_registry) } /// Create a client with the explicitly provided backend. @@ -191,6 +193,7 @@ pub fn new_with_backend( executor: E, build_genesis_storage: &S, keystore: Option, + prometheus_registry: Option, ) -> sp_blockchain::Result, Block, RA>> where E: CodeExecutor + RuntimeInfo, @@ -207,6 +210,7 @@ pub fn new_with_backend( Default::default(), Default::default(), extensions, + prometheus_registry, ) } @@ -286,6 +290,7 @@ impl Client where fork_blocks: ForkBlocks, bad_blocks: BadBlocks, execution_extensions: ExecutionExtensions, + _prometheus_registry: Option, ) -> sp_blockchain::Result { if backend.blockchain().header(BlockId::Number(Zero::zero()))?.is_none() { let genesis_storage = build_genesis_storage.build_storage()?; diff --git a/client/src/lib.rs b/client/src/lib.rs index f282449c276e4..9c6393314f06a 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -68,6 +68,7 @@ //! Default::default(), //! Default::default(), //! Default::default(), +//! None, //! ); //! ``` //! diff --git a/client/src/light/mod.rs b/client/src/light/mod.rs index ca1a3a50c6b60..07816b3b35692 100644 --- a/client/src/light/mod.rs +++ b/client/src/light/mod.rs @@ -28,6 +28,7 @@ use sp_core::traits::CodeExecutor; use sp_runtime::BuildStorage; use sp_runtime::traits::{Block as BlockT, HashFor}; use sp_blockchain::Result as ClientResult; +use prometheus_endpoint::Registry; use crate::call_executor::LocalCallExecutor; use crate::client::Client; @@ -58,6 +59,7 @@ pub fn new_light( backend: Arc>>, genesis_storage: &GS, code_executor: E, + prometheus_registry: Option, ) -> ClientResult< Client< Backend>, @@ -84,6 +86,7 @@ pub fn new_light( Default::default(), Default::default(), Default::default(), + prometheus_registry, ) } diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index 7d22abf3d8787..37fa1e2b34e52 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -210,7 +210,8 @@ impl TestClientBuilder