Skip to content

Commit 22dbbf2

Browse files
zhongzcandylokandy
andauthored
docs: update Key Concepts part (#174)
* docs: update Key Concepts part * update * update * update * update * dco Signed-off-by: Zhenchi <[email protected]> * fix: fmt Signed-off-by: Zhenchi <[email protected]> * fix: doc test Signed-off-by: Zhenchi <[email protected]> * Update minitrace/src/lib.rs Co-authored-by: Andy Lok <[email protected]> * Update minitrace/src/lib.rs Co-authored-by: Andy Lok <[email protected]> * address comment Signed-off-by: Zhenchi <[email protected]> --------- Signed-off-by: Zhenchi <[email protected]> Co-authored-by: Andy Lok <[email protected]>
1 parent a97bad8 commit 22dbbf2

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

minitrace-macro/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ impl Args {
9797
/// name but can be customized by passing a string literal as an argument using the `name` parameter.
9898
///
9999
/// The `#[trace]` attribute requires a local parent context to function correctly. Ensure that
100-
/// the function annotated with `#[trace]` is called within the scope of `Span::set_local_parent()`.
100+
/// the function annotated with `#[trace]` is called within __a local context of a `Span`__, which is
101+
/// established by invoking the `Span::set_local_parent()` method.
101102
///
102103
/// ## Arguments
103104
///

minitrace/src/lib.rs

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//!
1717
//! ```toml
1818
//! [dependencies]
19-
//! minitrace = "0.5"
19+
//! minitrace = "0.6"
2020
//! ```
2121
//!
2222
//! Libraries can attach their spans to the caller's span (if caller has set [local parent](#local-span))
@@ -63,7 +63,7 @@
6363
//!
6464
//! ```toml
6565
//! [dependencies]
66-
//! minitrace = { version = "0.5", features = ["enable"] }
66+
//! minitrace = { version = "0.6", features = ["enable"] }
6767
//! ```
6868
//!
6969
//! Executables should initialize a reporter implementation early in the program's runtime.
@@ -102,7 +102,8 @@
102102
//! parent span id from a remote source. If there's no remote parent span, the parent span
103103
//! id is typically set to its default value of zero.
104104
//!
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.
106107
//!
107108
//! `Span` is thread-safe and can be sent across threads.
108109
//! ```
@@ -129,16 +130,59 @@
129130
//! minitrace::flush();
130131
//! ```
131132
//!
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+
//!
132163
//! ## Local Span
133164
//!
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.
136176
//!
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`.
140180
//!
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.
142186
//! ```
143187
//! use minitrace::collector::Config;
144188
//! use minitrace::collector::ConsoleReporter;
@@ -148,10 +192,9 @@
148192
//!
149193
//! {
150194
//! let root = Span::root("root", SpanContext::random());
195+
//! let _guard = root.set_local_parent();
151196
//!
152197
//! {
153-
//! let _guard = root.set_local_parent();
154-
//!
155198
//! // The parent of this span is `root`.
156199
//! let _span1 = LocalSpan::enter_with_local_parent("a child span");
157200
//!
@@ -181,11 +224,10 @@
181224
//!
182225
//! {
183226
//! let root = Span::root("root", SpanContext::random());
227+
//! let _guard = root.set_local_parent();
184228
//!
185229
//! Event::add_to_parent("event in root", &root, || []);
186-
//!
187230
//! {
188-
//! let _guard = root.set_local_parent();
189231
//! let _span1 = LocalSpan::enter_with_local_parent("a child span");
190232
//!
191233
//! Event::add_to_local_parent("event in span1", || [("key".into(), "value".into())]);
@@ -197,9 +239,12 @@
197239
//!
198240
//! ## Macro
199241
//!
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`].
203248
//!
204249
//! ```
205250
//! use futures::executor::block_on;

0 commit comments

Comments
 (0)