Skip to content

Commit f57f8b9

Browse files
authored
macro use full name as default (#171)
* rename path_name to full_name Signed-off-by: Andy Lok <[email protected]> * default to full name Signed-off-by: Andy Lok <[email protected]> * add some useful macros Signed-off-by: Andy Lok <[email protected]> * fix Signed-off-by: Andy Lok <[email protected]> * fix Signed-off-by: Andy Lok <[email protected]> * fix Signed-off-by: Andy Lok <[email protected]> --------- Signed-off-by: Andy Lok <[email protected]>
1 parent baafd33 commit f57f8b9

File tree

10 files changed

+124
-57
lines changed

10 files changed

+124
-57
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
## v0.6.1
66

7-
- Add macro attribute `#[trace(path_name = true)]` to use the full path of the function instead of only the function name.
7+
- 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)]`.
8+
- Add utility macros `func_name!()`, `full_name!()`, and `file_location!()` to generate names for use in span.
89

910
## v0.6.0
1011

minitrace-macro/src/lib.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct Args {
2424
}
2525

2626
enum Name {
27-
Function(String),
28-
FullPath,
27+
Plain(String),
28+
FullName,
2929
}
3030

3131
impl Args {
@@ -36,7 +36,7 @@ impl Args {
3636

3737
let mut args = HashSet::new();
3838
let mut func_name = func_name;
39-
let mut path_name = false;
39+
let mut short_name = false;
4040
let mut enter_on_poll = false;
4141

4242
for arg in &input {
@@ -53,9 +53,9 @@ impl Args {
5353
path,
5454
lit: Lit::Bool(b),
5555
..
56-
})) if path.is_ident("path_name") => {
57-
path_name = b.value;
58-
args.insert("path_name");
56+
})) if path.is_ident("short_name") => {
57+
short_name = b.value;
58+
args.insert("short_name");
5959
}
6060
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
6161
path,
@@ -69,13 +69,15 @@ impl Args {
6969
}
7070
}
7171

72-
let name = if path_name {
73-
if args.contains("name") {
74-
abort_call_site!("`name` and `path_name` can not be used together");
72+
let name = if args.contains("name") {
73+
if short_name {
74+
abort_call_site!("`name` and `short_name` can not be used together");
7575
}
76-
Name::FullPath
76+
Name::Plain(func_name)
77+
} else if short_name {
78+
Name::Plain(func_name)
7779
} else {
78-
Name::Function(func_name)
80+
Name::FullName
7981
};
8082

8183
if args.len() != input.len() {
@@ -99,9 +101,9 @@ impl Args {
99101
///
100102
/// ## Arguments
101103
///
102-
/// * `name` - The name of the span. Defaults to the function name.
103-
/// * `path_name` - Whether to use the full path of the function as the span name. Defaults to `false`.
104-
/// * `enter_on_poll` - Whether to enter the span on poll, if set to `false`, `in_span` will be used.
104+
/// * `name` - The name of the span. Defaults to the full path of the function.
105+
/// * `short_name` - Whether to use the function name without path as the span name. Defaults to `false`.
106+
/// * `enter_on_poll` - Whether to enter the span on poll. If set to `false`, `in_span` will be used.
105107
/// Only available for `async fn`. Defaults to `false`.
106108
///
107109
/// # Examples
@@ -131,15 +133,15 @@ impl Args {
131133
/// # use minitrace::prelude::*;
132134
/// # use minitrace::local::LocalSpan;
133135
/// fn foo() {
134-
/// let __guard__ = LocalSpan::enter_with_local_parent("foo");
136+
/// let __guard__ = LocalSpan::enter_with_local_parent("example::foo");
135137
/// // ...
136138
/// }
137139
///
138140
/// async fn bar() {
139141
/// async {
140142
/// // ...
141143
/// }
142-
/// .in_span(Span::enter_with_local_parent("bar"))
144+
/// .in_span(Span::enter_with_local_parent("example::bar"))
143145
/// .await
144146
/// }
145147
///
@@ -278,19 +280,11 @@ fn gen_block(
278280

279281
fn gen_name(span: proc_macro2::Span, name: Name) -> proc_macro2::TokenStream {
280282
match name {
281-
Name::Function(func_name) => quote_spanned!(span=>
282-
#func_name
283+
Name::Plain(name) => quote_spanned!(span=>
284+
#name
283285
),
284-
Name::FullPath => quote_spanned!(span=>
285-
{
286-
fn f() {}
287-
fn type_name_of<T>(_: T) -> &'static str {
288-
std::any::type_name::<T>()
289-
}
290-
let name = type_name_of(f);
291-
let name = &name[..name.len() - 3];
292-
name.trim_end_matches("::{{closure}}")
293-
}
286+
Name::FullName => quote_spanned!(span=>
287+
minitrace::full_name!()
294288
),
295289
}
296290
}

minitrace-macro/tests/ui/err/has-name-and-path-name.stderr

Lines changed: 0 additions & 7 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use minitrace::trace;
22

3-
#[trace(name = "Name", path_name = true)]
3+
#[trace(name = "Name", short_name = true)]
44
fn f() {}
55

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

minitrace-macro/tests/ui/ok/has-name-and-path-name.rs renamed to minitrace-macro/tests/ui/ok/has-name-and-short-name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use minitrace::trace;
22

3-
#[trace(name = "Name", path_name = false)]
3+
#[trace(name = "Name", short_name = false)]
44
async fn f(a: u32) -> u32 {
55
a
66
}

minitrace-macro/tests/ui/ok/has-path-name.rs renamed to minitrace-macro/tests/ui/ok/has-short-name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use minitrace::trace;
22

3-
#[trace(path_name = true)]
3+
#[trace(short_name = true)]
44
async fn f(a: u32) -> u32 {
55
a
66
}

minitrace/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pub mod collector;
315315
mod event;
316316
pub mod future;
317317
pub mod local;
318+
mod macros;
318319
mod span;
319320
#[doc(hidden)]
320321
pub mod util;

minitrace/src/macros.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
2+
3+
/// Get the name of the function where the macro is invoked. Returns a `&'static str`.
4+
///
5+
/// # Example
6+
///
7+
/// ```
8+
/// use minitrace::func_name;
9+
///
10+
/// fn foo() {
11+
/// assert_eq!(func_name!(), "foo");
12+
/// }
13+
/// # foo()
14+
/// ```
15+
#[macro_export]
16+
macro_rules! func_name {
17+
() => {{
18+
fn f() {}
19+
fn type_name_of<T>(_: T) -> &'static str {
20+
std::any::type_name::<T>()
21+
}
22+
let name = type_name_of(f);
23+
let name = &name[..name.len() - 3];
24+
name.rsplit("::")
25+
.find(|name| *name != "{{closure}}")
26+
.unwrap()
27+
}};
28+
}
29+
30+
/// Get the full path of the function where the macro is invoked. Returns a `&'static str`.
31+
///
32+
/// # Example
33+
///
34+
/// ```
35+
/// use minitrace::full_name;
36+
///
37+
/// fn foo() {
38+
/// assert_eq!(full_name!(), "rust_out::main::_doctest_main_minitrace_src_macros_rs_34_0::foo");
39+
/// }
40+
/// # foo()
41+
#[macro_export]
42+
macro_rules! full_name {
43+
() => {{
44+
fn f() {}
45+
fn type_name_of<T>(_: T) -> &'static str {
46+
std::any::type_name::<T>()
47+
}
48+
let name = type_name_of(f);
49+
let name = &name[..name.len() - 3];
50+
name.trim_end_matches("::{{closure}}")
51+
}};
52+
}
53+
54+
/// Get the source file location where the macro is invoked. Returns a `&'static str`.
55+
///
56+
/// # Example
57+
///
58+
/// ```
59+
/// use minitrace::file_location;
60+
///
61+
/// fn foo() {
62+
/// assert_eq!(file_location!(), "minitrace/src/macros.rs:8:15");
63+
/// }
64+
/// # #[cfg(not(target_os = "windows"))]
65+
/// # foo()
66+
#[macro_export]
67+
macro_rules! file_location {
68+
() => {
69+
std::concat!(file!(), ":", line!(), ":", column!())
70+
};
71+
}

