From 1c6c4815cb09f828f0006fb8963d4966ed40ad5a Mon Sep 17 00:00:00 2001 From: Folyd Date: Sat, 27 Mar 2021 12:10:16 +0800 Subject: [PATCH 1/2] core: add `len()` method to `Attributes` --- tracing-core/src/span.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tracing-core/src/span.rs b/tracing-core/src/span.rs index 1cb228d38d..7deb7c12df 100644 --- a/tracing-core/src/span.rs +++ b/tracing-core/src/span.rs @@ -195,6 +195,12 @@ impl<'a> Attributes<'a> { pub fn is_empty(&self) -> bool { self.values.is_empty() } + + /// Returns the number of fields in this `Attributes`. + #[inline] + pub fn len(&self) -> usize { + self.values.field_set().len() + } } // ===== impl Record ===== From b2ab30b4352e30fc9701e06966bc50e176139ed5 Mon Sep 17 00:00:00 2001 From: Folyd Date: Wed, 31 Mar 2021 10:52:09 +0800 Subject: [PATCH 2/2] Expose an accessor for the `FieldSet` on `Attributes` --- tracing-core/src/span.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tracing-core/src/span.rs b/tracing-core/src/span.rs index 7074a8506c..6cc97da9ab 100644 --- a/tracing-core/src/span.rs +++ b/tracing-core/src/span.rs @@ -1,7 +1,9 @@ //! Spans represent periods of time in the execution of a program. +use core::num::NonZeroU64; + +use crate::field::FieldSet; use crate::parent::Parent; use crate::{field, Metadata}; -use core::num::NonZeroU64; /// Identifies a span within the context of a collector. /// @@ -196,10 +198,19 @@ impl<'a> Attributes<'a> { self.values.is_empty() } - /// Returns the number of fields in this `Attributes`. - #[inline] - pub fn len(&self) -> usize { - self.values.field_set().len() + /// 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 + pub fn fields(&self) -> &FieldSet { + self.values.field_set() } }