Skip to content

Commit

Permalink
Instrument(ed) components using the plain tracing::debug_span! macr…
Browse files Browse the repository at this point in the history
…o instead of using `#[instrument]`

This skips waiting for <tokio-rs/tracing#1819> to land and should also compile a bit faster in some cases.
  • Loading branch information
Tamschi committed Jan 14, 2022
1 parent 34a83b2 commit 905cfd2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 50 deletions.
13 changes: 0 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ lignin-schema = { git = "https://github.com/Tamschi/lignin-schema.git", branch =
rhizome = { version = "0.0.1", features = ["macros"] } # public
static_assertions = "1.1.0"
typed-builder = "0.9.0" # semi-public
tracing = { version = "0.1.29", optional = true, default-features = false, features = ["attributes"] }
tracing = { version = "0.1.29", optional = true, default-features = false }

[dev-dependencies]
cargo-husky = "1.5.0"
Expand Down
2 changes: 0 additions & 2 deletions book/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ proc-macro2 = "1.0.24" #TODO
pulldown-cmark = "0.7.0" #TODO
pulldown-cmark-to-cmark = "5.0.0" #TODO
quote = "1.0.7"
tracing = { version = "0.1.29", default-features = false }

[dev-dependencies]
version-sync = "0.9.1"
Expand All @@ -45,4 +44,3 @@ pulldown-cmark = "0.7.0"
pulldown-cmark-to-cmark = "5.0.0"
quote = "1.0.7"
walkdir = "2.3.1"
tracing = { version = "0.1.29", default-features = false }
22 changes: 8 additions & 14 deletions proc-macro-definitions/src/component_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,18 +650,15 @@ impl ComponentDeclaration {
#(#struct_definition)*

impl#component_impl_generics #component_name#component_type_generics #component_where_clause {
#[::#asteracea::__::tracing::instrument(
level = "trace",
name = #new_span_name,
skip_all,
fields(#(#constructor_args_tracing_fields,)*),
)]
#(#constructor_attributes)*
pub fn #new#new_generics(
parent_node: &::std::sync::Arc<#asteracea::rhizome::Node>,
args: #new_args_name#new_args_generic_args,
) -> ::std::result::Result<Self, ::#asteracea::error::Escalation> where Self: 'a + 'static { // TODO: Self: 'static is necessary because of `derive_for::<Self>`, but that's not really a good approach... Using derived IDs would be better.
// Split off so that `#[instrument]` can see fields.
// Tracing's `#[instrument]` macro is slightly unwieldy in terms of compilation.
// The following should be equivalent to skipping all fields and setting them one by one:
let _tracing_span = ::#asteracea::__::tracing::debug_span!(#new_span_name, #(#constructor_args_tracing_fields,)*).entered();

let #new_args_name {
#(#constructor_args_field_patterns,)*
__Asteracea__phantom: _,
Expand Down Expand Up @@ -692,19 +689,16 @@ impl ComponentDeclaration {
#new_args_name::builder()
}

#[::#asteracea::__::tracing::instrument(
level = "trace",
name = #render_span_name,
skip_all,
fields(#(#render_args_tracing_fields,)*),
)]
#(#render_attributes)*
pub fn #render#render_generics(
#render_self: ::std::pin::Pin<&'a Self>,
#bump: &'bump #asteracea::bumpalo::Bump,
args: #render_args_name#render_args_generic_args,
) #render_type {
// Split off so that `#[instrument]` can see fields.
// Tracing's `#[instrument]` macro is slightly unwieldy in terms of compilation.
// The following should be equivalent to skipping all fields and setting them one by one:
let _tracing_span = ::#asteracea::__::tracing::debug_span!(#new_span_name, #(#render_args_tracing_fields,)*).entered();

let #render_args_name {
#(#render_args_field_patterns,)*
__Asteracea__phantom: _,
Expand Down
16 changes: 15 additions & 1 deletion proc-macro-definitions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ use quote::{quote, quote_spanned};
use std::iter;
use syn::{
parse::{Parse, ParseStream},
parse_macro_input, Error, Ident, Result,
parse_macro_input,
spanned::Spanned,
Error, Ident, Result,
};
use tap::Conv;

