//! Spans represent periods of time in the execution of a program. usecrate::field::FieldSet; usecrate::parent::Parent; usecrate::stdlib::num::NonZeroU64; usecrate::{field, Metadata};
/// Identifies a span within the context of a subscriber. /// /// They are generated by [`Subscriber`]s for each span as it is created, by /// the [`new_span`] trait method. See the documentation for that method for /// more information on span ID generation. /// /// [`Subscriber`]: super::subscriber::Subscriber /// [`new_span`]: super::subscriber::Subscriber::new_span #[derive(Clone, Debug, PartialEq, Eq, Hash)] pubstruct Id(NonZeroU64);
/// Attributes provided to a `Subscriber` describing a new span when it is /// created. #[derive(Debug)] pubstruct Attributes<'a> {
metadata: &'static Metadata<'static>,
values: &'a field::ValueSet<'a>,
parent: Parent,
}
/// A set of fields recorded by a span. #[derive(Debug)] pubstruct Record<'a> {
values: &'a field::ValueSet<'a>,
}
/// Indicates what [the `Subscriber` considers] the "current" span. /// /// As subscribers may not track a notion of a current span, this has three /// possible states: /// - "unknown", indicating that the subscriber does not track a current span, /// - "none", indicating that the current context is known to not be in a span, /// - "some", with the current span's [`Id`] and [`Metadata`]. /// /// [the `Subscriber` considers]: super::subscriber::Subscriber::current_span /// [`Metadata`]: super::metadata::Metadata #[derive(Debug)] pubstruct Current {
inner: CurrentInner,
}
impl Id { /// Constructs a new span ID from the given `u64`. /// /// <pre class="ignore" style="white-space:normal;font:inherit;"> /// <strong>Note</strong>: Span IDs must be greater than zero. /// </pre> /// /// # Panics /// - If the provided `u64` is 0. pubfn from_u64(u: u64) -> Self {
Id(NonZeroU64::new(u).expect("span IDs must be > 0"))
}
/// Constructs a new span ID from the given `NonZeroU64`. /// /// Unlike [`Id::from_u64`](Id::from_u64()), this will never panic. #[inline] pubconstfn from_non_zero_u64(id: NonZeroU64) -> Self {
Id(id)
}
// Allow `into` by-ref since we don't want to impl Copy for Id #[allow(clippy::wrong_self_convention)] /// Returns the span's ID as a `u64`. pubfn into_u64(&self) -> u64 { self.0.get()
}
// Allow `into` by-ref since we don't want to impl Copy for Id #[allow(clippy::wrong_self_convention)] /// Returns the span's ID as a `NonZeroU64`. #[inline] pubconstfn into_non_zero_u64(&self) -> NonZeroU64 { self.0
}
}
impl<'a> Attributes<'a> { /// Returns `Attributes` describing a new child span of the current span, /// with the provided metadata and values. pubfn new(metadata: &'static Metadata<'static>, values: &le='color:blue'>'a field::ValueSet<'a>) -> Self {
Attributes {
metadata,
values,
parent: Parent::Current,
}
}
/// Returns `Attributes` describing a new span at the root of its own trace /// tree, with the provided metadata and values. pubfn new_root(metadata: &'static Metadata<'static>, values: &n style='color:blue'>'a field::ValueSet<'a>) -> Self {
Attributes {
metadata,
values,
parent: Parent::Root,
}
}
/// Returns `Attributes` describing a new child span of the specified /// parent span, with the provided metadata and values. pubfn child_of(
parent: Id,
metadata: &'static Metadata<'static>,
values: &'a field::ValueSet<'a>,
) -> Self {
Attributes {
metadata,
values,
parent: Parent::Explicit(parent),
}
}
/// Returns a reference to the new span's metadata. pubfn metadata(&self) -> &'static Metadata<'static> { self.metadata
}
/// Returns a reference to a `ValueSet` containing any values the new span /// was created with. pubfn values(&self) -> &field::ValueSet<'a> { self.values
}
/// Returns true if the new span should be a root. pubfn is_root(&self) -> bool {
matches!(self.parent, Parent::Root)
}
/// Returns true if the new span's parent should be determined based on the /// current context. /// /// If this is true and the current thread is currently inside a span, then /// that span should be the new span's parent. Otherwise, if the current /// thread is _not_ inside a span, then the new span will be the root of its /// own trace tree. pubfn is_contextual(&self) -> bool {
matches!(self.parent, Parent::Current)
}
/// Returns the new span's explicitly-specified parent, if there is one. /// /// Otherwise (if the new span is a root or is a child of the current span), /// returns `None`. pubfn parent(&self) -> Option<&Id> { matchself.parent {
Parent::Explicit(ref p) => Some(p),
_ => None,
}
}
/// Records all the fields in this set of `Attributes` with the provided /// [Visitor]. /// /// [visitor]: super::field::Visit pubfn record(&self, visitor: &mutdyn field::Visit) { self.values.record(visitor)
}
/// Returns `true` if this set of `Attributes` contains a value for the /// given `Field`. pubfn contains(&self, field: &field::Field) -> bool { self.values.contains(field)
}
/// Returns true if this set of `Attributes` contains _no_ values. pubfn is_empty(&self) -> bool { self.values.is_empty()
}
/// Returns the set of all [fields] defined by this span's [`Metadata`]. /// /// Note that the [`FieldSet`] returned by this method includes *all* the /// fields declared by this span, not just those with values that are recorded /// as part of this set of `Attributes`. Other fields with values not present in /// this `Attributes`' value set may [record] values later. /// /// [fields]: crate::field /// [record]: Attributes::record() /// [`Metadata`]: crate::metadata::Metadata /// [`FieldSet`]: crate::field::FieldSet pubfn fields(&self) -> &FieldSet { self.values.field_set()
}
}
// ===== impl Record =====
impl<'a> Record<'a> { /// Constructs a new `Record` from a `ValueSet`. pubfn new(values: &'a field::ValueSet<'a>) -> Self { Self { values }
}
/// Records all the fields in this `Record` with the provided [Visitor]. /// /// [visitor]: super::field::Visit pubfn record(&self, visitor: &mutdyn field::Visit) { self.values.record(visitor)
}
/// Returns the number of fields that would be visited from this `Record` /// when [`Record::record()`] is called /// /// [`Record::record()`]: Record::record() pubfn len(&self) -> usize { self.values.len()
}
/// Returns `true` if this `Record` contains a value for the given `Field`. pubfn contains(&self, field: &field::Field) -> bool { self.values.contains(field)
}
/// Returns true if this `Record` contains _no_ values. pubfn is_empty(&self) -> bool { self.values.is_empty()
}
}
// ===== impl Current =====
impl Current { /// Constructs a new `Current` that indicates the current context is a span /// with the given `metadata` and `metadata`. pubfn new(id: Id, metadata: &'static Metadata<'static>) -> Self { Self {
inner: CurrentInner::Current { id, metadata },
}
}
/// Constructs a new `Current` that indicates the current context is *not* /// in a span. pubfn none() -> Self { Self {
inner: CurrentInner::None,
}
}
/// Constructs a new `Current` that indicates the `Subscriber` does not /// track a current span. pub(crate) fn unknown() -> Self { Self {
inner: CurrentInner::Unknown,
}
}
/// Returns `true` if the `Subscriber` that constructed this `Current` tracks a /// current span. /// /// If this returns `true` and [`id`], [`metadata`], or [`into_inner`] /// return `None`, that indicates that we are currently known to *not* be /// inside a span. If this returns `false`, those methods will also return /// `None`, but in this case, that is because the subscriber does not keep /// track of the currently-entered span. /// /// [`id`]: Current::id() /// [`metadata`]: Current::metadata() /// [`into_inner`]: Current::into_inner() pubfn is_known(&self) -> bool {
!matches!(self.inner, CurrentInner::Unknown)
}
/// Consumes `self` and returns the span `Id` and `Metadata` of the current /// span, if one exists and is known. pubfn into_inner(self) -> Option<(Id, &'static Metadata<'static>)> { matchself.inner {
CurrentInner::Current { id, metadata } => Some((id, metadata)),
_ => None,
}
}
/// Borrows the `Id` of the current span, if one exists and is known. pubfn id(&self) -> Option<&Id> { matchself.inner {
CurrentInner::Current { ref id, .. } => Some(id),
_ => None,
}
}
/// Borrows the `Metadata` of the current span, if one exists and is known. pubfn metadata(&self) -> Option<&'static Metadata<'static>> { matchself.inner {
CurrentInner::Current { metadata, .. } => Some(metadata),
_ => None,
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.