Skip to content

Commit 8824a60

Browse files
committed
replace with global tracing
1 parent 70492f5 commit 8824a60

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

primitives/runtime-interface/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1515
[dependencies]
1616
sp-wasm-interface = { version = "2.0.0-rc5", path = "../wasm-interface", default-features = false }
1717
sp-std = { version = "2.0.0-rc5", default-features = false, path = "../std" }
18-
sp-tracing = { version = "2.0.0-rc5", default-features = false, path = "../tracing" }
18+
tracing = { version = "0.1.17", default-features = false, optional = true }
1919
sp-runtime-interface-proc-macro = { version = "2.0.0-rc5", path = "proc-macro" }
2020
sp-externalities = { version = "0.8.0-rc5", optional = true, path = "../externalities" }
2121
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false }
@@ -35,7 +35,8 @@ default = [ "std" ]
3535
std = [
3636
"sp-wasm-interface/std",
3737
"sp-std/std",
38-
"sp-tracing/std",
38+
"tracing",
39+
"tracing/std",
3940
"codec/std",
4041
"sp-externalities",
4142
"primitive-types/std",

primitives/runtime-interface/proc-macro/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ pub fn runtime_interface(
3939
) -> proc_macro::TokenStream {
4040
let trait_def = parse_macro_input!(input as ItemTrait);
4141
let wasm_only = parse_macro_input!(attrs as Option<runtime_interface::keywords::wasm_only>);
42+
// FIXME: actual parse for this
43+
let no_tracing : Option<()> = None; //parse_macro_input!(tracing_attrs as Option<runtime_interface::keywords::no_tracing>);
4244

43-
runtime_interface::runtime_interface_impl(trait_def, wasm_only.is_some())
45+
runtime_interface::runtime_interface_impl(trait_def, wasm_only.is_some(), no_tracing.is_none())
4446
.unwrap_or_else(|e| e.to_compile_error())
4547
.into()
4648
}

primitives/runtime-interface/proc-macro/src/runtime_interface/bare_function_interface.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use std::iter;
4646

4747
/// Generate one bare function per trait method. The name of the bare function is equal to the name
4848
/// of the trait method.
49-
pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream> {
49+
pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool) -> Result<TokenStream> {
5050
let trait_name = &trait_def.ident;
5151
let runtime_interface = get_runtime_interface(trait_def)?;
5252

@@ -63,7 +63,7 @@ pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream
6363
// earlier versions compatibility dispatch (only std variant)
6464
let result: Result<TokenStream> = runtime_interface.all_versions().try_fold(token_stream?, |mut t, (version, method)|
6565
{
66-
t.extend(function_std_impl(trait_name, method, version, is_wasm_only)?);
66+
t.extend(function_std_impl(trait_name, method, version, is_wasm_only, with_tracing)?);
6767
Ok(t)
6868
});
6969

@@ -145,6 +145,7 @@ fn function_std_impl(
145145
method: &TraitItemMethod,
146146
version: u32,
147147
is_wasm_only: bool,
148+
with_tracing: bool,
148149
) -> Result<TokenStream> {
149150
let function_name = create_function_ident_with_version(&method.sig.ident, version);
150151
let function_name_str = function_name.to_string();
@@ -164,6 +165,14 @@ fn function_std_impl(
164165
}
165166
).take(1),
166167
);
168+
let tracing = if with_tracing {
169+
Some(quote!(
170+
let __trace_span = #crate_::tracing::trace_span!(#function_name_str);
171+
let __guard = __trace_span.enter();
172+
))
173+
} else {
174+
None
175+
};
167176
let return_value = &method.sig.output;
168177
let attrs = method.attrs.iter().filter(|a| !a.path.is_ident("version"));
169178
// Don't make the function public accessible when this is a wasm only interface.
@@ -174,7 +183,7 @@ fn function_std_impl(
174183
#[cfg(feature = "std")]
175184
#( #attrs )*
176185
fn #function_name( #( #args, )* ) #return_value {
177-
#crate_::sp_tracing::enter_span!(#function_name_str);
186+
#tracing
178187
#call_to_trait
179188
}
180189
}

primitives/runtime-interface/proc-macro/src/runtime_interface/host_function_interface.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::iter::{Iterator, self};
4343

4444
/// Generate the extern host functions for wasm and the `HostFunctions` struct that provides the
4545
/// implementations for the host functions on the host.
46-
pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream> {
46+
pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool) -> Result<TokenStream> {
4747
let trait_name = &trait_def.ident;
4848
let extern_host_function_impls = get_runtime_interface(trait_def)?
4949
.latest_versions()
@@ -57,7 +57,7 @@ pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream
5757
t.extend(generate_exchangeable_host_function(m)?);
5858
Ok::<_, Error>(t)
5959
})?;
60-
let host_functions_struct = generate_host_functions_struct(trait_def, is_wasm_only)?;
60+
let host_functions_struct = generate_host_functions_struct(trait_def, is_wasm_only, with_tracing)?;
6161

