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
// Copyright 2023 TiKV Project Authors. Licensed under Apache-2.0.
use std::borrow::Cow;
use crate::local::local_span_stack::LOCAL_SPAN_STACK;
use crate::Span;
/// An event that represents a single point in time during the execution of a span.
pub struct Event;
impl Event {
/// Adds an event to the parent span with the given name and properties.
///
/// # Examples
///
/// ```
/// use minitrace::prelude::*;
///
/// let root = Span::root("root", SpanContext::random());
///
/// Event::add_to_parent("event in root", &root, || [("key".into(), "value".into())]);
/// ```
pub fn add_to_parent<I, F>(name: impl Into<Cow<'static, str>>, parent: &Span, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
{
#[cfg(feature = "enable")]
{
let mut span = Span::enter_with_parent(name, parent).with_properties(properties);
if let Some(mut inner) = span.inner.take() {
inner.raw_span.is_event = true;
inner.submit_spans();
}
}
}
/// Adds an event to the current local parent span with the given name and properties.
///
/// # Examples
///
/// ```
/// use minitrace::prelude::*;
///
/// let root = Span::root("root", SpanContext::random());
/// let _guard = root.set_local_parent();
///
/// Event::add_to_local_parent("event in root", || [("key".into(), "value".into())]);
/// ```
pub fn add_to_local_parent<I, F>(name: impl Into<Cow<'static, str>>, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
{
#[cfg(feature = "enable")]
{
LOCAL_SPAN_STACK
.try_with(|stack| stack.borrow_mut().add_event(name, properties))
.ok();
}
}
}