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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
//! A high-performance, ergonomic timeline tracing library for Rust.
//!
//! ## Span
//!
//! A [`SpanRecord`] represents an individual unit of work. It contains:
//! - An operation name
//! - A start timestamp and duration
//! - A set of key-value properties
//! - A reference to a parent `Span`
//!
//! To record such a span record, we create a [`Span`] to start clocking and drop it to stop recording.
//!
//! A new `Span` can be started via [`Span::root()`] and [`Span::enter_with_parent()`]. `Span::enter_with_parent()`
//! will start a child span to a given parent span.
//!
//! `Span` is thread-safe and can be sent across threads.
//!
//! ```
//! use minitrace::prelude::*;
//! use futures::executor::block_on;
//!
//! let (root, collector) = Span::root("root");
//!
//! {
//! let _child_span = Span::enter_with_parent("a child span", &root);
//! // some work
//! }
//!
//! drop(root);
//! let records: Vec<SpanRecord> = block_on(collector.collect());
//!
//! println!("{:#?}", records);
//! // [
//! // SpanRecord {
//! // id: 1,
//! // parent_id: 0,
//! // begin_unix_time_ns: 1642166520139678013,
//! // duration_ns: 16008,
//! // event: "root",
//! // properties: [],
//! // },
//! // SpanRecord {
//! // id: 2,
//! // parent_id: 1,
//! // begin_unix_time_ns: 1642166520139692070,
//! // duration_ns: 634,
//! // event: "a child span",
//! // properties: [],
//! // },
//! // ]
//! ```
//!
//!
//! ## Local Span
//!
//! A `Span` can be optimized into [`LocalSpan`], if the span is not supposed to be sent to other threads,
//! which can greatly reduce the overhead.
//!
//! Before starting a `LocalSpan`, a scope of parent span should be set using [`Span::set_local_parent()`].
//! Use [`LocalSpan::enter_with_local_parent()`] to start a `LocalSpan`, and then, it will become the new local parent.
//!
//! If no local parent is set, the `enter_with_local_parent()` will do nothing.
//!
//! ```
//! use minitrace::prelude::*;
//!
//! let (root, collector) = Span::root("root");
//!
//! {
//! let _guard = root.set_local_parent();
//!
//! // The parent of this span is `root`.
//! let _span1 = LocalSpan::enter_with_local_parent("a child span");
//!
//! foo();
//! }
//!
//! fn foo() {
//! // The parent of this span is `span1`.
//! let _span2 = LocalSpan::enter_with_local_parent("a child span of child span");
//! }
//! ```
//!
//!
//! ## Property
//!
//! Property is an arbitrary custom kev-value pair associated to a span.
//!
//! ```
//! use minitrace::prelude::*;
//! use futures::executor::block_on;
//!
//! let (mut root, collector) = Span::root("root");
//! root.with_property(|| ("key", "value".to_owned()));
//!
//! {
//! let _guard = root.set_local_parent();
//!
//! let _span1 = LocalSpan::enter_with_local_parent("a child span")
//! .with_property(|| ("key", "value".to_owned()));
//! }
//!
//! drop(root);
//! let records: Vec<SpanRecord> = block_on(collector.collect());
//!
//! println!("{:#?}", records);
//! // [
//! // SpanRecord {
//! // id: 1,
//! // parent_id: 0,
//! // begin_unix_time_ns: 1642166791041022255,
//! // duration_ns: 121705,
//! // event: "root",
//! // properties: [
//! // (
//! // "key",
//! // "value",
//! // ),
//! // ],
//! // },
//! // SpanRecord {
//! // id: 2,
//! // parent_id: 1,
//! // begin_unix_time_ns: 1642166791041132550,
//! // duration_ns: 7724,
//! // event: "a child span",
//! // properties: [
//! // (
//! // "key",
//! // "value",
//! // ),
//! // ],
//! // },
//! // ]
//! ```
//!
//! ## Macro
//!
//! An attribute-macro [`trace`] can help get rid of boilerplate. The macro always requires a local
//! parent in the context, otherwise, no span will be recorded.
//!
//! ```
//! use minitrace::prelude::*;
//! use futures::executor::block_on;
//!
//! #[trace("do_something")]
//! fn do_something(i: u64) {
//! std::thread::sleep(std::time::Duration::from_millis(i));
//! }
//!
//! #[trace("do_something_async")]
//! async fn do_something_async(i: u64) {
//! futures_timer::Delay::new(std::time::Duration::from_millis(i)).await;
//! }
//!
//! let (root, collector) = Span::root("root");
//!
//! {
//! let _g = root.set_local_parent();
//! do_something(100);
//! block_on(do_something_async(100));
//! }
//!
//! drop(root);
//! let records: Vec<SpanRecord> = block_on(collector.collect());
//!
//! println!("{:#?}", records);
//! // [
//! // SpanRecord {
//! // id: 1,
//! // parent_id: 0,
//! // begin_unix_time_ns: 1642167988459480418,
//! // duration_ns: 200741472,
//! // event: "root",
//! // properties: [],
//! // },
//! // SpanRecord {
//! // id: 2,
//! // parent_id: 1,
//! // begin_unix_time_ns: 1642167988459571971,
//! // duration_ns: 100084126,
//! // event: "do_something",
//! // properties: [],
//! // },
//! // SpanRecord {
//! // id: 3,
//! // parent_id: 1,
//! // begin_unix_time_ns: 1642167988559887219,
//! // duration_ns: 100306947,
//! // event: "do_something_async",
//! // properties: [],
//! // },
//! // ]
//! ```
//!
//! [`Span`]: crate::Span
//! [`LocalSpan`]: crate::local::LocalSpan
//! [`SpanRecord`]: crate::collector::SpanRecord
//! [`FutureExt`]: crate::future::FutureExt
//! [`trace`]: crate::trace
//! [`LocalCollector`]: crate::local::LocalCollector
//! [`Span::root()`]: crate::Span::root
//! [`Span::enter_with_parent()`]: crate::Span::enter_with_parent
//! [`Span::set_local_parent()`]: crate::Span::set_local_parent
//! [`LocalSpan::enter_with_local_parent()`]: crate::local::LocalSpan::enter_with_local_parent
pub mod collector;
pub mod future;
pub mod local;
mod span;
#[doc(hidden)]
pub mod util;
pub use crate::span::Span;
/// An attribute-macro to help get rid of boilerplate.
///
/// [`trace`] always require an local parent in the context. Make sure that the caller
/// is within the scope of [`Span::set_local_parent()`].
///
/// # Examples
///
/// ```
/// use minitrace::prelude::*;
///
/// #[trace("foo")]
/// fn foo() {
/// // some work
/// }
///
/// #[trace("bar")]
/// async fn bar() {
/// // some work
/// }
///
/// #[trace("qux", enter_on_poll = true)]
/// async fn qux() {
/// // some work
/// }
/// ```
///
/// The examples above will be translated into:
///
/// ```
/// # use minitrace::prelude::*;
/// # use minitrace::local::LocalSpan;
/// fn foo() {
/// let __guard = LocalSpan::enter_with_local_parent("foo");
/// // some work
/// }
///
/// fn bar() -> impl core::future::Future<Output = ()> {
/// async {
/// // some work
/// }
/// .in_span(Span::enter_with_local_parent("bar"))
/// }
///
/// fn qux() -> impl core::future::Future<Output = ()> {
/// async {
/// // some work
/// }
/// .enter_on_poll("qux")
/// }
/// ```
///
/// [`in_span()`]: crate::future::FutureExt::in_span
pub use minitrace_macro::trace;
pub mod prelude {
//! A "prelude" for crates using the `minitrace` crate.
#[doc(no_inline)]
pub use crate::collector::{CollectArgs, Collector, SpanRecord};
#[doc(no_inline)]
pub use crate::future::FutureExt as _;
#[doc(no_inline)]
pub use crate::local::LocalSpan;
#[doc(no_inline)]
pub use crate::span::Span;
#[doc(no_inline)]
pub use crate::trace;
}
/// Test README
#[cfg(doctest)]
mod test_readme {
macro_rules! external_doc_test {
($x:expr) => {
#[doc = $x]
extern "C" {}
};
}
external_doc_test!(include_str!("../README.md"));
}