6262
Ok(
6363
quote! {
@@ -158,13 +158,21 @@ fn generate_exchangeable_host_function(method: &TraitItemMethod) -> Result<Token
158158

159159
/// Generate the `HostFunctions` struct that implements `wasm-interface::HostFunctions` to provide
160160
/// implementations for the extern host functions.
161-
fn generate_host_functions_struct(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream> {
161+
fn generate_host_functions_struct(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool)
162+
-> Result<TokenStream>
163+
{
162164
let crate_ = generate_crate_access();
163165

164166
let host_functions = get_runtime_interface(trait_def)?
165167
.all_versions()
166168
.map(|(version, method)|
167-
generate_host_function_implementation(&trait_def.ident, method, version, is_wasm_only)
169+
generate_host_function_implementation(
170+
&trait_def.ident,
171+
method,
172+
version,
173+
is_wasm_only,
174+
with_tracing
175+
)
168176
)
169177
.collect::<Result<Vec<_>>>()?;
170178

@@ -194,6 +202,7 @@ fn generate_host_function_implementation(
194202
method: &TraitItemMethod,
195203
version: u32,
196204
is_wasm_only: bool,
205+
with_tracing: bool,
197206
) -> Result<TokenStream> {
198207
let name = create_host_function_ident(&method.sig.ident, version, trait_name).to_string();
199208
let struct_name = Ident::new(&name.to_pascal_case(), Span::call_site());
@@ -207,6 +216,15 @@ fn generate_host_function_implementation(
207216
let host_function_call = generate_host_function_call(&method.sig, version, is_wasm_only);
208217
let into_preallocated_ffi_value = generate_into_preallocated_ffi_value(&method.sig)?;
209218
let convert_return_value = generate_return_value_into_wasm_value(&method.sig);
219+
let tracing = if with_tracing {
220+
Some(quote!(
221+
let __trace_span = #crate_::tracing::trace_span!(#name);
222+
let __guard = __trace_span.enter();
223+
))
224+
} else {
225+
None
226+
};
227+
210228

211229
Ok(
212230
quote! {
@@ -227,7 +245,7 @@ fn generate_host_function_implementation(
227245
__function_context__: &mut dyn #crate_::sp_wasm_interface::FunctionContext,
228246
args: &mut dyn Iterator<Item = #crate_::sp_wasm_interface::Value>,
229247
) -> std::result::Result<Option<#crate_::sp_wasm_interface::Value>, String> {
230-
#crate_::sp_tracing::enter_span!(#name);
248+
#tracing
231249
#( #wasm_to_ffi_values )*
232250
#( #ffi_to_host_values )*
233251
#host_function_call

primitives/runtime-interface/proc-macro/src/runtime_interface/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,22 @@ mod trait_decl_impl;
3333
pub mod keywords {
3434
// Custom keyword `wasm_only` that can be given as attribute to [`runtime_interface`].
3535
syn::custom_keyword!(wasm_only);
36+
// Custom keyword `no_tracing` that can be given as attribute to [`runtime_interface`].
37+
syn::custom_keyword!(no_tracing);
3638
}
3739

3840
/// Implementation of the `runtime_interface` attribute.
3941
///
4042
/// It expects the trait definition the attribute was put above and if this should be an wasm only
4143
/// interface.
42-
pub fn runtime_interface_impl(trait_def: ItemTrait, is_wasm_only: bool) -> Result<TokenStream> {
43-
let bare_functions = bare_function_interface::generate(&trait_def, is_wasm_only)?;
44+
pub fn runtime_interface_impl(trait_def: ItemTrait, is_wasm_only: bool, with_tracing: bool)
45+
-> Result<TokenStream>
46+
{
47+
let bare_functions = bare_function_interface::generate(&trait_def, is_wasm_only, with_tracing)?;
4448
let crate_include = generate_runtime_interface_include();
4549
let mod_name = Ident::new(&trait_def.ident.to_string().to_snake_case(), Span::call_site());
4650
let trait_decl_impl = trait_decl_impl::process(&trait_def, is_wasm_only)?;
47-
let host_functions = host_function_interface::generate(&trait_def, is_wasm_only)?;
51+
let host_functions = host_function_interface::generate(&trait_def, is_wasm_only, with_tracing)?;
4852
let vis = trait_def.vis;
4953
let attrs = &trait_def.attrs;
5054

primitives/runtime-interface/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ extern crate self as sp_runtime_interface;
111111
pub use sp_wasm_interface;
112112

113113
#[doc(hidden)]
114-
pub use sp_tracing;
114+
#[cfg(feature = "std")]
115+
use tracing;
115116

116117
#[doc(hidden)]
117118
pub use sp_std;

0 commit comments

Comments
 (0)