Skip to content

Commit 988e53d

Browse files
feat: add convenient method add_event to Span (#62)
Co-authored-by: Andy Lok <andylokandy@hotmail.com>
1 parent 300a590 commit 988e53d

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

fastrace/src/span.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::time::Duration;
88

99
use fastant::Instant;
1010

11+
use crate::Event;
1112
use crate::collector::CollectTokenItem;
1213
use crate::collector::GlobalCollect;
1314
use crate::collector::SpanContext;
@@ -197,6 +198,28 @@ impl Span {
197198
}
198199
}
199200

201+
/// Adds an event to the span with the given name and properties.
202+
///
203+
/// # Examples
204+
///
205+
/// ```
206+
/// use fastrace::prelude::*;
207+
///
208+
/// let root = Span::root("root", SpanContext::random());
209+
///
210+
/// root.add_event("event in root", || [("key".into(), "value".into())])
211+
/// .add_event("event2 in root", || [("key2".into(), "value2".into())]);
212+
/// ```
213+
#[inline]
214+
pub fn add_event<I, F>(&self, name: impl Into<Cow<'static, str>>, properties: F) -> &Self
215+
where
216+
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
217+
F: FnOnce() -> I,
218+
{
219+
Event::add_to_parent(name, self, properties);
220+
self
221+
}
222+
200223
/// Sets the current `Span` as the local parent for the current thread.
201224
///
202225
/// This method is used to establish a `Span` as the local parent within the current scope.

0 commit comments

Comments
 (0)