mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-21 18:01:07 +00:00
Telemetry per node (#7463)
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 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 crate::logging::event_format::{CustomFmtContext, EventFormat};
|
||||
use std::fmt;
|
||||
use tracing::{Event, Level, Subscriber};
|
||||
use tracing_subscriber::{
|
||||
fmt::{
|
||||
time::{FormatTime, SystemTime},
|
||||
FormatFields,
|
||||
},
|
||||
layer::Context,
|
||||
registry::LookupSpan,
|
||||
Layer,
|
||||
};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// A `Layer` that display logs in the browser's console.
|
||||
pub struct ConsoleLogLayer<S, N = tracing_subscriber::fmt::format::DefaultFields, T = SystemTime> {
|
||||
event_format: EventFormat<T>,
|
||||
fmt_fields: N,
|
||||
_inner: std::marker::PhantomData<S>,
|
||||
}
|
||||
|
||||
impl<S, T> ConsoleLogLayer<S, tracing_subscriber::fmt::format::DefaultFields, T> {
|
||||
/// Create a new [`ConsoleLogLayer`] using the `EventFormat` provided in argument.
|
||||
pub fn new(event_format: EventFormat<T>) -> Self {
|
||||
Self {
|
||||
event_format,
|
||||
fmt_fields: Default::default(),
|
||||
_inner: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: the following code took inspiration from `EventFormat` (in this file)
|
||||
impl<S, N, T: FormatTime> ConsoleLogLayer<S, N, T>
|
||||
where
|
||||
S: Subscriber + for<'a> LookupSpan<'a>,
|
||||
N: for<'writer> FormatFields<'writer> + 'static,
|
||||
{
|
||||
fn format_event(
|
||||
&self,
|
||||
ctx: &Context<'_, S>,
|
||||
writer: &mut dyn fmt::Write,
|
||||
event: &Event,
|
||||
) -> fmt::Result {
|
||||
self.event_format.format_event_custom(
|
||||
CustomFmtContext::ContextWithFormatFields(ctx, &self.fmt_fields),
|
||||
writer,
|
||||
event,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: the following code took inspiration from tracing-subscriber
|
||||
//
|
||||
// https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/fmt_layer.rs#L717
|
||||
impl<S, N, T> Layer<S> for ConsoleLogLayer<S, N, T>
|
||||
where
|
||||
S: Subscriber + for<'a> LookupSpan<'a>,
|
||||
N: for<'writer> FormatFields<'writer> + 'static,
|
||||
T: FormatTime + 'static,
|
||||
{
|
||||
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
|
||||
thread_local! {
|
||||
static BUF: std::cell::RefCell<String> = std::cell::RefCell::new(String::new());
|
||||
}
|
||||
|
||||
BUF.with(|buf| {
|
||||
let borrow = buf.try_borrow_mut();
|
||||
let mut a;
|
||||
let mut b;
|
||||
let mut buf = match borrow {
|
||||
Ok(buf) => {
|
||||
a = buf;
|
||||
&mut *a
|
||||
}
|
||||
_ => {
|
||||
b = String::new();
|
||||
&mut b
|
||||
}
|
||||
};
|
||||
|
||||
if self.format_event(&ctx, &mut buf, event).is_ok() {
|
||||
if !buf.is_empty() {
|
||||
let meta = event.metadata();
|
||||
let level = meta.level();
|
||||
// NOTE: the following code took inspiration from tracing-subscriber
|
||||
//
|
||||
// https://github.com/iamcodemaker/console_log/blob/f13b5d6755/src/lib.rs#L149
|
||||
match *level {
|
||||
Level::ERROR => web_sys::console::error_1(&JsValue::from(buf.as_str())),
|
||||
Level::WARN => web_sys::console::warn_1(&JsValue::from(buf.as_str())),
|
||||
Level::INFO => web_sys::console::info_1(&JsValue::from(buf.as_str())),
|
||||
Level::DEBUG => web_sys::console::log_1(&JsValue::from(buf.as_str())),
|
||||
Level::TRACE => web_sys::console::debug_1(&JsValue::from(buf.as_str())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buf.clear();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 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/>.
|
||||
|
||||
#[cfg(target_os = "unknown")]
|
||||
mod console_log;
|
||||
mod prefix_layer;
|
||||
|
||||
#[cfg(target_os = "unknown")]
|
||||
pub use console_log::*;
|
||||
pub use prefix_layer::*;
|
||||
@@ -0,0 +1,86 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 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 tracing::{span::Attributes, Id, Subscriber};
|
||||
use tracing_subscriber::{layer::Context, registry::LookupSpan, Layer};
|
||||
|
||||
/// Span name used for the logging prefix. See macro `sc_tracing::logging::prefix_logs_with!`
|
||||
pub const PREFIX_LOG_SPAN: &str = "substrate-log-prefix";
|
||||
|
||||
/// A `Layer` that captures the prefix span ([`PREFIX_LOG_SPAN`]) which is then used by
|
||||
/// [`EventFormat`] to prefix the log lines by customizable string.
|
||||
///
|
||||
/// See the macro `sc_cli::prefix_logs_with!` for more details.
|
||||
pub struct PrefixLayer;
|
||||
|
||||
impl<S> Layer<S> for PrefixLayer
|
||||
where
|
||||
S: Subscriber + for<'a> LookupSpan<'a>,
|
||||
{
|
||||
fn new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
|
||||
let span = ctx
|
||||
.span(id)
|
||||
.expect("new_span has been called for this span; qed");
|
||||
|
||||
if span.name() != PREFIX_LOG_SPAN {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut extensions = span.extensions_mut();
|
||||
|
||||
if extensions.get_mut::<Prefix>().is_none() {
|
||||
let mut s = String::new();
|
||||
let mut v = PrefixVisitor(&mut s);
|
||||
attrs.record(&mut v);
|
||||
|
||||
if !s.is_empty() {
|
||||
let fmt_fields = Prefix(s);
|
||||
extensions.insert(fmt_fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct PrefixVisitor<'a, W: std::fmt::Write>(&'a mut W);
|
||||
|
||||
macro_rules! write_node_name {
|
||||
($method:ident, $type:ty, $format:expr) => {
|
||||
fn $method(&mut self, field: &tracing::field::Field, value: $type) {
|
||||
if field.name() == "name" {
|
||||
let _ = write!(self.0, $format, value);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl<'a, W: std::fmt::Write> tracing::field::Visit for PrefixVisitor<'a, W> {
|
||||
write_node_name!(record_debug, &dyn std::fmt::Debug, "[{:?}] ");
|
||||
write_node_name!(record_str, &str, "[{}] ");
|
||||
write_node_name!(record_i64, i64, "[{}] ");
|
||||
write_node_name!(record_u64, u64, "[{}] ");
|
||||
write_node_name!(record_bool, bool, "[{}] ");
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Prefix(String);
|
||||
|
||||
impl Prefix {
|
||||
pub(crate) fn as_str(&self) -> &str {
|
||||
self.0.as_str()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user