Improve tracing (#5698)

* Improve tracing implementation

* Enable tracing in runtime interfaces

* Switch to `TRACE` level
This commit is contained in:
Bastian Köcher
2020-04-20 14:37:27 +02:00
committed by GitHub
parent ca1c60c2cf
commit 1d1caed335
18 changed files with 206 additions and 119 deletions
@@ -15,6 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
sp-wasm-interface = { version = "2.0.0-dev", path = "../wasm-interface", default-features = false }
sp-std = { version = "2.0.0-dev", default-features = false, path = "../std" }
sp-tracing = { version = "2.0.0-dev", default-features = false, path = "../tracing" }
sp-runtime-interface-proc-macro = { version = "2.0.0-dev", path = "proc-macro" }
sp-externalities = { version = "0.8.0-dev", optional = true, path = "../externalities" }
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false }
@@ -34,6 +35,7 @@ default = [ "std" ]
std = [
"sp-wasm-interface/std",
"sp-std/std",
"sp-tracing/std",
"codec/std",
"sp-externalities",
"primitive-types/std",
@@ -146,6 +146,7 @@ fn function_std_impl(
is_wasm_only: bool,
) -> Result<TokenStream> {
let function_name = create_function_ident_with_version(&method.sig.ident, version);
let function_name_str = function_name.to_string();
let crate_ = generate_crate_access();
let args = get_function_arguments(&method.sig).map(FnArg::Typed).chain(
@@ -172,6 +173,7 @@ fn function_std_impl(
#[cfg(feature = "std")]
#( #attrs )*
fn #function_name( #( #args, )* ) #return_value {
#crate_::sp_tracing::enter_span!(#function_name_str);
#call_to_trait
}
}
@@ -226,6 +226,7 @@ fn generate_host_function_implementation(
__function_context__: &mut dyn #crate_::sp_wasm_interface::FunctionContext,
args: &mut dyn Iterator<Item = #crate_::sp_wasm_interface::Value>,
) -> std::result::Result<Option<#crate_::sp_wasm_interface::Value>, String> {
#crate_::sp_tracing::enter_span!(#name);
#( #wasm_to_ffi_values )*
#( #ffi_to_host_values )*
#host_function_call
@@ -109,6 +109,9 @@ extern crate self as sp_runtime_interface;
#[cfg(feature = "std")]
pub use sp_wasm_interface;
#[doc(hidden)]
pub use sp_tracing;
#[doc(hidden)]
pub use sp_std;
@@ -19,3 +19,4 @@ sp-runtime-interface-test-wasm-deprecated = { version = "2.0.0-dev", path = "../
sp-state-machine = { version = "0.8.0-dev", path = "../../../primitives/state-machine" }
sp-runtime = { version = "2.0.0-dev", path = "../../runtime" }
sp-io = { version = "2.0.0-dev", path = "../../io" }
tracing = "0.1.13"
@@ -27,6 +27,8 @@ use sp_runtime_interface_test_wasm_deprecated::WASM_BINARY as WASM_BINARY_DEPREC
use sp_wasm_interface::HostFunctions as HostFunctionsT;
use sc_executor::CallInWasm;
use std::{collections::HashSet, sync::{Arc, Mutex}};
type TestExternalities = sp_state_machine::TestExternalities<sp_runtime::traits::BlakeTwo256, u64>;
fn call_wasm_method<HF: HostFunctionsT>(binary: &[u8], method: &str) -> TestExternalities {
@@ -150,3 +152,47 @@ fn test_versionining_with_new_host_works() {
"test_versionning_works",
);
}
#[test]
fn test_tracing() {
use tracing::span::Id as SpanId;
#[derive(Clone)]
struct TracingSubscriber(Arc<Mutex<Inner>>);
#[derive(Default)]
struct Inner {
spans: HashSet<&'static str>,
}
impl tracing::subscriber::Subscriber for TracingSubscriber {
fn enabled(&self, _: &tracing::Metadata) -> bool { true }
fn new_span(&self, span: &tracing::span::Attributes) -> tracing::Id {
let mut inner = self.0.lock().unwrap();
let id = SpanId::from_u64((inner.spans.len() + 1) as _);
inner.spans.insert(span.metadata().name());
id
}
fn record(&self, _: &SpanId, _: &tracing::span::Record) {}
fn record_follows_from(&self, _: &SpanId, _: &SpanId) {}
fn event(&self, _: &tracing::Event) {}
fn enter(&self, _: &SpanId) {}
fn exit(&self, _: &SpanId) {}
}
let subscriber = TracingSubscriber(Default::default());
let _guard = tracing::subscriber::set_default(subscriber.clone());
// Call some method to generate a trace
call_wasm_method::<HostFunctions>(&WASM_BINARY[..], "test_return_data");
let inner = subscriber.0.lock().unwrap();
assert!(inner.spans.contains("return_input_version_1"));
assert!(inner.spans.contains("ext_test_api_return_input_version_1"));
}