Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Allow adding a prefix to the informant #6174

Merged
merged 41 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6c3a2c0
Initial commit
cecton May 28, 2020
9c2e726
Add a Service Configuration's field + adapt informant + provide means…
cecton May 28, 2020
c966d4b
CLEANUP
cecton May 28, 2020
a3c306e
fix tests
cecton May 28, 2020
d3c1d52
fixed bad path to object
cecton May 28, 2020
cd86c58
Change OutputFormat enum to struct
cecton May 29, 2020
6c0029b
Add informant_prefix to builder and service
cecton Jun 2, 2020
ed89283
Revert "Change OutputFormat enum to struct"
cecton Jun 2, 2020
ad18c29
Revert "fix tests"
cecton Jun 2, 2020
ff258dc
Revert "Add a Service Configuration's field + adapt informant + provi…
cecton Jun 2, 2020
f5cf997
Implementation using the ServiceBuilder
cecton Jun 2, 2020
1b02761
reduce line length
cecton Jun 2, 2020
b88ed95
Merge commit f85fabd753c40041edcaac487f6f1ce259e8d11b (no conflict)
cecton Jun 2, 2020
b81f8ff
fix line width again
cecton Jun 2, 2020
a04e788
WIP
cecton Jun 2, 2020
cb8e402
WIP
cecton Jun 2, 2020
5dceb40
WIP
cecton Jun 2, 2020
bf7e144
use struct instead of enum
cecton Jun 2, 2020
d8daabb
WIP
cecton Jun 2, 2020
4133e3c
Update client/service/src/lib.rs
cecton Jun 3, 2020
23d775e
improve doc
cecton Jun 3, 2020
f006ccf
Update client/service/src/builder.rs
cecton Jun 3, 2020
b5dd8d8
Update client/service/src/builder.rs
cecton Jun 3, 2020
e32866a
change code
cecton Jun 3, 2020
1b38e33
Update client/informant/src/lib.rs
cecton Jun 3, 2020
5523518
enable_color
cecton Jun 3, 2020
36c4148
reorg log
cecton Jun 3, 2020
9728c25
remove macro
cecton Jun 3, 2020
036a1ea
Removed builder for informant prefix
cecton Jun 3, 2020
f494058
fix doc
cecton Jun 3, 2020
6ba7627
Update client/informant/src/lib.rs
cecton Jun 8, 2020
c1ec56a
Update client/informant/src/lib.rs
cecton Jun 8, 2020
d334904
Update client/informant/src/lib.rs
cecton Jun 8, 2020
f73afa4
Update client/informant/src/lib.rs
cecton Jun 8, 2020
89a9c2b
Update client/service/src/builder.rs
cecton Jun 8, 2020
a82f2cb
Update client/service/src/builder.rs
cecton Jun 8, 2020
f75b06a
Update client/service/src/builder.rs
cecton Jun 8, 2020
f1bcacb
Merge commit cc5b3a63fe5adb5100436ff43d5260a2ea18d893 (no conflict)
cecton Jun 9, 2020
38a4f27
Merge commit e287915eb37928491adbf4666e764ea0be5ac21a (conflicts)
cecton Jun 9, 2020
bb7c7c2
Merge commit feb334d87773331c23ef6560a55f43fcb5ef62a8 (no conflict)
cecton Jun 9, 2020
89b5df8
Merge branch 'cecton-informant-prefix' of github.com:paritytech/subst…
cecton Jun 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions client/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,6 @@ impl<C: SubstrateCli> Runner<C> {
{
let service = service_builder(self.config)?;

let informant_future = sc_informant::build(&service, sc_informant::OutputFormat::Coloured);
let _informant_handle = self.tokio_runtime.spawn(informant_future);

// we eagerly drop the service so that the internal exit future is fired,
// but we need to keep holding a reference to the global telemetry guard
// and drop the runtime first.
Expand Down
3 changes: 2 additions & 1 deletion client/informant/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ parity-util-mem = { version = "0.6.1", default-features = false, features = ["pr
wasm-timer = "0.2"
sc-client-api = { version = "2.0.0-rc2", path = "../api" }
sc-network = { version = "0.8.0-rc2", path = "../network" }
sc-service = { version = "0.8.0-rc2", default-features = false, path = "../service" }
sp-blockchain = { version = "2.0.0-rc2", path = "../../primitives/blockchain" }
sp-runtime = { version = "2.0.0-rc2", path = "../../primitives/runtime" }
sp-utils = { version = "2.0.0-rc2", path = "../../primitives/utils" }
sp-transaction-pool = { version = "2.0.0-rc2", path = "../../primitives/transaction-pool" }
40 changes: 25 additions & 15 deletions client/informant/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use crate::OutputFormat;
use ansi_term::Colour;
use sc_client_api::ClientInfo;
use log::info;
use sc_network::SyncState;
use sp_runtime::traits::{Block as BlockT, CheckedDiv, NumberFor, Zero, Saturating};
use sc_service::NetworkStatus;
use std::{convert::{TryFrom, TryInto}, fmt};
use sc_client_api::ClientInfo;
use sc_network::{NetworkStatus, SyncState};
use sp_runtime::traits::{Block as BlockT, CheckedDiv, NumberFor, Saturating, Zero};
use std::{
convert::{TryFrom, TryInto},
fmt,
};
use wasm_timer::Instant;
use crate::OutputFormat;

/// State of the informant display system.
///
Expand Down Expand Up @@ -67,16 +69,22 @@ impl<B: BlockT> InformantDisplay<B> {
self.last_update = Instant::now();
self.last_number = Some(best_number);

let (status, target) = match (net_status.sync_state, net_status.best_seen_block) {
(SyncState::Idle, _) => ("💤 Idle".into(), "".into()),
(SyncState::Downloading, None) => (format!("⚙️ Preparing{}", speed), "".into()),
(SyncState::Downloading, Some(n)) => (format!("⚙️ Syncing{}", speed), format!(", target=#{}", n)),
let (level, status, target) = match (net_status.sync_state, net_status.best_seen_block) {
(SyncState::Idle, _) => ("💤", "Idle".into(), "".into()),
(SyncState::Downloading, None) => ("⚙️ ", format!("Preparing{}", speed), "".into()),
(SyncState::Downloading, Some(n)) => (
"⚙️ ",
format!("Syncing{}", speed),
format!(", target=#{}", n),
),
};

if self.format == OutputFormat::Coloured {
if self.format.enable_color {
info!(
target: "substrate",
"{}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}",
"{} {}{}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}",
level,
self.format.prefix,
Colour::White.bold().paint(&status),
target,
Colour::White.bold().paint(format!("{}", num_connected_peers)),
Expand All @@ -86,11 +94,13 @@ impl<B: BlockT> InformantDisplay<B> {
info.chain.finalized_hash,
Colour::Green.paint(format!("⬇ {}", TransferRateFormat(net_status.average_download_per_sec))),
Colour::Red.paint(format!("⬆ {}", TransferRateFormat(net_status.average_upload_per_sec))),
);
)
} else {
info!(
target: "substrate",
"{}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}",
"{} {}{}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}",
level,
self.format.prefix,
status,
target,
num_connected_peers,
Expand All @@ -100,7 +110,7 @@ impl<B: BlockT> InformantDisplay<B> {
info.chain.finalized_hash,
TransferRateFormat(net_status.average_download_per_sec),
TransferRateFormat(net_status.average_upload_per_sec),
);
)
}
}
}
Expand Down
74 changes: 55 additions & 19 deletions client/informant/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,66 @@
//! Console informant. Prints sync progress and block events. Runs on the calling thread.

use ansi_term::Colour;
use sc_client_api::{BlockchainEvents, UsageProvider};
use futures::prelude::*;
use log::{info, warn, trace};
use sp_runtime::traits::Header;
use sc_service::AbstractService;
use log::{info, trace, warn};
use parity_util_mem::MallocSizeOf;
use sc_client_api::{BlockchainEvents, UsageProvider};
use sc_network::{network_state::NetworkState, NetworkStatus};
use sp_blockchain::HeaderMetadata;
use sp_runtime::traits::{Block as BlockT, Header};
use sp_transaction_pool::TransactionPool;
use sp_utils::mpsc::TracingUnboundedReceiver;
use std::fmt::Display;
use std::sync::Arc;
use std::time::Duration;

mod display;

/// The format to print telemetry output in.
#[derive(PartialEq)]
pub enum OutputFormat {
Coloured,
Plain,
#[derive(Clone)]
pub struct OutputFormat {
/// Enable color output in logs.
pub enable_color: bool,
/// Add a prefix before every log line
pub prefix: String,
Comment on lines +42 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should here also again should be made aware that this does not uses a whitespace in front of. While I'm still not sure if we should not add the whitespace by default for a prefix.

Another point, please create the following:

struct Prefix {
    prefix: String,
    style: Option<ansi_term::Style>,
}

This will make it possible for the user to supply a style as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not add the whitespace imo. People might want to use something like [mynode] without whitespace in front of the log. But as a counter argument I would also add that the emoji uses a whitespace already so it would be inconsistent. (But I personally find the emojis annoying in logs.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the style is not really useful as people can put it in the String already. Do you really want it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point

}

/// Creates an informant in the form of a `Future` that must be polled regularly.
pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futures::Future<Output = ()> {
let client = service.client();
let pool = service.transaction_pool();
/// An empty trait that implements `TransactionPool`
cecton marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(target_os = "unknown")]
pub trait TransactionPoolAndMaybeMallogSizeOf: TransactionPool {}

let mut display = display::InformantDisplay::new(format);
/// An empty trait that implements `TransactionPool` and `MallocSizeOf`
cecton marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(target_os = "unknown"))]
pub trait TransactionPoolAndMaybeMallogSizeOf: TransactionPool + MallocSizeOf {}

let display_notifications = service
.network_status(Duration::from_millis(5000))
#[cfg(target_os = "unknown")]
impl<T: TransactionPool> TransactionPoolAndMaybeMallogSizeOf for T {}

#[cfg(not(target_os = "unknown"))]
impl<T: TransactionPool + MallocSizeOf> TransactionPoolAndMaybeMallogSizeOf for T {}

/// Creates an informant in the form of a `Future` that must be polled regularly.
cecton marked this conversation as resolved.
Show resolved Hide resolved
pub fn build<B: BlockT, C>(
client: Arc<C>,
network_status_stream_builder: impl FnOnce(
Duration,
) -> TracingUnboundedReceiver<(
NetworkStatus<B>,
NetworkState,
)>,
pool: Arc<impl TransactionPoolAndMaybeMallogSizeOf>,
format: OutputFormat,
) -> impl futures::Future<Output = ()>
where
C: UsageProvider<B> + HeaderMetadata<B> + BlockchainEvents<B>,
<C as HeaderMetadata<B>>::Error: Display,
{
let mut display = display::InformantDisplay::new(format.clone());

let client_1 = client.clone();
let display_notifications = network_status_stream_builder(Duration::from_millis(5000))
.for_each(move |(net_status, _)| {
let info = client.usage_info();
let info = client_1.usage_info();
if let Some(ref usage) = info.usage {
trace!(target: "usage", "Usage statistics: {}", usage);
} else {
Expand All @@ -64,7 +97,6 @@ pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futur
future::ready(())
});

let client = service.client();
let mut last_best = {
let info = client.usage_info();
Some((info.chain.best_number, info.chain.best_hash))
Expand All @@ -82,7 +114,8 @@ pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futur

match maybe_ancestor {
Ok(ref ancestor) if ancestor.hash != *last_hash => info!(
"♻️ Reorg on #{},{} to #{},{}, common ancestor #{},{}",
"♻️ {}Reorg on #{},{} to #{},{}, common ancestor #{},{}",
format.prefix,
Colour::Red.bold().paint(format!("{}", last_num)), last_hash,
Colour::Green.bold().paint(format!("{}", n.header.number())), n.hash,
Colour::White.bold().paint(format!("{}", ancestor.number)), ancestor.hash,
Expand All @@ -97,7 +130,10 @@ pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futur
last_best = Some((n.header.number().clone(), n.hash.clone()));
}

info!(target: "substrate", "✨ Imported #{} ({})", Colour::White.bold().paint(format!("{}", n.header.number())), n.hash);
info!(
target: "substrate", "✨ {}Imported #{} ({})",
format.prefix, Colour::White.bold().paint(format!("{}", n.header.number())), n.hash,
cecton marked this conversation as resolved.
Show resolved Hide resolved
);
future::ready(())
});

Expand Down
20 changes: 20 additions & 0 deletions client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ pub use libp2p::{Multiaddr, PeerId};
pub use libp2p::multiaddr;

pub use sc_peerset::ReputationChange;
use sp_runtime::traits::{Block as BlockT, NumberFor};

/// The maximum allowed number of established connections per peer.
///
Expand Down Expand Up @@ -313,3 +314,22 @@ pub trait NetworkStateInfo {
/// Returns the local Peer ID.
fn local_peer_id(&self) -> PeerId;
}

/// Overview status of the network.
#[derive(Clone)]
pub struct NetworkStatus<B: BlockT> {
/// Current global sync state.
pub sync_state: SyncState,
/// Target sync block number.
pub best_seen_block: Option<NumberFor<B>>,
/// Number of peers participating in syncing.
pub num_sync_peers: u32,
/// Total number of connected peers
pub num_connected_peers: usize,
/// Total number of active peers.
pub num_active_peers: usize,
/// Downloaded bytes per second averaged over the past few seconds.
pub average_download_per_sec: u64,
/// Uploaded bytes per second averaged over the past few seconds.
pub average_upload_per_sec: u64,
}
2 changes: 1 addition & 1 deletion client/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ sc-rpc-server = { version = "2.0.0-rc2", path = "../rpc-servers" }
sc-rpc = { version = "2.0.0-rc2", path = "../rpc" }
sc-block-builder = { version = "0.8.0-rc2", path = "../block-builder" }
sp-block-builder = { version = "2.0.0-rc2", path = "../../primitives/block-builder" }

sc-informant = { version = "0.8.0-rc2", path = "../informant" }
sc-telemetry = { version = "2.0.0-rc2", path = "../telemetry" }
sc-offchain = { version = "2.0.0-rc2", path = "../offchain" }
parity-multiaddr = { package = "parity-multiaddr", version = "0.7.3" }
Expand Down
Loading