RPC to trace execution of specified block (#7780)

* Add filter reload handle

* add RPC, move logging module from cli to tracing

* remove dup fn

* working example

* Update client/rpc-api/src/system/mod.rs

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>

* Prefer "set" to "reload"

* Re-enable the commented out features of the logger

* Remove duplicate code

* cleanup

* unneeded lvar

* Bump to latest patch release

* Add new CLI option to disable log filter reloading,

Move profiling CLI options to SharedParams

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Applied suggestions from reviews

* Fix calls to init_logger()

* Handle errors when parsing logging directives

* Deny `system_setLogFilter` RPC by default

* One more time

* Don't ignore parse errors for log directives set via CLI or RPC

* Improve docs

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/cli/src/config.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fix merge errors

* include default directives with system_setLogFilter RPC,

implement system_rawSetLogFilter RPC to exclude defaults

* docs etc...

* update test

* refactor: rename fn

* Add a test for system_set_log_filter – NOTE: the code should likely change to return an error when bad directives are passed

* Update client/cli/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Address review grumbles

* Add doc note on panicking behaviour

* print all invalid directives before panic

* change RPCs to: addLogFilter and resetLogFilter

* make CLI log directives default

* add comments

* restore previous behaviour to panic when hard-coded directives are invalid

* change/refactor directive parsing

* fix line width

* add test for log filter reloading

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* finish up suggestions from code review

* improve test

* change expect message

* change fn name

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* add docs, remove unused fn

* propagate Err on invalid log directive

* Update tracing-subscriber version

* Improve docs for `disable_log_reloading` CLI param

* WIP implementation: RPC and trace capturing

* WIP

* fix incorrect number of digest items

* return errors

* add From impl for Values, rename structs

* fixes

* implement option to choose targets for traces

* rename fn

* fix EnvFilter and add root span

* fix root span

* add docs, remove unnecessary traits

* fix regression on parent_id introduced in a9c73113a8

* fix line width

* remove unused

* include block hash, parent hash & targets in response

* move types from sp-tracing into sp-rpc

move block and parent hash into root of BlockTrace

* switch from log::trace to tracing::trace in state-machine

* use unsigned integer type to represent Ext::id in traces

* ensure id is unique by implementing Subscriber

tracing_subscriber::FmtSubscriber does not guarantee
unique ids

* indentation

* fix typo

* update types

* add sp_io::storage events

* Change response format

- update types
- record distinct timestamps
- sort spans by first entered

* convert to HexDisplay, refactor

* Sort out fallout from merge

* Update client/rpc-api/src/state/mod.rs

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Exit early unless the node runs with --rpc-methods=Unsafe

* Better error handling

* Use wasm-timer

* revert trace alteration in `state-machine` and remove events in `sp_io::storage`

Resolve in follow-up PR

* Review feedback: less collects

* Without Arcs

* Fix span exit

* typo

* cleanup

* Add a few debug messages to tracing module

* Structure traces state-machine/ext; Dispatchable extrinsics spans not working

* Correctly encode Option storage values

* Remove test field for Put and Get

* Try out some changes to dispatch macro

* Add various log messages in dispatch

* Add span dispatch span to new proc macro

* Remove debug messages in dispatch

* Trivial clean up

* Structure remaining state-machine traces (ChangesRoot*)

* Removed unnesecary tracing targets

* Remove log

* New cargo.lock post merge

* Add logging for wasm_overrides

* remove temp logs

* remove temp logs

* remove unused dep

* remove temp logs

* add logging to wasm_overrides

* add logging to state_tracing

* add logging for spans to substrate (includes timings)

* Skip serializing some event fields; Remove most storage traces

* Bring back all ext.rs traces

* Do not skip bool values in events

* Skip serializing span values

* Serialize span values; remove some trace events in ext

* Remove more trace events

* Delete commented out traces

* Remove all unused traces

* Add event filtering

* Fix typo

* wip - change response types to be more efficient

missing import

type

* Serialize struct fields as camelCase

* Add back in event filtering

* Remove name field from event

* Sort spans by time entered

* Sort spans in ASCending order

* Add storage keys target param to rpc

* Limit payload size; improve hash fields; include storage keys

- cleanup event_key_filter
- better block hash representation
- limit payload size
- cleanup based on andrews comments

* Error when serialized payload is to big

* Import MAX_PAYLOAD from rpc-servers

* Clean up ext.rs

* Misc. cleaning and comments

* Strict ordering span Id; no span sort; adjust for rpc base payload

* Add RPC docs to rpc-api/src/state/mod

* Make params bullet points

* Update primitives/rpc/src/tracing.rs

* Put all tracing logic within trace

* Remove attr.record in new_span

* Add back value record in new_span

* restore result collection in ext

* Revert "Add back value record in new_span"

This reverts commit baf1a735f23e5eef1bf6264adfabb788910fa661.

* 🤦

* more 🤦

* Update docs; Try fix line width issues

* Improve docs

* Improve docs

* Remove default key filters + add key recs to docs

* Try restore old traces

* Add back doc comment

* Clean up newlines in ext.rs

* More new line remova;
l

* Use FxHashMap

* Try use EnvFilter directives for event filtering

* Remove directive, filter events by fields

* Use trace metadata correctly

* Try EnvFilter directive with all default targets

* Revert "Try EnvFilter directive with all default targets"

This reverts commit 4cc6ebc721d207c3b846444174f89d45038525ac.

* Clean up clippy warning

* Incorporate Niklas feedback

* Update trace/log macro calls to have better syntx

* Use Ordering::Relaxed

* Improve patch and filter doc comment

* Clean up `BlockSubscriber::new`

* Try optimize `BlockSubscriber::enabled`

* Apply suggestions from code review

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

* Apply suggestions from code review

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

* Use contains_key

* use heuristic for payload size

* Add error tupe for client::tracing::block

* Minor tweaks

* Make a note about `--features with-tracing`

* Add CURL example to RPC docs

* Link to substrate-archibe wasm

* Trivial doc clean up based on David feedback

* Explicit result type name

* Respect line length

* Use the error

* Don't print timings when spans close

* Fix failing sc-rpc-api

* Update  sp-tracing inner-line doc

* Update client/tracing/src/block/mod.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/service/src/client/call_executor.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/service/src/client/call_executor.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/tracing/src/block/mod.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/tracing/src/block/mod.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Address some review grumbles

* Update primitives/state-machine/src/ext.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Use result_encoded structure fields in ext.rs

* Use value key for ext put

* Add notes about tracing key names matter

Co-authored-by: Matt <mattrutherford@users.noreply.github.com>
Co-authored-by: David <dvdplm@gmail.com>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: emostov <32168567+emostov@users.noreply.github.com>
This commit is contained in:
mattrutherford
2021-05-05 08:44:36 +01:00
committed by GitHub
parent 37ea3958d9
commit 6e8957b095
25 changed files with 803 additions and 66 deletions
+2
View File
@@ -15,6 +15,8 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
serde = { version = "1.0.101", features = ["derive"] }
sp-core = { version = "3.0.0", path = "../core" }
tracing-core = "0.1.17"
rustc-hash = "1.1.0"
[dev-dependencies]
serde_json = "1.0.41"
+1
View File
@@ -21,6 +21,7 @@
pub mod number;
pub mod list;
pub mod tracing;
/// A util function to assert the result of serialization and deserialization is the same.
#[cfg(test)]
+98
View File
@@ -0,0 +1,98 @@
// This file is part of Substrate.
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Types for working with tracing data
use serde::{Serialize, Deserialize};
use rustc_hash::FxHashMap;
/// Container for all related spans and events for the block being traced.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct BlockTrace {
/// Hash of the block being traced
pub block_hash: String,
/// Parent hash
pub parent_hash: String,
/// Module targets that were recorded by the tracing subscriber.
/// Empty string means record all targets.
pub tracing_targets: String,
/// Storage key targets used to filter out events that do not have one of the storage keys.
/// Empty string means do not filter out any events.
pub storage_keys: String,
/// Vec of tracing spans
pub spans: Vec<Span>,
/// Vec of tracing events
pub events: Vec<Event>,
}
/// Represents a tracing event, complete with recorded data.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Event {
/// Event target
pub target: String,
/// Associated data
pub data: Data,
/// Parent id, if it exists
pub parent_id: Option<u64>,
}
/// Represents a single instance of a tracing span.
///
/// Exiting a span does not imply that the span will not be re-entered.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Span {
/// id for this span
pub id: u64,
/// id of the parent span, if any
pub parent_id: Option<u64>,
/// Name of this span
pub name: String,
/// Target, typically module
pub target: String,
/// Indicates if the span is from wasm
pub wasm: bool,
}
/// Holds associated values for a tracing span.
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Data {
/// HashMap of `String` values recorded while tracing
pub string_values: FxHashMap<String, String>,
}
/// Error response for the `state_traceBlock` RPC.
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TraceError {
/// Error message
pub error: String,
}
/// Response for the `state_traceBlock` RPC.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub enum TraceBlockResponse {
/// Error block tracing response
TraceError(TraceError),
/// Successful block tracing response
BlockTrace(BlockTrace)
}
@@ -29,6 +29,7 @@ rand = { version = "0.7.2", optional = true }
sp-externalities = { version = "0.9.0", path = "../externalities", default-features = false }
smallvec = "1.4.1"
sp-std = { version = "3.0.0", default-features = false, path = "../std" }
tracing = { version = "0.1.22", optional = true }
[dev-dependencies]
hex-literal = "0.3.1"
@@ -52,4 +53,5 @@ std = [
"parking_lot",
"rand",
"sp-panic-handler",
"tracing"
]
+30 -9
View File
@@ -201,11 +201,22 @@ where
let _guard = guard();
let result = self.overlay.storage(key).map(|x| x.map(|x| x.to_vec())).unwrap_or_else(||
self.backend.storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL));
trace!(target: "state", "{:04x}: Get {}={:?}",
self.id,
HexDisplay::from(&key),
result.as_ref().map(HexDisplay::from)
// NOTE: be careful about touching the key names used outside substrate!
trace!(
target: "state",
method = "Get",
ext_id = self.id,
key = %HexDisplay::from(&key),
result = ?result.as_ref().map(HexDisplay::from),
result_encoded = %HexDisplay::from(
&result
.as_ref()
.map(|v| EncodeOpaqueValue(v.clone()))
.encode()
),
);
result
}
@@ -354,17 +365,27 @@ where
}
fn place_storage(&mut self, key: StorageKey, value: Option<StorageValue>) {
trace!(target: "state", "{:04x}: Put {}={:?}",
self.id,
HexDisplay::from(&key),
value.as_ref().map(HexDisplay::from)
);
let _guard = guard();
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to directly set child storage key");
return;
}
// NOTE: be careful about touching the key names used outside substrate!
trace!(
target: "state",
method = "Put",
ext_id = self.id,
key = %HexDisplay::from(&key),
value = ?value.as_ref().map(HexDisplay::from),
value_encoded = %HexDisplay::from(
&value
.as_ref()
.map(|v| EncodeOpaqueValue(v.clone()))
.encode()
),
);
self.mark_dirty();
self.overlay.set_storage(key, value);
}
@@ -46,7 +46,9 @@ pub use std_reexport::*;
#[cfg(feature = "std")]
pub use execution::*;
#[cfg(feature = "std")]
pub use log::{debug, warn, trace, error as log_error};
pub use log::{debug, warn, error as log_error};
#[cfg(feature = "std")]
pub use tracing::trace;
/// In no_std we skip logs for state_machine, this macro
/// is a noops.
+10
View File
@@ -24,6 +24,11 @@ tracing = { version = "0.1.25", default-features = false }
tracing-core = { version = "0.1.17", default-features = false }
log = { version = "0.4.8", optional = true }
tracing-subscriber = { version = "0.2.15", optional = true, features = ["tracing-log"] }
parking_lot = { version = "0.10.0", optional = true }
erased-serde = { version = "0.3.9", optional = true }
serde = { version = "1.0.101", optional = true }
serde_json = { version = "1.0.41", optional = true }
slog = { version = "2.5.2", features = ["nested-values"], optional = true }
[features]
default = [ "std" ]
@@ -39,4 +44,9 @@ std = [
"sp-std/std",
"log",
"tracing-subscriber",
"parking_lot",
"erased-serde",
"serde",
"serde_json",
"slog"
]
+1 -1
View File
@@ -1,6 +1,6 @@
Substrate tracing primitives and macros.
To trace functions or invidual code in Substrate, this crate provides [`within_span`]
To trace functions or individual 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
+23 -21
View File
@@ -28,14 +28,36 @@
//! 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.
//!
//! Note: The `tracing` crate requires trace metadata to be static. This does not work
//! for wasm code in substrate, as it is regularly updated with new code from on-chain
//! events. The workaround for this is for the wasm tracing wrappers to put the
//! `name` and `target` data in the `values` map (normally they would be in the static
//! metadata assembled at compile time).
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
use tracing;
pub use tracing::{
debug, debug_span, error, error_span, event, info, info_span, Level, span, Span,
trace, trace_span, warn, warn_span,
};
pub use crate::types::{
WasmEntryAttributes, WasmFieldName, WasmFields, WasmLevel, WasmMetadata, WasmValue,
WasmValuesSet
};
#[cfg(feature = "std")]
pub use crate::types::{
WASM_NAME_KEY, WASM_TARGET_KEY, WASM_TRACE_IDENTIFIER
};
/// 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
/// have done, the wasm implementation diverges slightly and is optimised for that 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
@@ -86,23 +108,9 @@
/// 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")]
use tracing;
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
};
/// Try to init a simple tracing subscriber with log compatibility layer.
/// Ignores any error. Useful for testing.
#[cfg(feature = "std")]
@@ -112,12 +120,6 @@ pub fn try_init_simple() {
.with_writer(std::io::stderr).try_init();
}
#[cfg(feature = "std")]
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. Pass in level and name or
@@ -53,8 +53,6 @@ impl From<&tracing_core::Level> for WasmLevel {
}
}
impl core::default::Default for WasmLevel {
fn default() -> Self {
WasmLevel::TRACE