Tracing for wasm with bridging to native (#6916)

* implement events handling, implement parent_id for spans & events

* add events to sp_io::storage

* update test

* add tests

* adjust limit

* let tracing crate handle parent_ids

* re-enable current-id tracking

* add test for threads with CurrentSpan

* fix log level

* remove redundant check for non wasm traces

* remove duplicate definition in test

* Adding conditional events API

* prefer explicit parent_id over current,

enhance test

* limit changes to client::tracing event implementation

* remove From impl due to fallback required on parent_id

* make tracing codecable

* replace with global tracing

* new tracing interface

* impl TracingSubscriber in client

* implement access to global TracingSubscriber from primitives

* span for wasm

* increment towards Wasm Tracing Subscriber implementation

* increment, remove sp-tracing from runtime-interface

* increment, it compiles

* attained original functionality with new mechanism

* implement remaining TracingSubscriber functions

* remove spans from decl_module

* add handling for encoded values

* Revert "replace with global tracing"

This reverts commit 8824a60deea54d9b437407a21c8ceaf6a1902ee5.

* Wasm Side Tracing

* tracing on wasm

* enable tracing wasm on node-runtime

* export all the macros in std

* tracing subscriber on wasm-side only

* pass spans and events over and record them

* reactivate previous code and  cleanup

* further cleaning up

* extend the span macros, activate through executive

* tracking the actual extrinsic, too

* style

* fixing tests

* spaces -> tabs

* attempting to reactivate params

* activate our tests in CI

* some passing

* tests passing

* with core lazy

* global tracer for wasm side with pass over

* fixing metadata referencing

* remove const_fn feature requirement

* reenable dispatch traces

* reset client tracing

* further cleaning up

* fixing runtime-test

* move tracing-build setup into runtime-test

* Merge DebugWriter from tracing and frame-support, move to sp-std

* remove dangling fixme

* Docs for tracing primitives

* cleaning up a bit more

* Wasm interface docs

* optimise docs.rs setup

* adding tracing flags to uncomment

* remove brace

* fixing imports

* fixing broken syntax

* add required modules

* nicer formatting

* better target management

* adding low level storage tracing events into frame

* add custom Debug impl for WasmMetadata

* cloning profiler

* adding info about cloning profiler

* using in-scope for within calls

* proper time tracing, cleaning up println

* allow to disable tracing on runtime_interface-macro

* disable tracing for wasm-tracing-interface

* simplify wasm-tracing-api

* update client to new interface

* fixing docs and tests for sp-tracing

* update integration tests

* re-activating enter_span

* dropping FIXME, it's documented

* fix formatting

* fix formatting

* fix imports

* more debug info

* inform wasm about it being disabled by returning 1

* only one tracer, but enabled multi-all support

* make trait pub again for tests

* Apply suggestions from code review

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* fixing wasm doc tests for proper usage

* remove unnecessary import

* fixing formatting

* minor style fixes

* downgrading wabt

* update error message for UI

* Fix interface test

* next attempt to fix macros

* geee

* revert tracing on hashed for future PR

* remove local macros, use originals

* we are able to convert to static items

* implement more WasmValue types

* adding support to convert str, debug and encoded values

* more minor fixes

* revert unsafe 'static making

* fix indentation

* remove commented lines

* bump all them tracing versions

* cleaning up docs and info

* document new flag

* the new layered system handles span cloning better

* Apply suggestions from code review

Co-authored-by: David <dvdplm@gmail.com>

Co-authored-by: Matt Rutherford <mattrutherford@users.noreply.github.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: David <dvdplm@gmail.com>
This commit is contained in:
Benjamin Kampmann
2020-09-18 08:10:59 +02:00
committed by GitHub
parent 649bee1a1e
commit a9c73113a8
34 changed files with 1207 additions and 436 deletions
+161 -49
View File
@@ -17,7 +17,7 @@
//! Substrate tracing primitives and macros.
//!
//! To trace functions or invidual code in Substrate, this crate provides [`tracing_span`]
//! To trace functions or invidual code in Substrate, this crate provides [`within_span`]
//! and [`enter_span`]. See the individual docs for how to use these macros.
//!
//! Note that to allow traces from wasm execution environment there are
@@ -28,21 +28,80 @@
//! Additionally, we have a const: `WASM_TRACE_IDENTIFIER`, which holds a span name used
//! to signal that the 'actual' span name and target should be retrieved instead from
//! the associated Fields mentioned above.
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
#[macro_use]
extern crate rental;
/// Tracing facilities and helpers.
///
/// This is modeled after the `tracing`/`tracing-core` interface and uses that more or
/// less directly for the native side. Because of certain optimisations the these crates
/// have done, the wasm implementation diverges slightly and is optimised for thtat use
/// case (like being able to cross the wasm/native boundary via scale codecs).
///
/// One of said optimisations is that all macros will yield to a `noop` in non-std unless
/// the `with-tracing` feature is explicitly activated. This allows you to just use the
/// tracing wherever you deem fit and without any performance impact by default. Only if
/// the specific `with-tracing`-feature is activated on this crate will it actually include
/// the tracing code in the non-std environment.
///
/// Because of that optimisation, you should not use the `span!` and `span_*!` macros
/// directly as they yield nothing without the feature present. Instead you should use
/// `enter_span!` and `within_span!` which would strip away even any parameter conversion
/// you do within the span-definition (and thus optimise your performance). For your
/// convineience you directly specify the `Level` and name of the span or use the full
/// feature set of `span!`/`span_*!` on it:
///
/// # Example
///
/// ```rust
/// sp_tracing::enter_span!(sp_tracing::Level::TRACE, "fn wide span");
/// {
/// sp_tracing::enter_span!(sp_tracing::trace_span!("outer-span"));
/// {
/// sp_tracing::enter_span!(sp_tracing::Level::TRACE, "inner-span");
/// // ..
/// } // inner span exists here
/// } // outer span exists here
///
/// sp_tracing::within_span! {
/// sp_tracing::debug_span!("debug-span", you_can_pass="any params");
/// 1 + 1;
/// // some other complex code
/// } // debug span ends here
///
/// ```
///
///
/// # Setup
///
/// This project only provides the macros and facilities to manage tracing
/// it doesn't implement the tracing subscriber or backend directly that is
/// up to the developer integrating it into a specific environment. In native
/// this can and must be done through the regular `tracing`-facitilies, please
/// see their documentation for details.
///
/// On the wasm-side we've adopted a similar approach of having a global
/// `TracingSubscriber` that the macros call and that does the actual work
/// of tracking. To provide your tracking, you must implement `TracingSubscriber`
/// and call `set_tracing_subscriber` at the very beginning of your execution
/// the default subscriber is doing nothing, so any spans or events happening before
/// will not be recorded!
///
mod types;
#[cfg(feature = "std")]
#[doc(hidden)]
pub use tracing;
use tracing;
#[cfg(feature = "std")]
pub mod proxy;
pub use tracing::{
debug, debug_span, error, error_span, info, info_span, trace, trace_span, warn, warn_span,
span, event, Level, Span,
};
pub use crate::types::{
WasmMetadata, WasmEntryAttributes, WasmValuesSet, WasmValue, WasmFields, WasmLevel, WasmFieldName
};
#[cfg(feature = "std")]
use std::sync::atomic::{AtomicBool, Ordering};
/// Try to init a simple tracing subscriber with log compatibility layer.
/// Ignores any error. Useful for testing.
@@ -51,74 +110,127 @@ pub fn try_init_simple() {
let _ = tracing_subscriber::fmt().with_writer(std::io::stderr).try_init();
}
/// Flag to signal whether to run wasm tracing
#[cfg(feature = "std")]
static WASM_TRACING_ENABLED: AtomicBool = AtomicBool::new(false);
pub use crate::types::{
WASM_NAME_KEY, WASM_TARGET_KEY, WASM_TRACE_IDENTIFIER
};
/// Runs given code within a tracing span, measuring it's execution time.
///
/// If tracing is not enabled, the code is still executed.
/// If tracing is not enabled, the code is still executed. Pass in level and name or
/// use any valid `sp_tracing::Span`followe by `;` and the code to execute,
///
/// # Example
///
/// ```
/// sp_tracing::tracing_span! {
/// sp_tracing::within_span! {
/// sp_tracing::Level::TRACE,
/// "test-span";
/// 1 + 1;
/// // some other complex code
/// }
///
/// sp_tracing::within_span! {
/// sp_tracing::span!(sp_tracing::Level::WARN, "warn-span", you_can_pass="any params");
/// 1 + 1;
/// // some other complex code
/// }
///
/// sp_tracing::within_span! {
/// sp_tracing::debug_span!("debug-span", you_can_pass="any params");
/// 1 + 1;
/// // some other complex code
/// }
/// ```
#[cfg(any(feature = "std", feature = "with-tracing"))]
#[macro_export]
macro_rules! tracing_span {
macro_rules! within_span {
(
$span:expr;
$( $code:tt )*
) => {
$span.in_scope(||
{
$( $code )*
}
)
};
(
$lvl:expr,
$name:expr;
$( $code:tt )*
) => {
{
$crate::enter_span!($name);
$( $code )*
$crate::within_span!($crate::span!($crate::Level::TRACE, $name); $( $code )*)
}
}
};
}
#[cfg(all(not(feature = "std"), not(feature = "with-tracing")))]
#[macro_export]
macro_rules! within_span {
(
$span:stmt;
$( $code:tt )*
) => {
$( $code )*
};
(
$lvl:expr,
$name:expr;
$( $code:tt )*
) => {
$( $code )*
};
}
/// Enter a span - noop for `no_std` without `with-tracing`
#[cfg(all(not(feature = "std"), not(feature = "with-tracing")))]
#[macro_export]
macro_rules! enter_span {
( $lvl:expr, $name:expr ) => ( );
( $name:expr ) => ( ) // no-op
}
/// Enter a span.
///
/// The span will be valid, until the scope is left.
/// The span will be valid, until the scope is left. Use either level and name
/// or pass in any valid `sp_tracing::Span` for extended usage. The span will
/// be exited on drop which is at the end of the block or to the next
/// `enter_span!` calls, as this overwrites the local variable. For nested
/// usage or to ensure the span closes at certain time either put it into a block
/// or use `within_span!`
///
/// # Example
///
/// ```
/// sp_tracing::enter_span!("test-span");
/// sp_tracing::enter_span!(sp_tracing::Level::TRACE, "test-span");
/// // previous will be dropped here
/// sp_tracing::enter_span!(
/// sp_tracing::span!(sp_tracing::Level::DEBUG, "debug-span", params="value"));
/// sp_tracing::enter_span!(sp_tracing::info_span!("info-span", params="value"));
///
/// {
/// sp_tracing::enter_span!(sp_tracing::Level::TRACE, "outer-span");
/// {
/// sp_tracing::enter_span!(sp_tracing::Level::TRACE, "inner-span");
/// // ..
/// } // inner span exists here
/// } // outer span exists here
///
/// ```
#[cfg(any(feature = "std", feature = "with-tracing"))]
#[macro_export]
macro_rules! enter_span {
( $name:expr ) => {
let __tracing_span__ = $crate::if_tracing!(
$crate::tracing::span!($crate::tracing::Level::TRACE, $name)
);
let __tracing_guard__ = $crate::if_tracing!(__tracing_span__.enter());
}
}
/// Generates the given code if the tracing dependency is enabled.
#[macro_export]
#[cfg(feature = "std")]
macro_rules! if_tracing {
( $if:expr ) => {{ $if }}
}
#[macro_export]
#[cfg(not(feature = "std"))]
macro_rules! if_tracing {
( $if:expr ) => {{}}
}
#[cfg(feature = "std")]
pub fn wasm_tracing_enabled() -> bool {
WASM_TRACING_ENABLED.load(Ordering::Relaxed)
}
#[cfg(feature = "std")]
pub fn set_wasm_tracing(b: bool) {
WASM_TRACING_ENABLED.store(b, Ordering::Relaxed)
( $span:expr ) => {
// Calling this twice in a row will overwrite (and drop) the earlier
// that is a _documented feature_!
let __within_span__ = $span;
let __tracing_guard__ = __within_span__.enter();
};
( $lvl:expr, $name:expr ) => {
$crate::enter_span!($crate::span!($crate::Level::TRACE, $name))
};
}