Attribute Macro minitrace_macro::trace
source · #[trace]
Expand description
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 tofalse
.enter_on_poll
- Whether to enter the span on poll. If set tofalse
,in_span
will be used. Only available forasync fn
. Defaults tofalse
.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{}
.
§Examples
use minitrace::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:
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
}