Skip to main content

Registry

Struct Registry 

Source
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

Source

pub fn new() -> Self

Create a new empty registry with the default cardinality cap (DEFAULT_CARDINALITY_CAP).

Source

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).

Source

pub fn cardinality_cap(&self) -> usize

Current cardinality cap.

Source

pub fn cardinality_count(&self) -> usize

Count of unique (name, labels) tuples currently registered across all labeled metric types.

Source

pub fn cardinality_overflows(&self) -> u64

Total number of overflow events (labeled registrations that hit the cap and were routed to the sink).

Source

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.

Source

pub fn describe_counter( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )

Convenience: describe a counter.

Source

pub fn describe_gauge( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )

Convenience: describe a gauge.

Source

pub fn describe_timer( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )

Convenience: describe a timer.

Source

pub fn describe_rate( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )

Convenience: describe a rate meter.

Source

pub fn describe_histogram( &self, name: &str, help: impl Into<Cow<'static, str>>, unit: Unit, )

Convenience: describe a histogram.

Source

pub fn metadata(&self, name: &str) -> Option<MetricMetadata>

Look up metadata for a metric by name.

Source

pub fn get_or_create_counter(&self, name: &str) -> Arc<Counter>

Get or create an unlabeled counter.

Requires the count feature.

Source

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.

Source

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.

Source

pub fn get_or_create_gauge(&self, name: &str) -> Arc<Gauge>

Get or create an unlabeled gauge. Requires the gauge feature.

Source

pub fn get_or_create_gauge_with( &self, name: &str, labels: &LabelSet, ) -> Arc<Gauge>

Labeled gauge with overflow-sink fallback. Requires the gauge feature.

Source

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.

Source

pub fn get_or_create_timer(&self, name: &str) -> Arc<Timer>

Get or create an unlabeled timer. Requires the timer feature.

Source

pub fn get_or_create_timer_with( &self, name: &str, labels: &LabelSet, ) -> Arc<Timer>

Labeled timer with overflow-sink fallback. Requires the timer feature.

Source

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.

Source

pub fn counter_names(&self) -> Vec<String>

All unlabeled counter names. Requires the count feature.

Source

pub fn gauge_names(&self) -> Vec<String>

All unlabeled gauge names. Requires the gauge feature.

Source

pub fn timer_names(&self) -> Vec<String>

All unlabeled timer names. Requires the timer feature.

Source

pub fn metric_count(&self) -> usize

Total number of registered metrics across all enabled metric types and label combinations.

Source

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.

Source

pub fn counter_entries(&self) -> Vec<(String, LabelSet, Arc<Counter>)>

Capture every counter as (name, labels, Arc<Counter>). Requires the count feature.

Source

pub fn gauge_entries(&self) -> Vec<(String, LabelSet, Arc<Gauge>)>

Capture every gauge as (name, labels, Arc<Gauge>). Requires the gauge feature.

Source

pub fn timer_entries(&self) -> Vec<(String, LabelSet, Arc<Timer>)>

Capture every timer as (name, labels, Arc<Timer>). Requires the timer feature.

Source

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"

Trait Implementations§

Source§

impl Default for Registry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.