Telemetry per node (#7463)

This commit is contained in:
Cecile Tonglet
2021-01-20 12:28:56 +01:00
committed by GitHub
parent 71ef82afbc
commit 970cc25cef
49 changed files with 2578 additions and 2009 deletions
-6
View File
@@ -14,7 +14,6 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
log = "0.4.11"
atty = "0.2.13"
regex = "1.4.2"
tokio = { version = "0.2.21", features = [ "signal", "rt-core", "rt-threaded", "blocking" ] }
futures = "0.3.9"
@@ -43,10 +42,6 @@ structopt = "0.3.8"
sc-tracing = { version = "2.0.0", path = "../tracing" }
chrono = "0.4.10"
serde = "1.0.111"
tracing = "0.1.22"
tracing-log = "0.1.1"
tracing-subscriber = "0.2.15"
sc-cli-proc-macro = { version = "2.0.0", path = "./proc-macro" }
thiserror = "1.0.21"
[target.'cfg(not(target_os = "unknown"))'.dependencies]
@@ -54,7 +49,6 @@ rpassword = "5.0.0"
[dev-dependencies]
tempfile = "3.1.0"
ansi_term = "0.12.1"
[features]
wasmtime = [
@@ -1,21 +0,0 @@
[package]
name = "sc-cli-proc-macro"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "Helper macros for Substrate's client CLI"
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
[lib]
proc-macro = true
[dependencies]
proc-macro-crate = "0.1.4"
proc-macro2 = "1.0.6"
quote = { version = "1.0.3", features = ["proc-macro"] }
syn = { version = "1.0.58", features = ["proc-macro", "full", "extra-traits", "parsing"] }
-155
View File
@@ -1,155 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 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 proc_macro::TokenStream;
use proc_macro2::Span;
use proc_macro_crate::crate_name;
use quote::quote;
use syn::{Error, Expr, Ident, ItemFn};
/// Add a log prefix to the function.
///
/// This prefixes all the log lines with `[<name>]` (after the timestamp). It works by making a
/// tracing's span that is propagated to all the child calls and child tasks (futures) if they are
/// spawned properly with the `SpawnHandle` (see `TaskManager` in sc-cli) or if the futures use
/// `.in_current_span()` (see tracing-futures).
///
/// See Tokio's [tracing documentation](https://docs.rs/tracing-core/) and
/// [tracing-futures documentation](https://docs.rs/tracing-futures/) for more details.
///
/// # Implementation notes
///
/// If there are multiple spans with a log prefix, only the latest will be shown.
///
/// # Example with a literal
///
/// ```ignore
/// Builds a new service for a light client.
/// #[sc_cli::prefix_logs_with("light")]
/// pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
/// let (client, backend, keystore, mut task_manager, on_demand) =
/// sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;
///
/// ...
/// }
/// ```
///
/// Will produce logs that look like this:
///
/// ```text
/// 2020-10-16 08:03:14 Substrate Node
/// 2020-10-16 08:03:14 ✌️ version 2.0.0-47f7d3f2e-x86_64-linux-gnu
/// 2020-10-16 08:03:14 ❤️ by Anonymous, 2017-2020
/// 2020-10-16 08:03:14 📋 Chain specification: Local Testnet
/// 2020-10-16 08:03:14 🏷 Node name: nice-glove-1401
/// 2020-10-16 08:03:14 👤 Role: LIGHT
/// 2020-10-16 08:03:14 💾 Database: RocksDb at /tmp/substrate95w2Dk/chains/local_testnet/db
/// 2020-10-16 08:03:14 ⛓ Native runtime: node-template-1 (node-template-1.tx1.au1)
/// 2020-10-16 08:03:14 [light] 🔨 Initializing Genesis block/state (state: 0x121d…8e36, header-hash: 0x24ef…8ff6)
/// 2020-10-16 08:03:14 [light] Loading GRANDPA authorities from genesis on what appears to be first startup.
/// 2020-10-16 08:03:15 [light] ⏱ Loaded block-time = 6000 milliseconds from genesis on first-launch
/// 2020-10-16 08:03:15 [light] Using default protocol ID "sup" because none is configured in the chain specs
/// 2020-10-16 08:03:15 [light] 🏷 Local node identity is: 12D3KooWHX4rkWT6a6N55Km7ZnvenGdShSKPkzJ3yj9DU5nqDtWR
/// 2020-10-16 08:03:15 [light] 📦 Highest known block at #0
/// 2020-10-16 08:03:15 [light] 〽️ Prometheus server started at 127.0.0.1:9615
/// 2020-10-16 08:03:15 [light] Listening for new connections on 127.0.0.1:9944.
/// ```
///
/// # Example using the actual node name
///
/// ```ignore
/// Builds a new service for a light client.
/// #[sc_cli::prefix_logs_with(config.network.node_name.as_str())]
/// pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
/// let (client, backend, keystore, mut task_manager, on_demand) =
/// sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;
///
/// ...
/// }
/// ```
///
/// Will produce logs that look like this:
///
/// ```text
/// 2020-10-16 08:12:57 Substrate Node
/// 2020-10-16 08:12:57 ✌️ version 2.0.0-efb9b822a-x86_64-linux-gnu
/// 2020-10-16 08:12:57 ❤️ by Anonymous, 2017-2020
/// 2020-10-16 08:12:57 📋 Chain specification: Local Testnet
/// 2020-10-16 08:12:57 🏷 Node name: open-harbor-1619
/// 2020-10-16 08:12:57 👤 Role: LIGHT
/// 2020-10-16 08:12:57 💾 Database: RocksDb at /tmp/substrate9T9Mtb/chains/local_testnet/db
/// 2020-10-16 08:12:57 ⛓ Native runtime: node-template-1 (node-template-1.tx1.au1)
/// 2020-10-16 08:12:58 [open-harbor-1619] 🔨 Initializing Genesis block/state (state: 0x121d…8e36, header-hash: 0x24ef…8ff6)
/// 2020-10-16 08:12:58 [open-harbor-1619] Loading GRANDPA authorities from genesis on what appears to be first startup.
/// 2020-10-16 08:12:58 [open-harbor-1619] ⏱ Loaded block-time = 6000 milliseconds from genesis on first-launch
/// 2020-10-16 08:12:58 [open-harbor-1619] Using default protocol ID "sup" because none is configured in the chain specs
/// 2020-10-16 08:12:58 [open-harbor-1619] 🏷 Local node identity is: 12D3KooWRzmYC8QTK1Pm8Cfvid3skTS4Hn54jc4AUtje8Rqbfgtp
/// 2020-10-16 08:12:58 [open-harbor-1619] 📦 Highest known block at #0
/// 2020-10-16 08:12:58 [open-harbor-1619] 〽️ Prometheus server started at 127.0.0.1:9615
/// 2020-10-16 08:12:58 [open-harbor-1619] Listening for new connections on 127.0.0.1:9944.
/// ```
#[proc_macro_attribute]
pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
let item_fn = syn::parse_macro_input!(item as ItemFn);
if arg.is_empty() {
return Error::new(
Span::call_site(),
"missing argument: name of the node. Example: sc_cli::prefix_logs_with(<expr>)",
)
.to_compile_error()
.into();
}
let name = syn::parse_macro_input!(arg as Expr);
let crate_name = if std::env::var("CARGO_PKG_NAME")
.expect("cargo env var always there when compiling; qed")
== "sc-cli"
{
Ident::new("sc_cli", Span::call_site().into())
} else {
let crate_name = match crate_name("sc-cli") {
Ok(x) => x,
Err(err) => return Error::new(Span::call_site(), err).to_compile_error().into(),
};
Ident::new(&crate_name, Span::call_site().into())
};
let ItemFn {
attrs,
vis,
sig,
block,
} = item_fn;
(quote! {
#(#attrs)*
#vis #sig {
let span = #crate_name::tracing::info_span!(
#crate_name::PREFIX_LOG_SPAN,
name = #name,
);
let _enter = span.enter();
#block
}
})
.into()
}
@@ -123,7 +123,7 @@ mod tests {
}
fn copyright_start_year() -> i32 {
2020
2021
}
fn author() -> String {
+27 -19
View File
@@ -21,8 +21,8 @@
use crate::arg_enums::Database;
use crate::error::Result;
use crate::{
init_logger, DatabaseParams, ImportParams, KeystoreParams, NetworkParams, NodeKeyParams,
OffchainWorkerParams, PruningParams, SharedParams, SubstrateCli, InitLoggerParams,
DatabaseParams, ImportParams, KeystoreParams, NetworkParams, NodeKeyParams,
OffchainWorkerParams, PruningParams, SharedParams, SubstrateCli,
};
use log::warn;
use names::{Generator, Name};
@@ -32,7 +32,9 @@ use sc_service::config::{
NodeKeyConfig, OffchainWorkerConfig, PrometheusConfig, PruningMode, Role, RpcMethods,
TaskExecutor, TelemetryEndpoints, TransactionPoolOptions, WasmExecutionMethod,
};
use sc_service::{ChainSpec, TracingReceiver, KeepBlocks, TransactionStorageMode };
use sc_service::{ChainSpec, TracingReceiver, KeepBlocks, TransactionStorageMode};
use sc_telemetry::TelemetryHandle;
use sc_tracing::logging::GlobalLoggerBuilder;
use std::net::SocketAddr;
use std::path::PathBuf;
@@ -468,6 +470,7 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
&self,
cli: &C,
task_executor: TaskExecutor,
telemetry_handle: Option<TelemetryHandle>,
) -> Result<Configuration> {
let is_dev = self.is_dev()?;
let chain_id = self.chain_id(is_dev)?;
@@ -539,6 +542,7 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
role,
base_path: Some(base_path),
informant_output_format: Default::default(),
telemetry_handle,
})
}
@@ -569,34 +573,38 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
/// 1. Sets the panic handler
/// 2. Initializes the logger
/// 3. Raises the FD limit
fn init<C: SubstrateCli>(&self) -> Result<()> {
let logger_pattern = self.log_filters()?;
let tracing_receiver = self.tracing_receiver()?;
let tracing_targets = self.tracing_targets()?;
let disable_log_reloading = self.is_log_filter_reloading_disabled()?;
let disable_log_color = self.disable_log_color()?;
fn init<C: SubstrateCli>(&self) -> Result<sc_telemetry::TelemetryWorker> {
sp_panic_handler::set(&C::support_url(), &C::impl_version());
init_logger(InitLoggerParams {
pattern: logger_pattern,
tracing_receiver,
tracing_targets,
disable_log_reloading,
disable_log_color,
})?;
let mut logger = GlobalLoggerBuilder::new(self.log_filters()?);
logger.with_log_reloading(!self.is_log_filter_reloading_disabled()?);
if let Some(transport) = self.telemetry_external_transport()? {
logger.with_transport(transport);
}
if let Some(tracing_targets) = self.tracing_targets()? {
let tracing_receiver = self.tracing_receiver()?;
logger.with_profiling(tracing_receiver, tracing_targets);
}
if self.disable_log_color()? {
logger.with_colors(false);
}
let telemetry_worker = logger.init()?;
if let Some(new_limit) = fdlimit::raise_fd_limit() {
if new_limit < RECOMMENDED_OPEN_FILE_DESCRIPTOR_LIMIT {
warn!(
"Low open file descriptor limit configured for the process. \
Current value: {:?}, recommended value: {:?}.",
Current value: {:?}, recommended value: {:?}.",
new_limit, RECOMMENDED_OPEN_FILE_DESCRIPTOR_LIMIT,
);
}
}
Ok(())
Ok(telemetry_worker)
}
}
+3
View File
@@ -77,6 +77,9 @@ pub enum Error {
/// Application specific error chain sequence forwarder.
#[error(transparent)]
Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
#[error(transparent)]
GlobalLoggerError(#[from] sc_tracing::logging::Error),
}
impl std::convert::From<&str> for Error {
+13 -380
View File
@@ -21,7 +21,6 @@
#![warn(missing_docs)]
#![warn(unused_extern_crates)]
#![warn(unused_imports)]
#![warn(unused_crate_dependencies)]
pub mod arg_enums;
mod commands;
@@ -36,9 +35,10 @@ pub use config::*;
pub use error::*;
pub use params::*;
pub use runner::*;
pub use sc_cli_proc_macro::*;
pub use sc_service::{ChainSpec, Role};
use sc_service::{Configuration, TaskExecutor};
use sc_telemetry::TelemetryHandle;
pub use sc_tracing::logging::GlobalLoggerBuilder;
pub use sp_version::RuntimeVersion;
use std::io::Write;
pub use structopt;
@@ -46,18 +46,6 @@ use structopt::{
clap::{self, AppSettings},
StructOpt,
};
use tracing_subscriber::{
fmt::time::ChronoLocal,
EnvFilter,
FmtSubscriber,
Layer,
layer::SubscriberExt,
};
pub use sc_tracing::logging;
pub use logging::PREFIX_LOG_SPAN;
#[doc(hidden)]
pub use tracing;
/// Substrate client CLI
///
@@ -82,7 +70,8 @@ pub trait SubstrateCli: Sized {
/// Extracts the file name from `std::env::current_exe()`.
/// Resorts to the env var `CARGO_PKG_NAME` in case of Error.
fn executable_name() -> String {
std::env::current_exe().ok()
std::env::current_exe()
.ok()
.and_then(|e| e.file_name().map(|s| s.to_os_string()))
.and_then(|w| w.into_string().ok())
.unwrap_or_else(|| env!("CARGO_PKG_NAME").into())
@@ -112,7 +101,10 @@ pub trait SubstrateCli: Sized {
///
/// Gets the struct from the command line arguments. Print the
/// error message and quit the program in case of failure.
fn from_args() -> Self where Self: StructOpt + Sized {
fn from_args() -> Self
where
Self: StructOpt + Sized,
{
<Self as SubstrateCli>::from_iter(&mut std::env::args_os())
}
@@ -167,7 +159,7 @@ pub trait SubstrateCli: Sized {
let _ = std::io::stdout().write_all(e.message.as_bytes());
std::process::exit(0);
}
},
}
};
<Self as StructOpt>::from_clap(&matches)
@@ -222,377 +214,18 @@ pub trait SubstrateCli: Sized {
&self,
command: &T,
task_executor: TaskExecutor,
telemetry_handle: Option<TelemetryHandle>,
) -> error::Result<Configuration> {
command.create_configuration(self, task_executor)
command.create_configuration(self, task_executor, telemetry_handle)
}
/// Create a runner for the command provided in argument. This will create a Configuration and
/// a tokio runtime
fn create_runner<T: CliConfiguration>(&self, command: &T) -> error::Result<Runner<Self>> {
command.init::<Self>()?;
Runner::new(self, command)
let telemetry_worker = command.init::<Self>()?;
Runner::new(self, command, telemetry_worker)
}
/// Native runtime version.
fn native_runtime_version(chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion;
}
/// The parameters for [`init_logger`].
#[derive(Default)]
pub struct InitLoggerParams {
/// A comma seperated list of logging patterns.
///
/// E.g.: `test-crate=debug`
pub pattern: String,
/// The tracing receiver.
pub tracing_receiver: sc_tracing::TracingReceiver,
/// Optional comma seperated list of tracing targets.
pub tracing_targets: Option<String>,
/// Should log reloading be disabled?
pub disable_log_reloading: bool,
/// Should the log color output be disabled?
pub disable_log_color: bool,
}
/// Initialize the global logger
///
/// This sets various global logging and tracing instances and thus may only be called once.
pub fn init_logger(
InitLoggerParams {
pattern,
tracing_receiver,
tracing_targets,
disable_log_reloading,
disable_log_color,
}: InitLoggerParams,
) -> std::result::Result<(), String> {
use sc_tracing::parse_default_directive;
// Accept all valid directives and print invalid ones
fn parse_user_directives(
mut env_filter: EnvFilter,
dirs: &str,
) -> std::result::Result<EnvFilter, String> {
for dir in dirs.split(',') {
env_filter = env_filter.add_directive(parse_default_directive(&dir)?);
}
Ok(env_filter)
}
// Initialize filter - ensure to use `parse_default_directive` for any defaults to persist
// after log filter reloading by RPC
let mut env_filter = EnvFilter::default()
// Enable info
.add_directive(parse_default_directive("info")
.expect("provided directive is valid"))
// Disable info logging by default for some modules.
.add_directive(parse_default_directive("ws=off")
.expect("provided directive is valid"))
.add_directive(parse_default_directive("yamux=off")
.expect("provided directive is valid"))
.add_directive(parse_default_directive("cranelift_codegen=off")
.expect("provided directive is valid"))
// Set warn logging by default for some modules.
.add_directive(parse_default_directive("cranelift_wasm=warn")
.expect("provided directive is valid"))
.add_directive(parse_default_directive("hyper=warn")
.expect("provided directive is valid"));
if let Ok(lvl) = std::env::var("RUST_LOG") {
if lvl != "" {
env_filter = parse_user_directives(env_filter, &lvl)?;
}
}
if pattern != "" {
// We're not sure if log or tracing is available at this moment, so silently ignore the
// parse error.
env_filter = parse_user_directives(env_filter, &pattern)?;
}
let max_level_hint = Layer::<FmtSubscriber>::max_level_hint(&env_filter);
let max_level = if tracing_targets.is_some() {
// If profiling is activated, we require `trace` logging.
log::LevelFilter::Trace
} else {
match max_level_hint {
Some(tracing_subscriber::filter::LevelFilter::INFO) | None => log::LevelFilter::Info,
Some(tracing_subscriber::filter::LevelFilter::TRACE) => log::LevelFilter::Trace,
Some(tracing_subscriber::filter::LevelFilter::WARN) => log::LevelFilter::Warn,
Some(tracing_subscriber::filter::LevelFilter::ERROR) => log::LevelFilter::Error,
Some(tracing_subscriber::filter::LevelFilter::DEBUG) => log::LevelFilter::Debug,
Some(tracing_subscriber::filter::LevelFilter::OFF) => log::LevelFilter::Off,
}
};
tracing_log::LogTracer::builder()
.with_max_level(max_level)
.init()
.map_err(|e| format!("Registering Substrate logger failed: {:}!", e))?;
// If we're only logging `INFO` entries then we'll use a simplified logging format.
let simple = match max_level_hint {
Some(level) if level <= tracing_subscriber::filter::LevelFilter::INFO => true,
_ => false,
};
// Make sure to include profiling targets in the filter
if let Some(tracing_targets) = tracing_targets.clone() {
// Always log the special target `sc_tracing`, overrides global level.
// Required because profiling traces are emitted via `sc_tracing`
// NOTE: this must be done after we check the `max_level_hint` otherwise
// it is always raised to `TRACE`.
env_filter = env_filter.add_directive(
parse_default_directive("sc_tracing=trace").expect("provided directive is valid")
);
env_filter = parse_user_directives(env_filter, &tracing_targets)?;
}
let enable_color = atty::is(atty::Stream::Stderr) && !disable_log_color;
let timer = ChronoLocal::with_format(if simple {
"%Y-%m-%d %H:%M:%S".to_string()
} else {
"%Y-%m-%d %H:%M:%S%.3f".to_string()
});
let subscriber_builder = FmtSubscriber::builder()
.with_env_filter(env_filter)
.with_writer(std::io::stderr as _)
.event_format(logging::EventFormat {
timer,
enable_color,
display_target: !simple,
display_level: !simple,
display_thread_name: !simple,
});
if disable_log_reloading {
let subscriber = subscriber_builder
.finish()
.with(logging::NodeNameLayer);
initialize_tracing(subscriber, tracing_receiver, tracing_targets)
} else {
let subscriber_builder = subscriber_builder.with_filter_reloading();
let handle = subscriber_builder.reload_handle();
sc_tracing::set_reload_handle(handle);
let subscriber = subscriber_builder
.finish()
.with(logging::NodeNameLayer);
initialize_tracing(subscriber, tracing_receiver, tracing_targets)
}
}
fn initialize_tracing<S>(
subscriber: S,
tracing_receiver: sc_tracing::TracingReceiver,
profiling_targets: Option<String>,
) -> std::result::Result<(), String>
where
S: tracing::Subscriber + Send + Sync + 'static,
{
if let Some(profiling_targets) = profiling_targets {
let profiling = sc_tracing::ProfilingLayer::new(tracing_receiver, &profiling_targets);
if let Err(e) = tracing::subscriber::set_global_default(subscriber.with(profiling)) {
return Err(format!(
"Registering Substrate tracing subscriber failed: {:}!", e
))
}
} else {
if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
return Err(format!(
"Registering Substrate tracing subscriber failed: {:}!", e
))
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate as sc_cli;
use std::{env, process::Command};
use tracing::{metadata::Kind, subscriber::Interest, Callsite, Level, Metadata};
#[test]
fn test_logger_filters() {
let test_pattern = "afg=debug,sync=trace,client=warn,telemetry,something-with-dash=error";
init_logger(
InitLoggerParams { pattern: test_pattern.into(), ..Default::default() },
).unwrap();
tracing::dispatcher::get_default(|dispatcher| {
let test_filter = |target, level| {
struct DummyCallSite;
impl Callsite for DummyCallSite {
fn set_interest(&self, _: Interest) {}
fn metadata(&self) -> &Metadata<'_> {
unreachable!();
}
}
let metadata = tracing::metadata!(
name: "",
target: target,
level: level,
fields: &[],
callsite: &DummyCallSite,
kind: Kind::SPAN,
);
dispatcher.enabled(&metadata)
};
assert!(test_filter("afg", Level::INFO));
assert!(test_filter("afg", Level::DEBUG));
assert!(!test_filter("afg", Level::TRACE));
assert!(test_filter("sync", Level::TRACE));
assert!(test_filter("client", Level::WARN));
assert!(test_filter("telemetry", Level::TRACE));
assert!(test_filter("something-with-dash", Level::ERROR));
});
}
const EXPECTED_LOG_MESSAGE: &'static str = "yeah logging works as expected";
#[test]
fn dash_in_target_name_works() {
let executable = env::current_exe().unwrap();
let output = Command::new(executable)
.env("ENABLE_LOGGING", "1")
.args(&["--nocapture", "log_something_with_dash_target_name"])
.output()
.unwrap();
let output = String::from_utf8(output.stderr).unwrap();
assert!(output.contains(EXPECTED_LOG_MESSAGE));
}
/// This is no actual test, it will be used by the `dash_in_target_name_works` test.
/// The given test will call the test executable to only execute this test that
/// will only print `EXPECTED_LOG_MESSAGE` through logging while using a target
/// name that contains a dash. This ensures that targets names with dashes work.
#[test]
fn log_something_with_dash_target_name() {
if env::var("ENABLE_LOGGING").is_ok() {
let test_pattern = "test-target=info";
init_logger(
InitLoggerParams { pattern: test_pattern.into(), ..Default::default() },
).unwrap();
log::info!(target: "test-target", "{}", EXPECTED_LOG_MESSAGE);
}
}
const EXPECTED_NODE_NAME: &'static str = "THE_NODE";
#[test]
fn prefix_in_log_lines() {
let re = regex::Regex::new(&format!(
r"^\d{{4}}-\d{{2}}-\d{{2}} \d{{2}}:\d{{2}}:\d{{2}} \[{}\] {}$",
EXPECTED_NODE_NAME,
EXPECTED_LOG_MESSAGE,
)).unwrap();
let executable = env::current_exe().unwrap();
let output = Command::new(executable)
.env("ENABLE_LOGGING", "1")
.args(&["--nocapture", "prefix_in_log_lines_entrypoint"])
.output()
.unwrap();
let output = String::from_utf8(output.stderr).unwrap();
assert!(
re.is_match(output.trim()),
format!("Expected:\n{}\nGot:\n{}", re, output),
);
}
/// This is no actual test, it will be used by the `prefix_in_log_lines` test.
/// The given test will call the test executable to only execute this test that
/// will only print a log line prefixed by the node name `EXPECTED_NODE_NAME`.
#[test]
fn prefix_in_log_lines_entrypoint() {
if env::var("ENABLE_LOGGING").is_ok() {
let test_pattern = "test-target=info";
init_logger(
InitLoggerParams { pattern: test_pattern.into(), ..Default::default() },
).unwrap();
prefix_in_log_lines_process();
}
}
#[crate::prefix_logs_with(EXPECTED_NODE_NAME)]
fn prefix_in_log_lines_process() {
log::info!("{}", EXPECTED_LOG_MESSAGE);
}
/// This is no actual test, it will be used by the `do_not_write_with_colors_on_tty` test.
/// The given test will call the test executable to only execute this test that
/// will only print a log line with some colors in it.
#[test]
fn do_not_write_with_colors_on_tty_entrypoint() {
if env::var("ENABLE_LOGGING").is_ok() {
init_logger(InitLoggerParams::default()).unwrap();
log::info!("{}", ansi_term::Colour::Yellow.paint(EXPECTED_LOG_MESSAGE));
}
}
#[test]
fn do_not_write_with_colors_on_tty() {
let re = regex::Regex::new(&format!(
r"^\d{{4}}-\d{{2}}-\d{{2}} \d{{2}}:\d{{2}}:\d{{2}} {}$",
EXPECTED_LOG_MESSAGE,
)).unwrap();
let executable = env::current_exe().unwrap();
let output = Command::new(executable)
.env("ENABLE_LOGGING", "1")
.args(&["--nocapture", "do_not_write_with_colors_on_tty_entrypoint"])
.output()
.unwrap();
let output = String::from_utf8(output.stderr).unwrap();
assert!(
re.is_match(output.trim()),
format!("Expected:\n{}\nGot:\n{}", re, output),
);
}
#[test]
fn log_max_level_is_set_properly() {
fn run_test(rust_log: Option<String>, tracing_targets: Option<String>) -> String {
let executable = env::current_exe().unwrap();
let mut command = Command::new(executable);
command.env("PRINT_MAX_LOG_LEVEL", "1")
.args(&["--nocapture", "log_max_level_is_set_properly"]);
if let Some(rust_log) = rust_log {
command.env("RUST_LOG", rust_log);
}
if let Some(tracing_targets) = tracing_targets {
command.env("TRACING_TARGETS", tracing_targets);
}
let output = command.output().unwrap();
String::from_utf8(output.stderr).unwrap()
}
if env::var("PRINT_MAX_LOG_LEVEL").is_ok() {
init_logger(InitLoggerParams {
tracing_targets: env::var("TRACING_TARGETS").ok(),
..Default::default()
}).unwrap();
eprint!("MAX_LOG_LEVEL={:?}", log::max_level());
} else {
assert_eq!("MAX_LOG_LEVEL=Info", run_test(None, None));
assert_eq!("MAX_LOG_LEVEL=Trace", run_test(Some("test=trace".into()), None));
assert_eq!("MAX_LOG_LEVEL=Debug", run_test(Some("test=debug".into()), None));
assert_eq!("MAX_LOG_LEVEL=Trace", run_test(None, Some("test=info".into())));
}
}
}
+23 -2
View File
@@ -25,6 +25,7 @@ use futures::select;
use futures::{future, future::FutureExt, Future};
use log::info;
use sc_service::{Configuration, TaskType, TaskManager};
use sc_telemetry::{TelemetryHandle, TelemetryWorker};
use sp_utils::metrics::{TOKIO_THREADS_ALIVE, TOKIO_THREADS_TOTAL};
use std::marker::PhantomData;
use sc_service::Error as ServiceError;
@@ -114,12 +115,17 @@ where
pub struct Runner<C: SubstrateCli> {
config: Configuration,
tokio_runtime: tokio::runtime::Runtime,
telemetry_worker: TelemetryWorker,
phantom: PhantomData<C>,
}
impl<C: SubstrateCli> Runner<C> {
/// Create a new runtime with the command provided in argument
pub fn new<T: CliConfiguration>(cli: &C, command: &T) -> Result<Runner<C>> {
pub fn new<T: CliConfiguration>(
cli: &C,
command: &T,
telemetry_worker: TelemetryWorker,
) -> Result<Runner<C>> {
let tokio_runtime = build_runtime()?;
let runtime_handle = tokio_runtime.handle().clone();
@@ -132,9 +138,16 @@ impl<C: SubstrateCli> Runner<C> {
}
};
let telemetry_handle = telemetry_worker.handle();
Ok(Runner {
config: command.create_configuration(cli, task_executor.into())?,
config: command.create_configuration(
cli,
task_executor.into(),
Some(telemetry_handle),
)?,
tokio_runtime,
telemetry_worker,
phantom: PhantomData,
})
}
@@ -184,6 +197,7 @@ impl<C: SubstrateCli> Runner<C> {
{
self.print_node_infos();
let mut task_manager = self.tokio_runtime.block_on(initialize(self.config))?;
task_manager.spawn_handle().spawn("telemetry_worker", self.telemetry_worker.run());
let res = self.tokio_runtime.block_on(main(task_manager.future().fuse()));
self.tokio_runtime.block_on(task_manager.clean_shutdown());
Ok(res?)
@@ -222,4 +236,11 @@ impl<C: SubstrateCli> Runner<C> {
pub fn config_mut(&mut self) -> &mut Configuration {
&mut self.config
}
/// Get a new [`TelemetryHandle`].
///
/// This is used when you want to register a new telemetry for a Substrate node.
pub fn telemetry_handle(&self) -> TelemetryHandle {
self.telemetry_worker.handle()
}
}