pub struct LabeledCounter<L: LabelEnum> { /* private fields */ }Expand description
A counter indexed by an enum label, providing O(1) dimensional metrics.
Instead of using a HashMap to look up counters by label values, this type uses the enum variant’s index to directly access an array of counters. This eliminates hashing overhead on the hot path.
§Example
ⓘ
use fast_telemetry::{LabeledCounter, LabelEnum};
#[derive(Copy, Clone, Debug)]
enum HttpMethod { Get, Post, Put, Delete, Other }
impl LabelEnum for HttpMethod {
const CARDINALITY: usize = 5;
const LABEL_NAME: &'static str = "method";
fn as_index(self) -> usize { self as usize }
fn from_index(index: usize) -> Self {
match index {
0 => Self::Get, 1 => Self::Post, 2 => Self::Put,
3 => Self::Delete, _ => Self::Other,
}
}
fn variant_name(self) -> &'static str {
match self {
Self::Get => "get", Self::Post => "post", Self::Put => "put",
Self::Delete => "delete", Self::Other => "other",
}
}
}
let counter: LabeledCounter<HttpMethod> = LabeledCounter::new(4);
counter.inc(HttpMethod::Get);
counter.add(HttpMethod::Post, 5);
// Iteration for export
for (label, count) in counter.iter() {
println!("{}={}: {}", HttpMethod::LABEL_NAME, label.variant_name(), count);
}Implementations§
Source§impl<L: LabelEnum> LabeledCounter<L>
impl<L: LabelEnum> LabeledCounter<L>
Trait Implementations§
Source§impl<L: LabelEnum> DogStatsDExport for LabeledCounter<L>
impl<L: LabelEnum> DogStatsDExport for LabeledCounter<L>
Source§impl<L: LabelEnum> OtlpExport for LabeledCounter<L>
impl<L: LabelEnum> OtlpExport for LabeledCounter<L>
Source§impl<L: LabelEnum> PrometheusExport for LabeledCounter<L>
impl<L: LabelEnum> PrometheusExport for LabeledCounter<L>
Auto Trait Implementations§
impl<L> Freeze for LabeledCounter<L>
impl<L> RefUnwindSafe for LabeledCounter<L>where
L: RefUnwindSafe,
impl<L> Send for LabeledCounter<L>where
L: Send,
impl<L> Sync for LabeledCounter<L>where
L: Sync,
impl<L> Unpin for LabeledCounter<L>where
L: Unpin,
impl<L> UnsafeUnpin for LabeledCounter<L>
impl<L> UnwindSafe for LabeledCounter<L>where
L: UnwindSafe,
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
Mutably borrows from an owned value. Read more
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>
Wrap the input message
T in a tonic::Request