Skip to content

Commit

Permalink
feat: add #[trace(full_path = true)]
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Lok <[email protected]>
  • Loading branch information
andylokandy committed Oct 27, 2023
1 parent d48892d commit 4c126f3
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## v0.6.1

- Add macro attribute `#[trace(full_path = true)]` to use the full path of the function instead of only the function name.

## v0.6.0

- Span name and event name now accept both `&'static str` and `String` (`Into<Cow<'static, str>>`), which previously only accept `&'static str`.
Expand Down
2 changes: 1 addition & 1 deletion minitrace-datadog/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace-datadog"
version = "0.6.0"
version = "0.6.1"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion minitrace-jaeger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace-jaeger"
version = "0.6.0"
version = "0.6.1"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion minitrace-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace-macro"
version = "0.6.0"
version = "0.6.1"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand Down
Binary file removed minitrace-macro/img/pipeline.png
Binary file not shown.
59 changes: 54 additions & 5 deletions minitrace-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@ use syn::spanned::Spanned;
use syn::*;

struct Args {
name: String,
name: Name,
enter_on_poll: bool,
}

enum Name {
Function(String),
FullPath,
}

impl Args {
fn parse(default_name: String, input: AttributeArgs) -> Args {
fn parse(func_name: String, input: AttributeArgs) -> Args {
if input.len() > 2 {
abort_call_site!("too many arguments");
}

let mut args = HashSet::new();
let mut name = default_name;
let mut func_name = func_name;
let mut full_path = false;
let mut enter_on_poll = false;

for arg in &input {
Expand All @@ -40,9 +46,17 @@ impl Args {
lit: Lit::Str(s),
..
})) if path.is_ident("name") => {
name = s.value();
func_name = s.value();
args.insert("name");
}
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
path,
lit: Lit::Bool(b),
..
})) if path.is_ident("full_path") => {
full_path = b.value;
args.insert("full_path");
}
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
path,
lit: Lit::Bool(b),
Expand All @@ -55,6 +69,15 @@ impl Args {
}
}

let name = if full_path {
if args.contains("name") {
abort_call_site!("`name` and `full_path` can not be used together");
}
Name::FullPath
} else {
Name::Function(func_name)
};

