Skip to content

Commit

Permalink
instrumented macro implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-rei authored and m-reichert committed Jan 13, 2025
1 parent 3422bd5 commit 4e39879
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
workspace = { members = ["instrumented_macro"] }
[package]
name = "electrs"
version = "0.4.1"
Expand Down Expand Up @@ -76,7 +77,7 @@ tracing = { version = "0.1.40", default-features = false, features = ["attribute
# optional dependencies for electrum-discovery
electrum-client = { version = "0.8", optional = true }
zmq = "0.10.0"

instrumented_macro = { path = "instrumented_macro" }

[dev-dependencies]
bitcoind = { version = "0.34.3", features = ["25_0"] }
Expand Down
12 changes: 12 additions & 0 deletions instrumented_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "instrumented_macro"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
syn = "2.0"
quote = "1.0"
proc-macro2 = "1.0"
26 changes: 26 additions & 0 deletions instrumented_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

#[proc_macro_attribute]
pub fn instrumented(attr: TokenStream, item: TokenStream) -> TokenStream {
let additional_fields = if !attr.is_empty() {
let attr_tokens: proc_macro2::TokenStream = attr.into();
quote! {, #attr_tokens }
} else {
quote! {}
};

let function = parse_macro_input!(item as ItemFn);

let fields_tokens = quote! {
fields(module = module_path!(), file = file!(), line = line!() #additional_fields)
};

let expanded = quote! {
#[tracing::instrument(skip_all, #fields_tokens)]
#function
};

expanded.into()
}

0 comments on commit 4e39879

Please sign in to comment.