mod component_declaration;
mod map_message;
Expand Down Expand Up @@ -196,3 +199,14 @@ pub fn discard_these_attribute_args(args: TokenStream1, item: TokenStream1) -> T
drop(args);
item
}

/// Returns just an `::asteracea::__::tracing::Span`, preserving [`Span`] location but resolving it at [`Span::mixed_site()`](`Span::mixed_site`).
#[proc_macro]
pub fn fake_span(input: TokenStream1) -> TokenStream1 {
let span = input
.conv::<TokenStream2>()
.span()
.resolved_at(Span::mixed_site());
let asteracea = asteracea_ident(span);
quote_spanned!(span=> ::#asteracea::__::tracing::Span).into()
}
42 changes: 26 additions & 16 deletions src/__.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ pub use tracing;

#[cfg(not(feature = "tracing"))]
pub mod tracing {
pub use asteracea_proc_macro_definitions::discard_these_attribute_args as instrument;
pub use asteracea_proc_macro_definitions::fake_span as debug_span;

pub struct Span;
impl Span {
#[must_use]
pub fn entered(self) {
drop(self)
}
}
}

/// Only implemented for functions that have a signature ABI-compatible with `fn(*const R, T)`!
Expand Down Expand Up @@ -78,22 +86,24 @@ impl AnonymousContentParentParametersBuilder {
/// struct YesDebug;
/// struct NoDebug;
///
/// #[::asteracea::__::tracing::instrument(skip_all, fields(
/// value = {
/// use ::asteracea::__::CoerceTracingValue;
/// (&&&&::asteracea::__::InertWrapper(&value)).coerce()
/// },
/// debug = {
/// use ::asteracea::__::CoerceTracingValue;
/// (&&&&::asteracea::__::InertWrapper(&debug_)).coerce()
/// },
/// neither = {
/// use ::asteracea::__::CoerceTracingValue;
/// (&&&&::asteracea::__::InertWrapper(&neither)).coerce()
/// },
/// ))]
/// //FIXME: `#[instrument]` isn't hygienic, so the parameter can't be called `debug`. See <https://github.com/tokio-rs/tracing/issues/1318>.
/// //FIXME: `debug_span` isn't entirely hygienic, so the parameter can't be called `debug`.
/// // See <https://github.com/tokio-rs/tracing/issues/1318>.
/// pub fn auto_values(value: u32, debug_: YesDebug, neither: NoDebug) {
/// let _span = ::asteracea::__::tracing::debug_span!(
/// "auto_values",
/// value = {
/// use ::asteracea::__::CoerceTracingValue;
/// (&&&&&::asteracea::__::InertWrapper(&value)).coerce()
/// },
/// debug = {
/// use ::asteracea::__::CoerceTracingValue;
/// (&&&&&::asteracea::__::InertWrapper(&debug_)).coerce()
/// },
/// neither = {
/// use ::asteracea::__::CoerceTracingValue;
/// (&&&&&::asteracea::__::InertWrapper(&neither)).coerce()
/// },
/// ).entered();
/// drop((value, debug_, neither))
/// }
/// ```
Expand Down
6 changes: 3 additions & 3 deletions src/components/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
error::Escalation,
__::{tracing::instrument, Built},
__::{tracing::debug_span, Built},
};
use ::std::pin::Pin;
use bumpalo::Bump;
Expand All @@ -16,15 +16,14 @@ pub struct Router;

const _: () = {
impl Router {
#[instrument(name = "Router::new", skip_all)]
pub fn new(
_parent_node: &Arc<rhizome::Node>,
RouterNewArgs {}: RouterNewArgs,
) -> Result<Self, Escalation> {
let _span = debug_span!("Router::new").entered();
Ok(Self)
}

#[instrument(name = "Router::render", skip_all, fields(path = path))]
pub fn render<'bump>(
self: Pin<&Self>,
bump: &'bump Bump,
Expand All @@ -34,6 +33,7 @@ const _: () = {
rest,
}: RouterRenderArgs<'_, 'bump>,
) -> Result<Node<'bump, ThreadSafe>, Escalation> {
let _span = debug_span!("Router::render", path).entered();
for route in __Asteracea__anonymous_content {
let (RouterParentParameters { paths }, render_content) = route;
for route in paths {
Expand Down

0 comments on commit 905cfd2

Please sign in to comment.