if args.len() != input.len() {
abort_call_site!("duplicated arguments");
}
Expand All @@ -74,6 +97,13 @@ impl Args {
/// The `#[trace]` attribute requires a local parent context to function correctly. Ensure that
/// the function annotated with `#[trace]` is called within the scope of `Span::set_local_parent()`.
///
/// ## Arguments
///
/// * `name` - The name of the span. Defaults to the function name.
/// * `full_path` - Whether to use the full path of the function as the span name. Defaults to `false`.
/// * `enter_on_poll` - Whether to enter the span on poll, if set to `false`, `in_span` will be used.
/// Only available for `async fn`. Defaults to `false`.
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -205,7 +235,7 @@ fn gen_block(
async_keyword: bool,
args: Args,
) -> proc_macro2::TokenStream {
let name = args.name;
let name = gen_name(block.span(), args.name);

// Generate the instrumented function body.
// If the function is an `async fn`, this will wrap it in an async block.
Expand Down Expand Up @@ -246,6 +276,25 @@ fn gen_block(
}
}

fn gen_name(span: proc_macro2::Span, name: Name) -> proc_macro2::TokenStream {
match name {
Name::Function(func_name) => quote_spanned!(span=>
#func_name
),
Name::FullPath => quote_spanned!(span=>
{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let name = type_name_of(f);
let name = &name[..name.len() - 3];
name.trim_end_matches("::{{closure}}")
}
),
}
}

enum AsyncTraitKind<'a> {
// old construction. Contains the function
Function(&'a ItemFn),
Expand Down
6 changes: 6 additions & 0 deletions minitrace-macro/tests/ui/err/has-enter-on-poll-and-sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use minitrace::trace;

#[trace(enter_on_poll = true)]
fn f() {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: `enter_on_poll` can not be applied on non-async function
--> tests/ui/err/has-enter-on-poll-and-sync.rs:3:1
|
3 | #[trace(enter_on_poll = true)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `trace` (in Nightly builds, run with -Z macro-backtrace for more info)
6 changes: 6 additions & 0 deletions minitrace-macro/tests/ui/err/has-name-and-full-path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use minitrace::trace;

#[trace(name = "Name", full_path = true)]
fn f() {}

fn main() {}
7 changes: 7 additions & 0 deletions minitrace-macro/tests/ui/err/has-name-and-full-path.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: `name` and `full_path` can not be used together
--> tests/ui/err/has-name-and-full-path.rs:3:1
|
3 | #[trace(name = "Name", full_path = true)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `trace` (in Nightly builds, run with -Z macro-backtrace for more info)
11 changes: 11 additions & 0 deletions minitrace-macro/tests/ui/ok/has-enter-on-poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use minitrace::trace;

#[trace(enter_on_poll = true)]
async fn f(a: u32) -> u32 {
a
}

#[tokio::main]
async fn main() {
f(1).await;
}
11 changes: 11 additions & 0 deletions minitrace-macro/tests/ui/ok/has-full-path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use minitrace::trace;

#[trace(full_path = true)]
async fn f(a: u32) -> u32 {
a
}

#[tokio::main]
async fn main() {
f(1).await;
}
11 changes: 11 additions & 0 deletions minitrace-macro/tests/ui/ok/has-name-and-full-path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use minitrace::trace;

#[trace(name = "Name", full_path = false)]
async fn f(a: u32) -> u32 {
a
}

#[tokio::main]
async fn main() {
f(1).await;
}
4 changes: 2 additions & 2 deletions minitrace-opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "minitrace-opentelemetry"
version = "0.6.0"
version = "0.6.1"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
description = "Jaeger reporter for minitrace-rust"
description = "Opentelemetry reporter for minitrace-rust"
homepage = "https://github.com/tikv/minitrace-rust"
repository = "https://github.com/tikv/minitrace-rust"
documentation = "https://docs.rs/minitrace-jaeger"
Expand Down
10 changes: 5 additions & 5 deletions minitrace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace"
version = "0.6.0"
version = "0.6.1"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -17,7 +17,7 @@ enable = []

[dependencies]
futures = "0.3"
minitrace-macro = { version = "0.6.0", path = "../minitrace-macro" }
minitrace-macro = { version = "0.6.1", path = "../minitrace-macro" }
minstant = "0.1"
parking_lot = "0.12"
pin-project = "1"
Expand All @@ -37,9 +37,9 @@ futures-timer = "3"
log = "0.4"
logcall = "0.1.4"
minitrace = { path = ".", features = ["enable"] }
minitrace-datadog = { version = "0.6.0", path = "../minitrace-datadog" }
minitrace-jaeger = { version = "0.6.0", path = "../minitrace-jaeger" }
minitrace-opentelemetry = { version = "0.6.0", path = "../minitrace-opentelemetry" }
minitrace-datadog = { version = "0.6.1", path = "../minitrace-datadog" }
minitrace-jaeger = { version = "0.6.1", path = "../minitrace-jaeger" }
minitrace-opentelemetry = { version = "0.6.1", path = "../minitrace-opentelemetry" }
mockall = "0.11"
once_cell = "1"
opentelemetry = { version = "0.20", features = ["trace"] }
Expand Down
3 changes: 0 additions & 3 deletions minitrace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,6 @@
//! [`Future`]: std::future::Future
// Suppress a false-positive lint from clippy
// TODO: remove me once https://github.com/rust-lang/rust-clippy/issues/11076 is released
#![allow(unknown_lints)]
#![allow(clippy::arc_with_non_send_sync)]
#![allow(clippy::needless_doctest_main)]
#![cfg_attr(not(feature = "enable"), allow(dead_code))]
#![cfg_attr(not(feature = "enable"), allow(unused_mut))]
Expand Down
14 changes: 14 additions & 0 deletions minitrace/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ fn macro_example() {
futures_timer::Delay::new(std::time::Duration::from_millis(i)).await;
}

#[trace(full_path = true)]
fn do_something_full_path(i: u64) {
std::thread::sleep(std::time::Duration::from_millis(i));
}

#[trace(full_path = true)]
async fn do_something_async_full_path(i: u64) {
futures_timer::Delay::new(std::time::Duration::from_millis(i)).await;
}

let (reporter, collected_spans) = TestReporter::new();
minitrace::set_reporter(reporter, Config::default());

Expand All @@ -494,6 +504,8 @@ fn macro_example() {
let _g = root.set_local_parent();
do_something(100);
block_on(do_something_async(100));
do_something_full_path(100);
block_on(do_something_async_full_path(100));
}

minitrace::flush();
Expand All @@ -502,6 +514,8 @@ fn macro_example() {
root []
do_something []
do_something_async []
lib::macro_example::{{closure}}::do_something_async_full_path []
lib::macro_example::{{closure}}::do_something_full_path []
"#;
assert_eq!(
tree_str_from_span_records(collected_spans.lock().clone()),
Expand Down

0 comments on commit 4c126f3

Please sign in to comment.