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
// #![doc = include_str!("../README.md")]
//! # Crustrace
//!
//! A procedural macro crate for instrumenting Rust functions with tracing spans.
//!
//! This crate provides macros to automatically add tracing instrumentation to your functions.
use TokenStream;
use TokenStream as TokenStream2;
/// Instruments a function to create and enter a `tracing` span every time
/// the function is called.
///
/// Unless overridden, a span with `info` level will be generated.
/// The generated span's name will be the name of the function.
/// By default, all arguments to the function are included as fields on the span.
///
/// # Examples
///
/// Instrumenting a function:
/// ```
/// # use crustrace::instrument;
/// #[instrument]
/// pub fn my_function(my_arg: usize) {
/// // This creates a span named `my_function` with field `my_arg`
/// println!("inside my_function!");
/// }
/// ```
///
/// Setting the level for the generated span:
/// ```
/// # use crustrace::instrument;
/// #[instrument(level = "debug")]
/// pub fn my_function() {
/// // Creates a DEBUG level span
/// }
/// ```
///
/// Overriding the generated span's name:
/// ```
/// # use crustrace::instrument;
/// #[instrument(name = "my_custom_name")]
/// pub fn my_function() {
/// // Creates a span named `my_custom_name`
/// }
/// ```
/// Instruments all functions within a module or impl block with tracing spans.
///
/// This macro applies the instrumentation behavior to every function found within
/// the annotated module or impl block, automatically creating tracing spans for
/// each function call. This provides a convenient way to add comprehensive tracing
/// to an entire module without having to annotate each function individually.
///
/// The generated spans will use the default configuration (info level, function name
/// as span name, and all function arguments as fields) unless the individual functions
/// are also decorated with `#[instrument]` with custom parameters.
///
/// # Examples
///
/// Instrumenting all functions in a module:
/// ```
/// # use crustrace::omni;
/// #[omni]
/// mod my_module {
/// pub fn function_one(x: i32) {
/// // Automatically gets a span named `function_one` with field `x`
/// println!("Function one called with {}", x);
/// }
///
/// pub fn function_two() {
/// // Automatically gets a span named `function_two`
/// println!("Function two called");
/// }
/// }
/// ```
///
/// Instrumenting all methods in an impl block:
/// ```
/// # use crustrace::omni;
/// struct MyStruct;
///
/// #[omni]
/// impl MyStruct {
/// pub fn method_one(&self, value: String) {
/// // Automatically gets a span named `method_one` with field `value`
/// println!("Method called with {}", value);
/// }
///
/// pub fn method_two(&self) {
/// // Automatically gets a span named `method_two`
/// println!("Another method called");
/// }
/// }
/// ```