Skip to main content

DynamicCounterSet

Struct DynamicCounterSet 

Source
pub struct DynamicCounterSet { /* private fields */ }
Expand description

Runtime-label grouped counters.

Use DynamicCounterSet when label values are discovered at runtime and one logical operation updates several related counters for the same label set. Resolve and cache a DynamicCounterSetSeries once per active label set, then use pre-resolved counter indexes on the hot path.

use fast_telemetry::DynamicCounterSet;

let counters = DynamicCounterSet::with_shards(
    4,
    ["requests", "bytes", "errors"],
);
let requests = counters.counter_index("requests").unwrap();
let bytes = counters.counter_index("bytes").unwrap();

let series = counters.get_or_create(&[
    ("org", "eden"),
    ("endpoint", "ingest"),
]);
series.add_index_values(&[(requests, 1), (bytes, 4096)]);

assert_eq!(series.get("requests"), 1);
assert_eq!(series.get("bytes"), 4096);

Implementations§

Source§

impl DynamicCounterSet

Source

pub fn new<I, S>(counter_names: I) -> Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Creates a new dynamic grouped counter with a default shard count.

Prefer DynamicCounterSet::with_shards in services that already know the desired shard count.

Source

pub fn with_shards<I, S>(shard_count: usize, counter_names: I) -> Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Creates a new dynamic grouped counter with shard_count shards.

Source

pub fn with_max_series<I, S>( shard_count: usize, max_series: usize, counter_names: I, ) -> Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Creates a new dynamic grouped counter with a series cardinality cap.

When the number of unique label sets approximately reaches max_series, new label sets are redirected into a single overflow series (__ft_overflow=true). A max_series of 0 disables the cap.

Source

pub fn series(&self, labels: &[(&str, &str)]) -> DynamicCounterSetSeries

Resolve a reusable grouped series handle for labels.

Source

pub fn get_or_create(&self, labels: &[(&str, &str)]) -> DynamicCounterSetSeries

Resolve a reusable grouped series handle for labels.

Source

pub fn inc(&self, labels: &[(&str, &str)], counter: &str)

Increment one named counter for the series identified by labels.

Source

pub fn add(&self, labels: &[(&str, &str)], counter: &str, value: isize)

Add value to one named counter for the series identified by labels.

Source

pub fn add_index( &self, labels: &[(&str, &str)], counter_idx: usize, value: isize, )

Add value to one pre-resolved counter index for labels.

Source

pub fn add_values(&self, labels: &[(&str, &str)], updates: &[(&str, isize)])

Add named counter values in one grouped call.

Source

pub fn add_index_values( &self, labels: &[(&str, &str)], updates: &[(usize, isize)], )

Add pre-resolved (counter_idx, value) updates in one grouped call.

Source

pub fn add_all_values(&self, labels: &[(&str, &str)], values: &[isize])

Add one value for every counter in index order.

Source

pub fn get(&self, labels: &[(&str, &str)], counter: &str) -> isize

Get one named counter for labels.

Source

pub fn get_index(&self, labels: &[(&str, &str)], counter_idx: usize) -> isize

Get one pre-resolved counter index for labels.

Source

pub fn values(&self, labels: &[(&str, &str)]) -> Vec<isize>

Get all values for labels in counter index order.

Source

pub fn sum_all(&self) -> isize

Sums all counters across all dynamic series.

Source

pub fn snapshot(&self) -> Vec<(DynamicLabelSet, Vec<isize>)>

Return a cumulative snapshot of all dynamic label sets.

Source

pub fn snapshot_and_reset(&self) -> Vec<(DynamicLabelSet, Vec<isize>)>

Return a snapshot of all dynamic label sets and reset counters to zero.

Writes that occur concurrently with this method may be attributed to the next snapshot window rather than the current one. No counts are lost.

Source

pub fn sum_and_reset(&self, counter_idx: usize) -> isize

Reset one counter index for all dynamic label sets and return its total.

Source

pub fn visit_series(&self, f: impl FnMut(&str, &[(String, String)], isize))

Iterate all counter values without cloning label sets.

Calls f(counter_name, labels, value) for every counter in every dynamic label set. The callback runs while a shard read lock is held; it must be fast and must not call back into this metric.

Source

pub fn counter_names(&self) -> &[String]

Returns counter names in index order.

Source

pub fn counter_index(&self, counter: &str) -> Option<usize>

Return the index for counter, if it exists.

Source

pub fn cardinality(&self) -> usize

Returns the current number of distinct dynamic label sets.

Source

pub fn overflow_count(&self) -> u64

Returns the number of records routed to the overflow bucket.

Source

pub fn evict_stale(&self, max_staleness: u32) -> usize

Evict series that haven’t been accessed for max_staleness cycles.

Protected series (Arc::strong_count > 1) are never evicted because a caller still holds a DynamicCounterSetSeries handle.

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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more