pub struct Registry { /* private fields */ }Expand description
A thread-safe registry for storing metrics by name and labels.
The registry maintains two parallel storage tracks per metric type — an
unlabeled fast path (HashMap<String, Arc<T>>) and a labeled path keyed
by (name, LabelSet). The labeled path is subject to a hard cardinality
cap (default 10 000; see Registry::set_cardinality_cap). Per-metric
description, unit, and kind metadata are stored separately and consumed
by exporters in crate::exporters.
Implementations§
Source§impl Registry
impl Registry
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty registry with the default cardinality cap
(DEFAULT_CARDINALITY_CAP).
Sourcepub fn set_cardinality_cap(&self, cap: usize)
pub fn set_cardinality_cap(&self, cap: usize)
Set the cardinality cap. New labeled registrations beyond this cap
return overflow sinks (or Err(CardinalityExceeded) via the
try_*_with paths).
Sourcepub fn cardinality_cap(&self) -> usize
pub fn cardinality_cap(&self) -> usize
Current cardinality cap.
Sourcepub fn cardinality_count(&self) -> usize
pub fn cardinality_count(&self) -> usize
Count of unique (name, labels) tuples currently registered across
all labeled metric types.
Sourcepub fn cardinality_overflows(&self) -> u64
pub fn cardinality_overflows(&self) -> u64
Total number of overflow events (labeled registrations that hit the cap and were routed to the sink).
Sourcepub fn describe(&self, name: &str, metadata: MetricMetadata)
pub fn describe(&self, name: &str, metadata: MetricMetadata)
Register metadata (help text + unit + kind) for a metric name.
Calling describe again with the same name replaces the prior entry.
Sourcepub fn describe_counter(
&self,
name: &str,
help: impl Into<Cow<'static, str>>,
unit: Unit,
)
pub fn describe_counter( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )
Convenience: describe a counter.
Sourcepub fn describe_gauge(
&self,
name: &str,
help: impl Into<Cow<'static, str>>,
unit: Unit,
)
pub fn describe_gauge( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )
Convenience: describe a gauge.
Sourcepub fn describe_timer(
&self,
name: &str,
help: impl Into<Cow<'static, str>>,
unit: Unit,
)
pub fn describe_timer( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )
Convenience: describe a timer.
Sourcepub fn describe_rate(
&self,
name: &str,
help: impl Into<Cow<'static, str>>,
unit: Unit,
)
pub fn describe_rate( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )
Convenience: describe a rate meter.
Sourcepub fn describe_histogram(
&self,
name: &str,
help: impl Into<Cow<'static, str>>,
unit: Unit,
)
pub fn describe_histogram( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )
Convenience: describe a histogram.
Sourcepub fn metadata(&self, name: &str) -> Option<MetricMetadata>
pub fn metadata(&self, name: &str) -> Option<MetricMetadata>
Look up metadata for a metric by name.
Sourcepub fn get_or_create_counter(&self, name: &str) -> Arc<Counter>
pub fn get_or_create_counter(&self, name: &str) -> Arc<Counter>
Get or create an unlabeled counter.
Requires the count feature.
Sourcepub fn get_or_create_counter_with(
&self,
name: &str,
labels: &LabelSet,
) -> Arc<Counter>
pub fn get_or_create_counter_with( &self, name: &str, labels: &LabelSet, ) -> Arc<Counter>
Get or create a counter for the supplied (name, labels) tuple.
Routes to the per-type cardinality-overflow sink when the cap is full.
Sourcepub fn try_get_or_create_counter_with(
&self,
name: &str,
labels: &LabelSet,
) -> Result<Arc<Counter>>
pub fn try_get_or_create_counter_with( &self, name: &str, labels: &LabelSet, ) -> Result<Arc<Counter>>
Try to get or create a labeled counter. Returns
Err(CardinalityExceeded) when the cap is full.
Sourcepub fn get_or_create_gauge(&self, name: &str) -> Arc<Gauge>
pub fn get_or_create_gauge(&self, name: &str) -> Arc<Gauge>
Get or create an unlabeled gauge. Requires the gauge feature.
Sourcepub fn get_or_create_gauge_with(
&self,
name: &str,
labels: &LabelSet,
) -> Arc<Gauge>
pub fn get_or_create_gauge_with( &self, name: &str, labels: &LabelSet, ) -> Arc<Gauge>
Labeled gauge with overflow-sink fallback. Requires the gauge feature.
Sourcepub fn try_get_or_create_gauge_with(
&self,
name: &str,
labels: &LabelSet,
) -> Result<Arc<Gauge>>
pub fn try_get_or_create_gauge_with( &self, name: &str, labels: &LabelSet, ) -> Result<Arc<Gauge>>
Labeled gauge returning Err(CardinalityExceeded) on overflow.
Requires the gauge feature.
Sourcepub fn get_or_create_timer(&self, name: &str) -> Arc<Timer>
pub fn get_or_create_timer(&self, name: &str) -> Arc<Timer>
Get or create an unlabeled timer. Requires the timer feature.
Sourcepub fn get_or_create_timer_with(
&self,
name: &str,
labels: &LabelSet,
) -> Arc<Timer>
pub fn get_or_create_timer_with( &self, name: &str, labels: &LabelSet, ) -> Arc<Timer>
Labeled timer with overflow-sink fallback. Requires the timer feature.
Sourcepub fn try_get_or_create_timer_with(
&self,
name: &str,
labels: &LabelSet,
) -> Result<Arc<Timer>>
pub fn try_get_or_create_timer_with( &self, name: &str, labels: &LabelSet, ) -> Result<Arc<Timer>>
Labeled timer returning Err(CardinalityExceeded) on overflow.
Requires the timer feature.
Sourcepub fn counter_names(&self) -> Vec<String>
pub fn counter_names(&self) -> Vec<String>
All unlabeled counter names. Requires the count feature.
Sourcepub fn gauge_names(&self) -> Vec<String>
pub fn gauge_names(&self) -> Vec<String>
All unlabeled gauge names. Requires the gauge feature.
Sourcepub fn timer_names(&self) -> Vec<String>
pub fn timer_names(&self) -> Vec<String>
All unlabeled timer names. Requires the timer feature.
Sourcepub fn metric_count(&self) -> usize
pub fn metric_count(&self) -> usize
Total number of registered metrics across all enabled metric types and label combinations.
Sourcepub fn clear(&self)
pub fn clear(&self)
Clear every registered metric, all metadata, and reset cardinality
counters. Previously-returned Arcs remain valid but become detached
from the registry.
Sourcepub fn counter_entries(&self) -> Vec<(String, LabelSet, Arc<Counter>)>
pub fn counter_entries(&self) -> Vec<(String, LabelSet, Arc<Counter>)>
Capture every counter as (name, labels, Arc<Counter>). Requires the
count feature.
Sourcepub fn gauge_entries(&self) -> Vec<(String, LabelSet, Arc<Gauge>)>
pub fn gauge_entries(&self) -> Vec<(String, LabelSet, Arc<Gauge>)>
Capture every gauge as (name, labels, Arc<Gauge>). Requires the
gauge feature.
Sourcepub fn timer_entries(&self) -> Vec<(String, LabelSet, Arc<Timer>)>
pub fn timer_entries(&self) -> Vec<(String, LabelSet, Arc<Timer>)>
Capture every timer as (name, labels, Arc<Timer>). Requires the
timer feature.
Sourcepub fn scoped(&self, prefix: impl Into<String>) -> ScopedRegistry<'_>
pub fn scoped(&self, prefix: impl Into<String>) -> ScopedRegistry<'_>
Build a ScopedRegistry that prepends prefix to every metric
name on registration and lookup.
All scoped lookups land in the same underlying Registry, so a scope
is a pure naming convention — there’s no separate Arc storage. Two
scopes that produce the same effective name (e.g.
scoped("http_").counter("requests") and counter("http_requests"))
return the same Arc<Counter>.
Available since v0.9.5.
§Example
use metrics_lib::{init, metrics};
init();
let http = metrics().registry().scoped("http.");
http.counter("requests").inc(); // registers "http.requests"
http.gauge("inflight").set(1.0); // registers "http.inflight"