pub struct LabelSet { /* private fields */ }Expand description
An ordered, deduplicated set of label key/value pairs.
LabelSet maintains its inner Vec<Label> sorted by key for stable
hashing and rendering: two semantically equivalent sets (same pairs in any
insertion order) compare equal, hash equal, and render identically.
Construction is allocation-free for an empty set (LabelSet::EMPTY) and
allocates the backing Vec on first insertion.
Implementations§
Source§impl LabelSet
impl LabelSet
Sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &str)> + '_
pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> + '_
Iterate label pairs as (&str, &str) in sorted order.
Sourcepub fn add(
&mut self,
key: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>,
) -> &mut Self
pub fn add( &mut self, key: impl Into<Cow<'static, str>>, value: impl Into<Cow<'static, str>>, ) -> &mut Self
Insert a (key, value) pair, replacing any existing value for key.
Maintains sort order via binary_search_by. Returns &mut self for
chaining.
Sourcepub fn with(
self,
key: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>,
) -> Self
pub fn with( self, key: impl Into<Cow<'static, str>>, value: impl Into<Cow<'static, str>>, ) -> Self
Builder-style insert: consumes self and returns it with (key, value) added or replaced.
Sourcepub fn remove(&mut self, key: &str) -> bool
pub fn remove(&mut self, key: &str) -> bool
Remove a label by key. Returns true if a pair was removed.
Sourcepub fn to_prometheus(&self) -> String
pub fn to_prometheus(&self) -> String
Render labels in Prometheus / OpenMetrics format: {k="v",k="v"}.
Returns an empty string for an empty set. Label values are escaped per
the Prometheus exposition format: \ → \\, " → \", \n → \n.
Label keys are emitted verbatim (Prometheus requires [a-zA-Z_][a-zA-Z0-9_]*);
callers are responsible for choosing valid keys.