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
impl DynamicCounterSet
Sourcepub fn new<I, S>(counter_names: I) -> Self
pub fn new<I, S>(counter_names: I) -> Self
Creates a new dynamic grouped counter with a default shard count.
Prefer DynamicCounterSet::with_shards in services that already know
the desired shard count.
Sourcepub fn with_shards<I, S>(shard_count: usize, counter_names: I) -> Self
pub fn with_shards<I, S>(shard_count: usize, counter_names: I) -> Self
Creates a new dynamic grouped counter with shard_count shards.
Sourcepub fn with_max_series<I, S>(
shard_count: usize,
max_series: usize,
counter_names: I,
) -> Self
pub fn with_max_series<I, S>( shard_count: usize, max_series: usize, counter_names: I, ) -> Self
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.
Sourcepub fn series(&self, labels: &[(&str, &str)]) -> DynamicCounterSetSeries
pub fn series(&self, labels: &[(&str, &str)]) -> DynamicCounterSetSeries
Resolve a reusable grouped series handle for labels.
Sourcepub fn get_or_create(&self, labels: &[(&str, &str)]) -> DynamicCounterSetSeries
pub fn get_or_create(&self, labels: &[(&str, &str)]) -> DynamicCounterSetSeries
Resolve a reusable grouped series handle for labels.
Sourcepub fn inc(&self, labels: &[(&str, &str)], counter: &str)
pub fn inc(&self, labels: &[(&str, &str)], counter: &str)
Increment one named counter for the series identified by labels.
Sourcepub fn add(&self, labels: &[(&str, &str)], counter: &str, value: isize)
pub fn add(&self, labels: &[(&str, &str)], counter: &str, value: isize)
Add value to one named counter for the series identified by labels.
Sourcepub fn add_index(
&self,
labels: &[(&str, &str)],
counter_idx: usize,
value: isize,
)
pub fn add_index( &self, labels: &[(&str, &str)], counter_idx: usize, value: isize, )
Add value to one pre-resolved counter index for labels.
Sourcepub fn add_values(&self, labels: &[(&str, &str)], updates: &[(&str, isize)])
pub fn add_values(&self, labels: &[(&str, &str)], updates: &[(&str, isize)])
Add named counter values in one grouped call.
Sourcepub fn add_index_values(
&self,
labels: &[(&str, &str)],
updates: &[(usize, isize)],
)
pub fn add_index_values( &self, labels: &[(&str, &str)], updates: &[(usize, isize)], )
Add pre-resolved (counter_idx, value) updates in one grouped call.
Sourcepub fn add_all_values(&self, labels: &[(&str, &str)], values: &[isize])
pub fn add_all_values(&self, labels: &[(&str, &str)], values: &[isize])
Add one value for every counter in index order.
Sourcepub fn get(&self, labels: &[(&str, &str)], counter: &str) -> isize
pub fn get(&self, labels: &[(&str, &str)], counter: &str) -> isize
Get one named counter for labels.
Sourcepub fn get_index(&self, labels: &[(&str, &str)], counter_idx: usize) -> isize
pub fn get_index(&self, labels: &[(&str, &str)], counter_idx: usize) -> isize
Get one pre-resolved counter index for labels.
Sourcepub fn values(&self, labels: &[(&str, &str)]) -> Vec<isize>
pub fn values(&self, labels: &[(&str, &str)]) -> Vec<isize>
Get all values for labels in counter index order.
Sourcepub fn snapshot(&self) -> Vec<(DynamicLabelSet, Vec<isize>)>
pub fn snapshot(&self) -> Vec<(DynamicLabelSet, Vec<isize>)>
Return a cumulative snapshot of all dynamic label sets.
Sourcepub fn snapshot_and_reset(&self) -> Vec<(DynamicLabelSet, Vec<isize>)>
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.
Sourcepub fn sum_and_reset(&self, counter_idx: usize) -> isize
pub fn sum_and_reset(&self, counter_idx: usize) -> isize
Reset one counter index for all dynamic label sets and return its total.
Sourcepub fn visit_series(&self, f: impl FnMut(&str, &[(String, String)], isize))
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.
Sourcepub fn counter_names(&self) -> &[String]
pub fn counter_names(&self) -> &[String]
Returns counter names in index order.
Sourcepub fn counter_index(&self, counter: &str) -> Option<usize>
pub fn counter_index(&self, counter: &str) -> Option<usize>
Return the index for counter, if it exists.
Sourcepub fn cardinality(&self) -> usize
pub fn cardinality(&self) -> usize
Returns the current number of distinct dynamic label sets.
Sourcepub fn overflow_count(&self) -> u64
pub fn overflow_count(&self) -> u64
Returns the number of records routed to the overflow bucket.
Sourcepub fn evict_stale(&self, max_staleness: u32) -> usize
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§
impl !Freeze for DynamicCounterSet
impl !RefUnwindSafe for DynamicCounterSet
impl Send for DynamicCounterSet
impl Sync for DynamicCounterSet
impl Unpin for DynamicCounterSet
impl UnsafeUnpin for DynamicCounterSet
impl UnwindSafe for DynamicCounterSet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request