Skip to content

Commit

Permalink
default to full name
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 0c1253a commit 7dcd945
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 45 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## v0.6.1

- Add macro attribute `#[trace(full_name = true)]` to use the full path of the function instead of only the function name.
- Macro will use the full path of the function as span name instead of the only function name. You can turn it off by setting `#[trace(short_name = true)]`.

## v0.6.0

Expand Down
34 changes: 18 additions & 16 deletions minitrace-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ struct Args {
}

enum Name {
Function(String),
FullPath,
Plain(String),
FullName,
}

impl Args {
Expand All @@ -36,7 +36,7 @@ impl Args {

let mut args = HashSet::new();
let mut func_name = func_name;
let mut full_name = false;
let mut short_name = false;
let mut enter_on_poll = false;

for arg in &input {
Expand All @@ -53,9 +53,9 @@ impl Args {
path,
lit: Lit::Bool(b),
..
})) if path.is_ident("full_name") => {
full_name = b.value;
args.insert("full_name");
})) if path.is_ident("short_name") => {
short_name = b.value;
args.insert("short_name");
}
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
path,
Expand All @@ -69,13 +69,15 @@ impl Args {
}
}

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

if args.len() != input.len() {
Expand All @@ -99,8 +101,8 @@ impl Args {
///
/// ## Arguments
///
/// * `name` - The name of the span. Defaults to the function name.
/// * `full_name` - Whether to use the full path of the function as the span name. Defaults to `false`.
/// * `name` - The name of the span. Defaults to the full path of the function.
/// * `short_name` - Whether to use the function name without path 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`.
///
Expand Down Expand Up @@ -278,10 +280,10 @@ 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::Plain(name) => quote_spanned!(span=>
#name
),
Name::FullPath => quote_spanned!(span=>
Name::FullName => quote_spanned!(span=>
{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
Expand Down
7 changes: 0 additions & 7 deletions minitrace-macro/tests/ui/err/has-name-and-full-name.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use minitrace::trace;

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

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

#[trace(name = "Name", full_name = false)]
#[trace(name = "Name", short_name = false)]
async fn f(a: u32) -> u32 {
a
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use minitrace::trace;

#[trace(full_name = true)]
#[trace(short_name = true)]
async fn f(a: u32) -> u32 {
a
}
Expand Down
36 changes: 18 additions & 18 deletions minitrace/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ fn test_macro() {
}
}

#[trace(enter_on_poll = true)]
#[trace(short_name = true, enter_on_poll = true)]
async fn work(millis: &u64) {
let _g = Span::enter_with_local_parent("work-inner");
tokio::time::sleep(std::time::Duration::from_millis(*millis))
Expand All @@ -399,7 +399,7 @@ fn test_macro() {
}

impl Bar {
#[trace]
#[trace(short_name = true)]
async fn work2(&self, millis: &u64) {
let _g = Span::enter_with_local_parent("work-inner");
tokio::time::sleep(std::time::Duration::from_millis(*millis))
Expand All @@ -408,7 +408,7 @@ fn test_macro() {
}
}

#[trace]
#[trace(short_name = true)]
async fn work3<'a>(millis1: &'a u64, millis2: &u64) {
let _g = Span::enter_with_local_parent("work-inner");
tokio::time::sleep(std::time::Duration::from_millis(*millis1))
Expand Down Expand Up @@ -476,23 +476,23 @@ root []
#[test]
#[serial]
fn macro_example() {
#[trace]
fn do_something(i: u64) {
#[trace(short_name = true)]
fn do_something_short_name(i: u64) {
std::thread::sleep(std::time::Duration::from_millis(i));
}

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

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

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

Expand All @@ -504,18 +504,18 @@ fn macro_example() {
let _g = root.set_local_parent();
do_something(100);
block_on(do_something_async(100));
do_something_full_name(100);
block_on(do_something_async_full_name(100));
do_something_short_name(100);
block_on(do_something_async_short_name(100));
}

minitrace::flush();

let expected_graph = r#"
root []
do_something []
do_something_async []
lib::macro_example::{{closure}}::do_something_async_full_name []
lib::macro_example::{{closure}}::do_something_full_name []
do_something_async_short_name []
do_something_short_name []
lib::macro_example::{{closure}}::do_something []
lib::macro_example::{{closure}}::do_something_async []
"#;
assert_eq!(
tree_str_from_span_records(collected_spans.lock().clone()),
Expand Down Expand Up @@ -589,7 +589,7 @@ root []
#[test]
#[serial]
fn max_spans_per_trace() {
#[trace]
#[trace(short_name = true)]
fn recursive(n: usize) {
if n > 1 {
recursive(n - 1);
Expand Down

0 comments on commit 7dcd945

Please sign in to comment.