diff --git a/bin/node-template/node/src/service.rs b/bin/node-template/node/src/service.rs index b9e5705333e70..a3aca89ef7468 100644 --- a/bin/node-template/node/src/service.rs +++ b/bin/node-template/node/src/service.rs @@ -11,7 +11,6 @@ pub use sc_executor::NativeExecutor; use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair}; use sc_finality_grandpa::SharedVoterState; use sc_keystore::LocalKeystore; -use sc_telemetry::TelemetrySpan; // Our native executor instance. native_executor_instance!( @@ -37,7 +36,6 @@ pub fn new_partial(config: &Configuration) -> Result, sc_finality_grandpa::LinkHalf, - Option, ) >, ServiceError> { if config.keystore_remote.is_some() { @@ -46,7 +44,7 @@ pub fn new_partial(config: &Configuration) -> Result(&config)?; let client = Arc::new(client); @@ -87,7 +85,7 @@ pub fn new_partial(config: &Configuration) -> Result Result select_chain, transaction_pool, inherent_data_providers, - other: (block_import, grandpa_link, telemetry_span), + other: (block_import, grandpa_link), } = new_partial(&config)?; if let Some(url) = &config.keystore_remote { @@ -177,7 +175,6 @@ pub fn new_full(mut config: Configuration) -> Result network_status_sinks, system_rpc_tx, config, - telemetry_span, }, )?; @@ -260,7 +257,7 @@ pub fn new_full(mut config: Configuration) -> Result /// Builds a new service for a light client. pub fn new_light(mut config: Configuration) -> Result { - let (client, backend, keystore_container, mut task_manager, on_demand, telemetry_span) = + let (client, backend, keystore_container, mut task_manager, on_demand) = sc_service::new_light_parts::(&config)?; config.network.extra_sets.push(sc_finality_grandpa::grandpa_peers_set_config()); @@ -327,7 +324,6 @@ pub fn new_light(mut config: Configuration) -> Result network, network_status_sinks, system_rpc_tx, - telemetry_span, })?; network_starter.start_network(); diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index aae16ebf0313e..55c046a9a6364 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -34,7 +34,7 @@ use sp_runtime::traits::Block as BlockT; use futures::prelude::*; use sc_client_api::{ExecutorProvider, RemoteBackend}; use node_executor::Executor; -use sc_telemetry::{TelemetryConnectionNotifier, TelemetrySpan}; +use sc_telemetry::TelemetryConnectionNotifier; type FullClient = sc_service::TFullClient; type FullBackend = sc_service::TFullBackend; @@ -58,10 +58,9 @@ pub fn new_partial(config: &Configuration) -> Result, ), grandpa::SharedVoterState, - Option, ) >, ServiceError> { - let (client, backend, keystore_container, task_manager, telemetry_span) = + let (client, backend, keystore_container, task_manager) = sc_service::new_full_parts::(&config)?; let client = Arc::new(client); @@ -159,7 +158,7 @@ pub fn new_partial(config: &Configuration) -> Result Result<( Arc::Hash>>, Arc>> ), ServiceError> { - let (client, backend, keystore_container, mut task_manager, on_demand, telemetry_span) = + let (client, backend, keystore_container, mut task_manager, on_demand) = sc_service::new_light_parts::(&config)?; config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); @@ -445,7 +443,6 @@ pub fn new_light_base(mut config: Configuration) -> Result<( config, backend, network_status_sinks, system_rpc_tx, network: network.clone(), task_manager: &mut task_manager, - telemetry_span, })?; Ok(( diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 5bc14d52a5f30..247f6d2fddb33 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -33,7 +33,7 @@ use sc_service::config::{ TaskExecutor, TelemetryEndpoints, TransactionPoolOptions, WasmExecutionMethod, }; use sc_service::{ChainSpec, TracingReceiver, KeepBlocks, TransactionStorageMode}; -use sc_telemetry::TelemetryHandle; +use sc_telemetry::{TelemetryHandle, TelemetrySpan}; use sc_tracing::logging::LoggerBuilder; use std::net::SocketAddr; use std::path::PathBuf; @@ -488,6 +488,13 @@ pub trait CliConfiguration: Sized { let max_runtime_instances = self.max_runtime_instances()?.unwrap_or(8); let is_validator = role.is_network_authority(); let (keystore_remote, keystore) = self.keystore_config(&config_dir)?; + let telemetry_endpoints = telemetry_handle + .as_ref() + .and_then(|_| self.telemetry_endpoints(&chain_spec).transpose()) + .transpose()? + // Don't initialise telemetry if `telemetry_endpoints` == Some([]) + .filter(|x| !x.is_empty()); + let telemetry_span = telemetry_endpoints.as_ref().map(|_| TelemetrySpan::new()); let unsafe_pruning = self .import_params() @@ -526,7 +533,8 @@ pub trait CliConfiguration: Sized { rpc_ws_max_connections: self.rpc_ws_max_connections()?, rpc_cors: self.rpc_cors(is_dev)?, prometheus_config: self.prometheus_config(DCV::prometheus_listen_port())?, - telemetry_endpoints: self.telemetry_endpoints(&chain_spec)?, + telemetry_endpoints, + telemetry_span, telemetry_external_transport: self.telemetry_external_transport()?, default_heap_pages: self.default_heap_pages()?, offchain_worker: self.offchain_worker(&role)?, diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 63abf8ca9576d..2ee95bd24d324 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -55,7 +55,6 @@ use sc_telemetry::{ telemetry, ConnectionMessage, TelemetryConnectionNotifier, - TelemetrySpan, SUBSTRATE_INFO, }; use sp_transaction_pool::MaintainedTransactionPool; @@ -184,7 +183,6 @@ type TFullParts = ( Arc>, KeystoreContainer, TaskManager, - Option, ); type TLightParts = ( @@ -193,7 +191,6 @@ type TLightParts = ( KeystoreContainer, TaskManager, Arc>, - Option, ); /// Light client backend type with a specific hash type. @@ -308,10 +305,9 @@ pub fn new_full_parts( { let keystore_container = KeystoreContainer::new(&config.keystore)?; - let telemetry_span = config.telemetry_endpoints.as_ref().map(|_| TelemetrySpan::new()); let task_manager = { let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry); - TaskManager::new(config.task_executor.clone(), registry, telemetry_span.clone())? + TaskManager::new(config.task_executor.clone(), registry, config.telemetry_span.clone())? }; let executor = NativeExecutor::::new( @@ -367,7 +363,6 @@ pub fn new_full_parts( backend, keystore_container, task_manager, - telemetry_span, )) } @@ -379,10 +374,9 @@ pub fn new_light_parts( TExecDisp: NativeExecutionDispatch + 'static, { let keystore_container = KeystoreContainer::new(&config.keystore)?; - let telemetry_span = config.telemetry_endpoints.as_ref().map(|_| TelemetrySpan::new()); let task_manager = { let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry); - TaskManager::new(config.task_executor.clone(), registry, telemetry_span.clone())? + TaskManager::new(config.task_executor.clone(), registry, config.telemetry_span.clone())? }; let executor = NativeExecutor::::new( @@ -421,7 +415,7 @@ pub fn new_light_parts( config.prometheus_config.as_ref().map(|config| config.registry.clone()), )?); - Ok((client, backend, keystore_container, task_manager, on_demand, telemetry_span)) + Ok((client, backend, keystore_container, task_manager, on_demand)) } /// Create an instance of db-backed client. @@ -473,8 +467,6 @@ pub fn new_client( pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> { /// The service configuration. pub config: Configuration, - /// Telemetry span, if any. - pub telemetry_span: Option, /// A shared client returned by `new_full_parts`/`new_light_parts`. pub client: Arc, /// A shared backend returned by `new_full_parts`/`new_light_parts`. @@ -567,7 +559,6 @@ pub fn spawn_tasks( let SpawnTasksParams { mut config, task_manager, - telemetry_span, client, on_demand, backend, @@ -588,13 +579,11 @@ pub fn spawn_tasks( config.dev_key_seed.clone().map(|s| vec![s]).unwrap_or_default(), )?; - let telemetry_connection_notifier = telemetry_span - .and_then(|span| init_telemetry( - &mut config, - span, - network.clone(), - client.clone(), - )); + let telemetry_connection_notifier = init_telemetry( + &mut config, + network.clone(), + client.clone(), + ); info!("📦 Highest known block at #{}", chain_info.best_number); @@ -692,11 +681,11 @@ async fn transaction_notifications( fn init_telemetry>( config: &mut Configuration, - telemetry_span: TelemetrySpan, network: Arc::Hash>>, client: Arc, ) -> Option { - let endpoints = config.telemetry_endpoints()?.clone(); + let telemetry_span = config.telemetry_span.clone()?; + let endpoints = config.telemetry_endpoints.clone()?; let genesis_hash = client.block_hash(Zero::zero()).ok().flatten().unwrap_or_default(); let connection_message = ConnectionMessage { name: config.network.node_name.to_owned(), diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 71fbb3a2f2e4e..1e316c37dc9a4 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -101,6 +101,10 @@ pub struct Configuration { /// This is a handle to a `TelemetryWorker` instance. It is used to initialize the telemetry for /// a substrate node. pub telemetry_handle: Option, + /// Telemetry span. + /// + /// This span is entered for every background task spawned using the TaskManager. + pub telemetry_span: Option, /// The default number of 64KB pages to allocate for Wasm execution pub default_heap_pages: Option, /// Should offchain workers be executed. @@ -207,16 +211,6 @@ impl Configuration { self.prometheus_config.as_ref().map(|config| &config.registry) } - /// Returns the telemetry endpoints if any and if the telemetry handle exists. - pub(crate) fn telemetry_endpoints(&self) -> Option<&TelemetryEndpoints> { - if self.telemetry_handle.is_none() { - return None; - } - - // Don't initialise telemetry if `telemetry_endpoints` == Some([]) - self.telemetry_endpoints.as_ref().filter(|x| !x.is_empty()) - } - /// Returns the network protocol id from the chain spec, or the default. pub fn protocol_id(&self) -> sc_network::config::ProtocolId { let protocol_id_full = match self.chain_spec.protocol_id() { diff --git a/client/service/src/task_manager/mod.rs b/client/service/src/task_manager/mod.rs index e910e2f3a32e2..6b14fbeec2c6f 100644 --- a/client/service/src/task_manager/mod.rs +++ b/client/service/src/task_manager/mod.rs @@ -91,10 +91,7 @@ impl SpawnTaskHandle { metrics.tasks_ended.with_label_values(&[name, "finished"]).inc_by(0); } - let telemetry_span = self.telemetry_span.clone(); let future = async move { - let _telemetry_entered = telemetry_span.as_ref().map(|x| x.enter()); - if let Some(metrics) = metrics { // Add some wrappers around `task`. let task = { @@ -127,7 +124,11 @@ impl SpawnTaskHandle { } }; - let join_handle = self.executor.spawn(Box::pin(future.in_current_span()), task_type); + let join_handle = { + let _span = self.telemetry_span.as_ref().map(|s| s.enter()); + self.executor.spawn(Box::pin(future.in_current_span()), task_type) + }; + let mut task_notifier = self.task_notifier.clone(); self.executor.spawn( Box::pin(async move { diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index f1d5c6a86b06e..a42dba84dfeae 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -268,6 +268,7 @@ fn node_config Self { Self(tracing::info_span!(TELEMETRY_LOG_SPAN)) } + + /// Return a clone of the underlying `tracing::Span` instance. + pub fn span(&self) -> tracing::Span { + self.0.clone() + } } /// Message sent when the connection (re-)establishes. diff --git a/utils/browser/src/lib.rs b/utils/browser/src/lib.rs index b72f2e973b682..ea9dfc9674f7f 100644 --- a/utils/browser/src/lib.rs +++ b/utils/browser/src/lib.rs @@ -24,7 +24,7 @@ use sc_service::{ GenericChainSpec, RuntimeGenesis, KeepBlocks, TransactionStorageMode, }; -use sc_telemetry::TelemetryHandle; +use sc_telemetry::{TelemetryHandle, TelemetrySpan}; use sc_tracing::logging::LoggerBuilder; use wasm_bindgen::prelude::*; use futures::{ @@ -72,6 +72,7 @@ where allow_private_ipv4: true, enable_mdns: false, }; + let telemetry_span = telemetry_handle.as_ref().map(|_| TelemetrySpan::new()); let config = Configuration { network, @@ -83,6 +84,7 @@ where }).into(), telemetry_external_transport: Some(transport), telemetry_handle, + telemetry_span, role: Role::Light, database: { info!("Opening Indexed DB database '{}'...", name);