|
16 | 16 | //! |
17 | 17 | //! ```toml |
18 | 18 | //! [dependencies] |
19 | | -//! minitrace = "0.5" |
| 19 | +//! minitrace = "0.6" |
20 | 20 | //! ``` |
21 | 21 | //! |
22 | 22 | //! Libraries can attach their spans to the caller's span (if caller has set [local parent](#local-span)) |
|
63 | 63 | //! |
64 | 64 | //! ```toml |
65 | 65 | //! [dependencies] |
66 | | -//! minitrace = { version = "0.5", features = ["enable"] } |
| 66 | +//! minitrace = { version = "0.6", features = ["enable"] } |
67 | 67 | //! ``` |
68 | 68 | //! |
69 | 69 | //! Executables should initialize a reporter implementation early in the program's runtime. |
|
102 | 102 | //! parent span id from a remote source. If there's no remote parent span, the parent span |
103 | 103 | //! id is typically set to its default value of zero. |
104 | 104 | //! |
105 | | -//! [`Span::enter_with_parent()`] starts a child span given a parent span. |
| 105 | +//! Once we have the root `Span`, we can create a child `Span` using [`Span::enter_with_parent()`], |
| 106 | +//! thereby establishing the reference relationship between the spans. |
106 | 107 | //! |
107 | 108 | //! `Span` is thread-safe and can be sent across threads. |
108 | 109 | //! ``` |
|
129 | 130 | //! minitrace::flush(); |
130 | 131 | //! ``` |
131 | 132 | //! |
| 133 | +//! Sometimes, passing a `Span` through a function to create a child `Span` can be inconvenient. |
| 134 | +//! We can employ a thread-local approach to avoid an explicit argument passing in the function. |
| 135 | +//! In minitrace, [`Span::set_local_parent()`] and [`Span::enter_with_local_parent()`] serve this purpose. |
| 136 | +//! |
| 137 | +//! [`Span::set_local_parent()`] method sets __a local context of the `Span`__ for the current thread. |
| 138 | +//! [`Span::enter_with_local_parent()`] accesses the parent `Span` from the local context and creates |
| 139 | +//! a child `Span` with it. |
| 140 | +//! |
| 141 | +//! ``` |
| 142 | +//! use minitrace::prelude::*; |
| 143 | +//! |
| 144 | +//! { |
| 145 | +//! let root_span = Span::root("root", SpanContext::random()); |
| 146 | +//! let _guard = root_span.set_local_parent(); |
| 147 | +//! |
| 148 | +//! foo(); |
| 149 | +//! |
| 150 | +//! // root_span ends here. |
| 151 | +//! } |
| 152 | +//! |
| 153 | +//! fn foo() { |
| 154 | +//! // The parent of this span is `root`. |
| 155 | +//! let _child_span = Span::enter_with_local_parent("a child span"); |
| 156 | +//! |
| 157 | +//! // ... |
| 158 | +//! |
| 159 | +//! // _child_span ends here. |
| 160 | +//! } |
| 161 | +//! ``` |
| 162 | +//! |
132 | 163 | //! ## Local Span |
133 | 164 | //! |
134 | | -//! A `Span` can be efficiently replaced with a [`LocalSpan`], reducing overhead |
135 | | -//! significantly, provided it is not intended for sending to other threads. |
| 165 | +//! In a clear single-thread execution flow, where we can ensure that the `Span` does |
| 166 | +//! not cross threads, meaning: |
| 167 | +//! - The `Span` is not sent to or shared by other threads |
| 168 | +//! - In asynchronous code, the lifetime of the `Span` doesn't cross an `.await` point |
| 169 | +//! |
| 170 | +//! we can use `LocalSpan` as a substitute for `Span` to effectively reduce overhead |
| 171 | +//! and greatly enhance performance. |
| 172 | +//! |
| 173 | +//! However, there is a precondition: The creation of `LocalSpan` must take place |
| 174 | +//! within __a local context of a `Span`__, which is established by invoking the |
| 175 | +//! [`Span::set_local_parent()`] method. |
136 | 176 | //! |
137 | | -//! Before starting a `LocalSpan`, a scope of parent span should be set using |
138 | | -//! [`Span::set_local_parent()`]. Use [`LocalSpan::enter_with_local_parent()`] to start |
139 | | -//! a `LocalSpan`, which then becomes the new local parent. |
| 177 | +//! If the code spans multiple function calls, this isn't always straightforward to |
| 178 | +//! confirm if the precondition is met. As such, it's recommended to invoke |
| 179 | +//! [`Span::set_local_parent()`] immediately after the creation of `Span`. |
140 | 180 | //! |
141 | | -//! If no local parent is set, the `enter_with_local_parent()` will do nothing. |
| 181 | +//! After __a local context of a `Span`__ is set using [`Span::set_local_parent()`], |
| 182 | +//! use [`LocalSpan::enter_with_local_parent()`] to start a `LocalSpan`, which then |
| 183 | +//! becomes the new local parent. |
| 184 | +//! |
| 185 | +//! If no local context is set, the [`LocalSpan::enter_with_local_parent()`] will do nothing. |
142 | 186 | //! ``` |
143 | 187 | //! use minitrace::collector::Config; |
144 | 188 | //! use minitrace::collector::ConsoleReporter; |
|
148 | 192 | //! |
149 | 193 | //! { |
150 | 194 | //! let root = Span::root("root", SpanContext::random()); |
| 195 | +//! let _guard = root.set_local_parent(); |
151 | 196 | //! |
152 | 197 | //! { |
153 | | -//! let _guard = root.set_local_parent(); |
154 | | -//! |
155 | 198 | //! // The parent of this span is `root`. |
156 | 199 | //! let _span1 = LocalSpan::enter_with_local_parent("a child span"); |
157 | 200 | //! |
|
181 | 224 | //! |
182 | 225 | //! { |
183 | 226 | //! let root = Span::root("root", SpanContext::random()); |
| 227 | +//! let _guard = root.set_local_parent(); |
184 | 228 | //! |
185 | 229 | //! Event::add_to_parent("event in root", &root, || []); |
186 | | -//! |
187 | 230 | //! { |
188 | | -//! let _guard = root.set_local_parent(); |
189 | 231 | //! let _span1 = LocalSpan::enter_with_local_parent("a child span"); |
190 | 232 | //! |
191 | 233 | //! Event::add_to_local_parent("event in span1", || [("key".into(), "value".into())]); |
|
197 | 239 | //! |
198 | 240 | //! ## Macro |
199 | 241 | //! |
200 | | -//! The attribute-macro [`trace`] helps to reduce boilerplate. However, the function annotated |
201 | | -//! by the `trace` always requires a local parent in the context, otherwise, no span will be |
202 | | -//! recorded. |
| 242 | +//! The attribute-macro [`trace`] helps to reduce boilerplate. |
| 243 | +//! |
| 244 | +//! Note: For successful tracing a function using the [`trace`] macro, the function call should occur |
| 245 | +//! within __a local context of a `Span`__. |
| 246 | +//! |
| 247 | +//! For more detailed usage instructions, please refer to [`trace`]. |
203 | 248 | //! |
204 | 249 | //! ``` |
205 | 250 | //! use futures::executor::block_on; |
|
0 commit comments