1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This crate is derived from [1] under the original license header:
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
// [1]: https://github.com/tikv/minitrace-rust/blob/v0.6.4/minitrace-macro/src/lib.rs
//! An attribute macro designed to eliminate boilerplate code for [`fastrace`].
//!
//! [`fastrace`]: https://crates.io/crates/fastrace
/// An attribute macro designed to eliminate boilerplate code.
///
/// This macro automatically creates a span for the annotated function. The span name defaults to
/// the function name but can be customized by passing a string literal as an argument using the
/// `name` parameter.
///
/// The `#[trace]` attribute requires a local parent context to function correctly. Ensure that
/// the function annotated with `#[trace]` is called within __a local context of a `Span`__, which
/// is established by invoking the `Span::set_local_parent()` method.
///
/// ## Arguments
///
/// * `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`.
/// * `properties` - A list of key-value pairs to be added as properties to the span. The value can
/// be a format string, where the function arguments are accessible. Defaults to `{}`.
/// * `crate` - The path to the fastrace crate. Defaults to `::fastrace`.
///
/// # Examples
///
/// ```
/// use fastrace::prelude::*;
///
/// #[trace]
/// fn simple() {
/// // ...
/// }
///
/// #[trace(short_name = true)]
/// async fn simple_async() {
/// // ...
/// }
///
/// #[trace(name = "qux", enter_on_poll = true)]
/// async fn baz() {
/// // ...
/// }
///
/// #[trace(properties = { "k1": "v1", "a": "argument `a` is {a:?}" })]
/// async fn properties(a: u64) {
/// // ...
/// }
/// ```
///
/// The code snippets above will be expanded to:
///
/// ```
/// # use fastrace::prelude::*;
/// # use fastrace::local::LocalSpan;
/// fn simple() {
/// let __guard__ = LocalSpan::enter_with_local_parent("example::simple");
/// // ...
/// }
///
/// async fn simple_async() {
/// let __span__ = Span::enter_with_local_parent("simple_async");
/// async {
/// // ...
/// }
/// .in_span(__span__)
/// .await
/// }
///
/// async fn baz() {
/// async {
/// // ...
/// }
/// .enter_on_poll("qux")
/// .await
/// }
///
/// async fn properties(a: u64) {
/// let __span__ = Span::enter_with_local_parent("example::properties").with_properties(|| {
/// [
/// (std::borrow::Cow::from("k1"), std::borrow::Cow::from("v1")),
/// (
/// std::borrow::Cow::from("a"),
/// std::borrow::Cow::from(format!("argument `a` is {a:?}")),
/// ),
/// ]
/// });
/// async {
/// // ...
/// }
/// .in_span(__span__)
/// .await
/// }
/// ```