minitrace/tests/lib.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ fn test_macro() {
390390
}
391391
}
392392

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

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

411-
#[trace]
411+
#[trace(short_name = true)]
412412
async fn work3<'a>(millis1: &'a u64, millis2: &u64) {
413413
let _g = Span::enter_with_local_parent("work-inner");
414414
tokio::time::sleep(std::time::Duration::from_millis(*millis1))
@@ -476,23 +476,23 @@ root []
476476
#[test]
477477
#[serial]
478478
fn macro_example() {
479-
#[trace]
480-
fn do_something(i: u64) {
479+
#[trace(short_name = true)]
480+
fn do_something_short_name(i: u64) {
481481
std::thread::sleep(std::time::Duration::from_millis(i));
482482
}
483483

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

489-
#[trace(path_name = true)]
490-
fn do_something_path_name(i: u64) {
489+
#[trace]
490+
fn do_something(i: u64) {
491491
std::thread::sleep(std::time::Duration::from_millis(i));
492492
}
493493

494-
#[trace(path_name = true)]
495-
async fn do_something_async_path_name(i: u64) {
494+
#[trace]
495+
async fn do_something_async(i: u64) {
496496
futures_timer::Delay::new(std::time::Duration::from_millis(i)).await;
497497
}
498498

@@ -504,18 +504,18 @@ fn macro_example() {
504504
let _g = root.set_local_parent();
505505
do_something(100);
506506
block_on(do_something_async(100));
507-
do_something_path_name(100);
508-
block_on(do_something_async_path_name(100));
507+
do_something_short_name(100);
508+
block_on(do_something_async_short_name(100));
509509
}
510510

511511
minitrace::flush();
512512

513513
let expected_graph = r#"
514514
root []
515-
do_something []
516-
do_something_async []
517-
lib::macro_example::{{closure}}::do_something_async_path_name []
518-
lib::macro_example::{{closure}}::do_something_path_name []
515+
do_something_async_short_name []
516+
do_something_short_name []
517+
lib::macro_example::{{closure}}::do_something []
518+
lib::macro_example::{{closure}}::do_something_async []
519519
"#;
520520
assert_eq!(
521521
tree_str_from_span_records(collected_spans.lock().clone()),
@@ -589,7 +589,7 @@ root []
589589
#[test]
590590
#[serial]
591591
fn max_spans_per_trace() {
592-
#[trace]
592+
#[trace(short_name = true)]
593593
fn recursive(n: usize) {
594594
if n > 1 {
595595
recursive(n - 1);

0 commit comments

Comments
 (0)