use core::cell::RefCell;
use core::fmt;
use critical_section::Mutex;
use portable_atomic::{AtomicU64, Ordering};
use crate::config::LABEL_VALUE_LEN;
use crate::counter::Counter;
use crate::erased::{ErasedCounterVec, ErasedGaugeVec, ErasedHistogramVec};
#[cfg(feature = "consistent-histograms")]
use crate::erased::{HistogramSnapshot, HistogramSnapshotError, snapshot_bucket_prefix};
use crate::escape::valid_label_name;
use crate::gauge::Gauge;
#[cfg(feature = "float")]
use crate::gauge::GaugeF64;
#[cfg(feature = "float")]
use crate::histogram::validate_f64_bounds;
use crate::histogram::validate_u64_bounds;
use crate::labels::{LabelBlock, build_block};
#[cfg(feature = "float")]
use crate::series::HistSeries;
use crate::series::IntHistSeries;
use crate::value::Value;
struct KeyMapState<const N: usize, const V: usize> {
blocks: heapless::Vec<LabelBlock<V>, N>,
fingerprints: [u32; N],
sorted_slots: [usize; N],
}
impl<const N: usize, const V: usize> KeyMapState<N, V> {
const fn new() -> Self {
Self {
blocks: heapless::Vec::new(),
fingerprints: [0; N],
sorted_slots: [0; N],
}
}
}
type KeyMap<const N: usize, const V: usize> = Mutex<RefCell<KeyMapState<N, V>>>;
fn label_fingerprint(values: &[&str]) -> u32 {
fn pair_at(bytes: &[u8], offset: usize) -> u32 {
bytes.get(offset).copied().map_or(0, u32::from)
| bytes.get(offset + 1).copied().map_or(0, u32::from) << 8
}
let mut fingerprint = 0x811c_9dc5_u32;
for value in values {
let bytes = value.as_bytes();
let first = pair_at(bytes, 0);
let middle = pair_at(bytes, bytes.len().saturating_sub(1) / 2);
let last = pair_at(bytes, bytes.len().saturating_sub(2));
let sample = first ^ middle.rotate_left(11) ^ last.rotate_left(22);
let len = u32::try_from(bytes.len()).unwrap_or(u32::MAX);
fingerprint ^= sample ^ len.wrapping_mul(0x9e37_79b1);
fingerprint = fingerprint.rotate_left(13).wrapping_mul(0x85eb_ca6b);
}
fingerprint
}
const fn str_eq(a: &str, b: &str) -> bool {
let a = a.as_bytes();
let b = b.as_bytes();
if a.len() != b.len() {
return false;
}
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}
const fn validate_label_names<const K: usize>(names: &'static [&'static str; K], histogram: bool) {
if histogram {
assert!(
K > 0,
"embeprom: histogram vectors require at least one label"
);
}
let mut i = 0;
while i < K {
assert!(valid_label_name(names[i]), "embeprom: invalid label name");
assert!(
!(histogram && str_eq(names[i], "le")),
"embeprom: histogram label name `le` is reserved"
);
let mut j = 0;
while j < i {
assert!(
!str_eq(names[i], names[j]),
"embeprom: duplicate label name"
);
j += 1;
}
i += 1;
}
}
fn slot_index<const N: usize, const K: usize, const V: usize>(
keys: &KeyMap<N, V>,
names: &'static [&'static str; K],
values: &[&str; K],
) -> Option<usize> {
let block = build_block::<K, V>(names, values).ok()?;
let fingerprint = label_fingerprint(values);
critical_section::with(|cs| {
let mut keys = keys.borrow_ref_mut(cs);
let len = keys.blocks.len();
let result = keys.sorted_slots[..len].binary_search_by(|slot| {
keys.fingerprints[*slot]
.cmp(&fingerprint)
.then_with(|| keys.blocks[*slot].as_str().cmp(block.as_str()))
});
match result {
Ok(position) => Some(keys.sorted_slots[position]),
Err(position) if len < N => {
keys.blocks.push(block).ok()?;
keys.fingerprints[len] = fingerprint;
keys.sorted_slots.copy_within(position..len, position + 1);
keys.sorted_slots[position] = len;
Some(len)
}
Err(_) => None,
}
})
}
fn write_labels_at<const N: usize, const V: usize>(
keys: &KeyMap<N, V>,
s: usize,
out: &mut dyn fmt::Write,
) -> fmt::Result {
let block = critical_section::with(|cs| keys.borrow_ref(cs).blocks[s].clone());
out.write_str(&block)
}
fn series_count_of<const N: usize, const V: usize>(keys: &KeyMap<N, V>) -> usize {
critical_section::with(|cs| keys.borrow_ref(cs).blocks.len())
}
#[derive(Debug, Clone, Copy)]
pub struct CounterSeries<'a> {
counter: Option<&'a Counter>,
}
impl CounterSeries<'_> {
fn metric(counter: &Counter) -> CounterSeries<'_> {
CounterSeries {
counter: Some(counter),
}
}
const fn sink() -> Self {
Self { counter: None }
}
#[inline]
pub fn inc(&self) {
if let Some(counter) = self.counter {
counter.inc();
}
}
#[inline]
pub fn inc_by(&self, n: u64) {
if let Some(counter) = self.counter {
counter.inc_by(n);
}
}
#[inline]
pub fn get(&self) -> u64 {
self.counter.map_or(0, Counter::get)
}
#[inline]
pub fn reset(&self) {
if let Some(counter) = self.counter {
counter.reset();
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct GaugeSeries<'a> {
gauge: Option<&'a Gauge>,
}
impl GaugeSeries<'_> {
fn metric(gauge: &Gauge) -> GaugeSeries<'_> {
GaugeSeries { gauge: Some(gauge) }
}
const fn sink() -> Self {
Self { gauge: None }
}
#[inline]
pub fn set(&self, value: i64) {
if let Some(gauge) = self.gauge {
gauge.set(value);
}
}
#[inline]
pub fn add(&self, delta: i64) {
if let Some(gauge) = self.gauge {
gauge.add(delta);
}
}
#[inline]
pub fn sub(&self, delta: i64) {
if let Some(gauge) = self.gauge {
gauge.sub(delta);
}
}
#[inline]
pub fn inc(&self) {
if let Some(gauge) = self.gauge {
gauge.inc();
}
}
#[inline]
pub fn dec(&self) {
if let Some(gauge) = self.gauge {
gauge.dec();
}
}
#[inline]
pub fn get(&self) -> i64 {
self.gauge.map_or(0, Gauge::get)
}
}
pub struct CounterVec<const N: usize, const K: usize = 1, const V: usize = LABEL_VALUE_LEN> {
names: &'static [&'static str; K],
keys: KeyMap<N, V>,
vals: [Counter; N],
}
impl<const N: usize, const K: usize, const V: usize> CounterVec<N, K, V> {
pub const fn new(names: &'static [&'static str; K]) -> Self {
validate_label_names(names, false);
Self {
names,
keys: Mutex::new(RefCell::new(KeyMapState::new())),
vals: [const { Counter::new() }; N],
}
}
pub fn with(&self, values: &[&str; K]) -> CounterSeries<'_> {
match slot_index(&self.keys, self.names, values) {
Some(idx) => CounterSeries::metric(&self.vals[idx]),
None => CounterSeries::sink(),
}
}
#[inline]
pub fn inc(&self, values: &[&str; K]) {
self.with(values).inc();
}
#[inline]
pub fn inc_by(&self, values: &[&str; K], n: u64) {
self.with(values).inc_by(n);
}
pub fn series_count(&self) -> usize {
series_count_of(&self.keys)
}
}
impl<const N: usize, const K: usize, const V: usize> ErasedCounterVec for CounterVec<N, K, V> {
fn series_count(&self) -> usize {
CounterVec::series_count(self)
}
fn write_labels(&self, s: usize, out: &mut dyn fmt::Write) -> fmt::Result {
write_labels_at(&self.keys, s, out)
}
fn value(&self, s: usize) -> u64 {
self.vals[s].get()
}
}
pub struct GaugeVec<const N: usize, const K: usize = 1, const V: usize = LABEL_VALUE_LEN> {
names: &'static [&'static str; K],
keys: KeyMap<N, V>,
vals: [Gauge; N],
}
impl<const N: usize, const K: usize, const V: usize> GaugeVec<N, K, V> {
pub const fn new(names: &'static [&'static str; K]) -> Self {
validate_label_names(names, false);
Self {
names,
keys: Mutex::new(RefCell::new(KeyMapState::new())),
vals: [const { Gauge::new(0) }; N],
}
}
pub fn with(&self, values: &[&str; K]) -> GaugeSeries<'_> {
match slot_index(&self.keys, self.names, values) {
Some(idx) => GaugeSeries::metric(&self.vals[idx]),
None => GaugeSeries::sink(),
}
}
#[inline]
pub fn set(&self, values: &[&str; K], v: i64) {
self.with(values).set(v);
}
#[inline]
pub fn add(&self, values: &[&str; K], d: i64) {
self.with(values).add(d);
}
pub fn series_count(&self) -> usize {
series_count_of(&self.keys)
}
}
impl<const N: usize, const K: usize, const V: usize> ErasedGaugeVec for GaugeVec<N, K, V> {
fn series_count(&self) -> usize {
GaugeVec::series_count(self)
}
fn write_labels(&self, s: usize, out: &mut dyn fmt::Write) -> fmt::Result {
write_labels_at(&self.keys, s, out)
}
fn value(&self, s: usize) -> Value {
Value::I64(self.vals[s].get())
}
}
#[cfg(feature = "float")]
#[derive(Debug, Clone, Copy)]
pub struct GaugeF64Series<'a> {
gauge: Option<&'a GaugeF64>,
}
#[cfg(feature = "float")]
impl GaugeF64Series<'_> {
fn metric(gauge: &GaugeF64) -> GaugeF64Series<'_> {
GaugeF64Series { gauge: Some(gauge) }
}
const fn sink() -> Self {
Self { gauge: None }
}
#[inline]
pub fn set(&self, value: f64) {
if let Some(gauge) = self.gauge {
gauge.set(value);
}
}
#[inline]
pub fn add(&self, delta: f64) {
if let Some(gauge) = self.gauge {
gauge.add(delta);
}
}
#[inline]
pub fn sub(&self, delta: f64) {
if let Some(gauge) = self.gauge {
gauge.sub(delta);
}
}
#[inline]
pub fn inc(&self) {
if let Some(gauge) = self.gauge {
gauge.inc();
}
}
#[inline]
pub fn dec(&self) {
if let Some(gauge) = self.gauge {
gauge.dec();
}
}
#[inline]
pub fn get(&self) -> f64 {
self.gauge.map_or(0.0, GaugeF64::get)
}
}
#[cfg(feature = "float")]
pub struct GaugeF64Vec<const N: usize, const K: usize = 1, const V: usize = LABEL_VALUE_LEN> {
names: &'static [&'static str; K],
keys: KeyMap<N, V>,
vals: [GaugeF64; N],
}
#[cfg(feature = "float")]
impl<const N: usize, const K: usize, const V: usize> GaugeF64Vec<N, K, V> {
pub const fn new(names: &'static [&'static str; K]) -> Self {
validate_label_names(names, false);
Self {
names,
keys: Mutex::new(RefCell::new(KeyMapState::new())),
vals: [const { GaugeF64::new(0.0) }; N],
}
}
pub fn with(&self, values: &[&str; K]) -> GaugeF64Series<'_> {
match slot_index(&self.keys, self.names, values) {
Some(idx) => GaugeF64Series::metric(&self.vals[idx]),
None => GaugeF64Series::sink(),
}
}
#[inline]
pub fn set(&self, values: &[&str; K], v: f64) {
self.with(values).set(v);
}
#[inline]
pub fn add(&self, values: &[&str; K], d: f64) {
self.with(values).add(d);
}
pub fn series_count(&self) -> usize {
series_count_of(&self.keys)
}
}
#[cfg(feature = "float")]
impl<const N: usize, const K: usize, const V: usize> ErasedGaugeVec for GaugeF64Vec<N, K, V> {
fn series_count(&self) -> usize {
GaugeF64Vec::series_count(self)
}
fn write_labels(&self, s: usize, out: &mut dyn fmt::Write) -> fmt::Result {
write_labels_at(&self.keys, s, out)
}
fn value(&self, s: usize) -> Value {
Value::F64(self.vals[s].get())
}
}
pub struct IntHistogramVec<
const N: usize,
const B: usize,
const K: usize = 1,
const V: usize = LABEL_VALUE_LEN,
> {
names: &'static [&'static str; K],
bounds: &'static [u64],
keys: KeyMap<N, V>,
buckets: [[AtomicU64; B]; N],
sums: [AtomicU64; N],
counts: [AtomicU64; N],
}
impl<const N: usize, const B: usize, const K: usize, const V: usize> IntHistogramVec<N, B, K, V> {
pub const fn new(names: &'static [&'static str; K], bounds: &'static [u64]) -> Self {
validate_label_names(names, true);
validate_u64_bounds::<B>(bounds);
Self {
names,
bounds,
keys: Mutex::new(RefCell::new(KeyMapState::new())),
buckets: [const { [const { AtomicU64::new(0) }; B] }; N],
sums: [const { AtomicU64::new(0) }; N],
counts: [const { AtomicU64::new(0) }; N],
}
}
pub fn with(&self, values: &[&str; K]) -> IntHistSeries<'_> {
match slot_index(&self.keys, self.names, values) {
Some(idx) => IntHistSeries::bound(
self.bounds,
&self.buckets[idx],
&self.sums[idx],
&self.counts[idx],
),
None => IntHistSeries::sink(self.bounds),
}
}
#[inline]
pub fn observe(&self, values: &[&str; K], v: u64) {
self.with(values).observe(v);
}
pub fn bounds(&self) -> &'static [u64] {
self.bounds
}
pub fn series_count(&self) -> usize {
series_count_of(&self.keys)
}
}
impl<const N: usize, const B: usize, const K: usize, const V: usize> ErasedHistogramVec
for IntHistogramVec<N, B, K, V>
{
fn bucket_count(&self) -> usize {
B
}
fn bound(&self, b: usize) -> Value {
Value::U64(self.bounds[b])
}
fn series_count(&self) -> usize {
IntHistogramVec::series_count(self)
}
fn write_labels(&self, s: usize, out: &mut dyn fmt::Write) -> fmt::Result {
write_labels_at(&self.keys, s, out)
}
fn bucket(&self, s: usize, b: usize) -> u64 {
self.buckets[s][b].load(Ordering::Relaxed)
}
fn total_count(&self, s: usize) -> u64 {
self.counts[s].load(Ordering::Relaxed)
}
fn sum(&self, s: usize) -> Value {
Value::U64(self.sums[s].load(Ordering::Relaxed))
}
#[cfg(feature = "consistent-histograms")]
fn snapshot<'a>(
&self,
s: usize,
buckets: &'a mut [u64],
) -> Result<HistogramSnapshot<'a>, HistogramSnapshotError> {
let buckets = snapshot_bucket_prefix(buckets, B)?;
critical_section::with(|_cs| {
for (snapshot_bucket, bucket) in buckets.iter_mut().zip(&self.buckets[s]) {
*snapshot_bucket = bucket.load(Ordering::Relaxed);
}
Ok(HistogramSnapshot {
buckets,
sum: Value::U64(self.sums[s].load(Ordering::Relaxed)),
count: self.counts[s].load(Ordering::Relaxed),
})
})
}
}
#[cfg(feature = "float")]
pub struct HistogramVec<
const N: usize,
const B: usize,
const K: usize = 1,
const V: usize = LABEL_VALUE_LEN,
> {
names: &'static [&'static str; K],
bounds: &'static [f64],
keys: KeyMap<N, V>,
buckets: [[AtomicU64; B]; N],
sums: [portable_atomic::AtomicF64; N],
counts: [AtomicU64; N],
}
#[cfg(feature = "float")]
impl<const N: usize, const B: usize, const K: usize, const V: usize> HistogramVec<N, B, K, V> {
pub const fn new(names: &'static [&'static str; K], bounds: &'static [f64]) -> Self {
validate_label_names(names, true);
validate_f64_bounds::<B>(bounds);
Self {
names,
bounds,
keys: Mutex::new(RefCell::new(KeyMapState::new())),
buckets: [const { [const { AtomicU64::new(0) }; B] }; N],
sums: [const { portable_atomic::AtomicF64::new(0.0) }; N],
counts: [const { AtomicU64::new(0) }; N],
}
}
pub fn with(&self, values: &[&str; K]) -> HistSeries<'_> {
match slot_index(&self.keys, self.names, values) {
Some(idx) => HistSeries::bound(
self.bounds,
&self.buckets[idx],
&self.sums[idx],
&self.counts[idx],
),
None => HistSeries::sink(self.bounds),
}
}
#[inline]
pub fn observe(&self, values: &[&str; K], v: f64) {
self.with(values).observe(v);
}
pub fn bounds(&self) -> &'static [f64] {
self.bounds
}
pub fn series_count(&self) -> usize {
series_count_of(&self.keys)
}
}
#[cfg(feature = "float")]
impl<const N: usize, const B: usize, const K: usize, const V: usize> ErasedHistogramVec
for HistogramVec<N, B, K, V>
{
fn bucket_count(&self) -> usize {
B
}
fn bound(&self, b: usize) -> Value {
Value::F64(self.bounds[b])
}
fn series_count(&self) -> usize {
HistogramVec::series_count(self)
}
fn write_labels(&self, s: usize, out: &mut dyn fmt::Write) -> fmt::Result {
write_labels_at(&self.keys, s, out)
}
fn bucket(&self, s: usize, b: usize) -> u64 {
self.buckets[s][b].load(Ordering::Relaxed)
}
fn total_count(&self, s: usize) -> u64 {
self.counts[s].load(Ordering::Relaxed)
}
fn sum(&self, s: usize) -> Value {
Value::F64(self.sums[s].load(Ordering::Relaxed))
}
#[cfg(feature = "consistent-histograms")]
fn snapshot<'a>(
&self,
s: usize,
buckets: &'a mut [u64],
) -> Result<HistogramSnapshot<'a>, HistogramSnapshotError> {
let buckets = snapshot_bucket_prefix(buckets, B)?;
critical_section::with(|_cs| {
for (snapshot_bucket, bucket) in buckets.iter_mut().zip(&self.buckets[s]) {
*snapshot_bucket = bucket.load(Ordering::Relaxed);
}
Ok(HistogramSnapshot {
buckets,
sum: Value::F64(self.sums[s].load(Ordering::Relaxed)),
count: self.counts[s].load(Ordering::Relaxed),
})
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "float")]
use crate::histogram::Histogram;
use crate::histogram::IntHistogram;
static REASON: [&str; 1] = ["reason"];
static PEER: [&str; 1] = ["peer"];
fn labels_of<E: ErasedCounterVec>(v: &E, s: usize) -> heapless::String<64> {
let mut out = heapless::String::<64>::new();
v.write_labels(s, &mut out).unwrap();
out
}
#[test]
fn counter_vec_creates_and_increments_series() {
let cv: CounterVec<4> = CounterVec::new(&REASON);
cv.inc(&["timeout"]);
cv.inc_by(&["timeout"], 2);
cv.inc(&["auth_fail"]);
assert_eq!(cv.series_count(), 2);
assert_eq!(cv.with(&["timeout"]).get(), 3);
assert_eq!(cv.with(&["auth_fail"]).get(), 1);
assert_eq!(labels_of(&cv, 0), "reason=\"timeout\"");
}
#[test]
fn sorted_lookup_preserves_insertion_order_slots() {
let cv: CounterVec<4> = CounterVec::new(&REASON);
let values = ["zz", "aa", "mm", "00"];
let rendered = [
"reason=\"zz\"",
"reason=\"aa\"",
"reason=\"mm\"",
"reason=\"00\"",
];
for (i, value) in values.iter().enumerate() {
cv.inc_by(&[*value], (i + 1) as u64);
}
for i in (0..values.len()).rev() {
assert_eq!(cv.with(&[values[i]]).get(), (i + 1) as u64);
assert_eq!(labels_of(&cv, i), rendered[i]);
}
}
#[test]
fn fingerprint_collisions_fall_back_to_exact_label_comparison() {
let first = "aa11bbaacc";
let second = "aa00bbaacc";
assert_eq!(label_fingerprint(&[first]), label_fingerprint(&[second]));
let cv: CounterVec<2> = CounterVec::new(&REASON);
cv.inc_by(&[first], 1);
cv.inc_by(&[second], 2);
assert_eq!(cv.series_count(), 2);
assert_eq!(cv.with(&[first]).get(), 1);
assert_eq!(cv.with(&[second]).get(), 2);
assert_eq!(labels_of(&cv, 0), "reason=\"aa11bbaacc\"");
assert_eq!(labels_of(&cv, 1), "reason=\"aa00bbaacc\"");
}
#[test]
#[should_panic(expected = "invalid label name")]
fn counter_vec_rejects_invalid_label_names_at_runtime() {
let _ = CounterVec::<1, 1>::new(&["not-valid"]);
}
#[test]
#[should_panic(expected = "duplicate label name")]
fn counter_vec_rejects_duplicate_label_names_at_runtime() {
let _ = CounterVec::<1, 2>::new(&["kind", "kind"]);
}
#[test]
fn cached_counter_series_can_be_reused_for_hot_path_updates() {
let cv: CounterVec<4> = CounterVec::new(&REASON);
let timeout = cv.with(&["timeout"]);
timeout.inc();
timeout.inc_by(4);
assert_eq!(timeout.get(), 5);
assert_eq!(cv.series_count(), 1);
}
#[test]
fn counter_vec_drops_excess_series_but_keeps_existing_ones_updating() {
let cv: CounterVec<2> = CounterVec::new(&REASON);
cv.inc(&["a"]);
cv.inc(&["b"]);
let sink = cv.with(&["c"]); sink.inc_by(100);
assert_eq!(sink.get(), 0);
cv.inc(&["a"]); assert_eq!(cv.series_count(), 2);
assert_eq!(cv.with(&["a"]).get(), 2);
}
#[test]
fn counter_vec_drops_oversized_label_values_without_affecting_existing_series() {
let cv: CounterVec<4, 1, 20> = CounterVec::new(&REASON);
cv.inc(&["ok"]);
let sink = cv.with(&["this value is far too long to fit"]);
sink.inc();
assert_eq!(sink.get(), 0);
assert_eq!(cv.series_count(), 1);
assert_eq!(cv.with(&["ok"]).get(), 1);
}
#[test]
fn gauge_vec_creates_and_sets_series() {
let gv: GaugeVec<4> = GaugeVec::new(&PEER);
gv.set(&["ap-1"], 10);
gv.add(&["ap-1"], -3);
gv.set(&["ap-2"], 5);
assert_eq!(gv.with(&["ap-1"]).get(), 7);
assert_eq!(gv.with(&["ap-2"]).get(), 5);
assert_eq!(gv.series_count(), 2);
}
#[test]
fn gauge_vec_returns_zero_reading_sink_when_full() {
let gv: GaugeVec<1> = GaugeVec::new(&PEER);
gv.set(&["ap-1"], 7);
let sink = gv.with(&["ap-2"]);
sink.set(99);
sink.inc();
assert_eq!(sink.get(), 0);
assert_eq!(gv.with(&["ap-1"]).get(), 7);
assert_eq!(gv.series_count(), 1);
}
#[cfg(feature = "float")]
#[test]
fn gauge_f64_vec_creates_and_sets_series() {
let gv: GaugeF64Vec<4> = GaugeF64Vec::new(&PEER);
gv.set(&["ap-1"], 10.5);
gv.add(&["ap-1"], -3.0);
gv.set(&["ap-2"], 5.25);
let ap1 = gv.with(&["ap-1"]);
ap1.sub(0.5);
ap1.inc();
ap1.dec();
assert_eq!(ap1.get().to_bits(), 7.0_f64.to_bits());
assert_eq!(gv.with(&["ap-2"]).get().to_bits(), 5.25_f64.to_bits());
assert_eq!(gv.series_count(), 2);
}
#[cfg(feature = "float")]
#[test]
fn gauge_f64_vec_returns_zero_reading_sink_when_full() {
let gv: GaugeF64Vec<1> = GaugeF64Vec::new(&PEER);
gv.set(&["ap-1"], 7.0);
let sink = gv.with(&["ap-2"]);
sink.set(99.0);
sink.add(1.0);
assert_eq!(sink.get().to_bits(), 0.0_f64.to_bits());
assert_eq!(gv.with(&["ap-1"]).get().to_bits(), 7.0_f64.to_bits());
assert_eq!(gv.series_count(), 1);
}
#[test]
fn int_histogram_vec_tracks_independent_series() {
let hv: IntHistogramVec<4, 2> = IntHistogramVec::new(&PEER, &[100, 1000]);
hv.observe(&["ap-1"], 50);
hv.observe(&["ap-1"], 500);
hv.observe(&["ap-2"], 2000);
let ap1 = hv.with(&["ap-1"]);
let ap2 = hv.with(&["ap-2"]);
assert_eq!(hv.series_count(), 2);
assert_eq!(hv.bounds(), &[100, 1000]);
assert_eq!(ap1.bounds(), &[100, 1000]);
assert_eq!(ap1.bucket(0), 1);
assert_eq!(ap1.bucket(1), 1);
assert_eq!(ap1.count(), 2);
assert_eq!(ap1.sum(), 550);
assert_eq!(ap2.bucket(0), 0);
assert_eq!(ap2.bucket(1), 0);
assert_eq!(ap2.count(), 1); assert_eq!(ap2.sum(), 2000);
}
fn observe_us<'a>(metric: impl Into<IntHistSeries<'a>>, v: u64) {
metric.into().observe(v);
}
#[test]
fn scalar_histogram_series_is_the_same_handle_as_a_bound_vec_series() {
let scalar = IntHistogram::<2>::new(&[100, 1000]);
let labeled: IntHistogramVec<4, 2> = IntHistogramVec::new(&PEER, &[100, 1000]);
observe_us(&scalar, 50);
observe_us(scalar.series(), 500);
observe_us(labeled.with(&["ap-1"]), 50);
assert_eq!(scalar.bucket(0), 1);
assert_eq!(scalar.bucket(1), 1);
assert_eq!(scalar.count(), 2);
assert_eq!(scalar.sum(), 550);
assert_eq!(scalar.series().sum(), 550);
assert_eq!(labeled.with(&["ap-1"]).count(), 1);
assert_eq!(labeled.with(&["ap-1"]).sum(), 50);
}
#[test]
fn cached_histogram_series_can_be_reused_for_hot_path_observations() {
let hv: IntHistogramVec<4, 2> = IntHistogramVec::new(&PEER, &[100, 1000]);
let ap = hv.with(&["ap-1"]);
ap.observe(50);
ap.observe(500);
assert_eq!(hv.series_count(), 1);
assert_eq!(ap.bucket(0), 1);
assert_eq!(ap.bucket(1), 1);
assert_eq!(ap.count(), 2);
assert_eq!(ap.sum(), 550);
}
#[test]
fn int_histogram_vec_sink_discards_observations_when_full() {
let hv: IntHistogramVec<1, 2> = IntHistogramVec::new(&PEER, &[100, 1000]);
hv.observe(&["ap-1"], 50);
let sink = hv.with(&["ap-2"]);
sink.observe(500);
assert_eq!(hv.series_count(), 1);
assert_eq!(sink.bounds(), &[100, 1000]);
assert_eq!(sink.bucket(0), 0);
assert_eq!(sink.bucket(1), 0);
assert_eq!(sink.count(), 0);
assert_eq!(sink.sum(), 0);
assert_eq!(hv.with(&["ap-1"]).count(), 1);
assert_eq!(hv.with(&["ap-1"]).sum(), 50);
}
#[test]
#[should_panic(expected = "histogram bounds must be strictly increasing")]
fn int_histogram_vec_rejects_invalid_bounds() {
let _ = IntHistogramVec::<1, 2>::new(&PEER, &[1000, 100]);
}
#[test]
#[should_panic(expected = "histogram label name `le` is reserved")]
fn int_histogram_vec_rejects_the_generated_bucket_label_name() {
let _ = IntHistogramVec::<1, 1>::new(&["le"], &[100]);
}
#[test]
#[should_panic(expected = "histogram vectors require at least one label")]
fn int_histogram_vec_requires_at_least_one_label() {
let _ = IntHistogramVec::<1, 1, 0>::new(&[], &[100]);
}
#[cfg(feature = "float")]
#[test]
fn float_histogram_series_is_the_same_handle_as_a_bound_vec_series() {
fn observe<'a>(metric: impl Into<HistSeries<'a>>, v: f64) {
metric.into().observe(v);
}
let scalar = Histogram::<2>::new(&[0.5, 5.0]);
let labeled: HistogramVec<4, 2> = HistogramVec::new(&PEER, &[0.5, 5.0]);
observe(&scalar, 0.25);
observe(labeled.with(&["ap-1"]), 2.0);
assert_eq!(scalar.count(), 1);
assert_eq!(labeled.with(&["ap-1"]).count(), 1);
assert_eq!(labeled.with(&["ap-1"]).sum().to_bits(), 2.0_f64.to_bits());
}
#[cfg(feature = "float")]
#[test]
fn histogram_vec_tracks_independent_series() {
let hv: HistogramVec<4, 2> = HistogramVec::new(&PEER, &[0.5, 5.0]);
hv.observe(&["ap-1"], 0.25);
hv.observe(&["ap-1"], 2.0);
let ap = hv.with(&["ap-1"]);
assert_eq!(hv.bounds(), &[0.5, 5.0]);
assert_eq!(ap.bounds(), &[0.5, 5.0]);
assert_eq!(ap.bucket(0), 1);
assert_eq!(ap.bucket(1), 1);
assert_eq!(ap.count(), 2);
assert_eq!(ap.sum().to_bits(), 2.25_f64.to_bits());
}
#[cfg(feature = "float")]
#[test]
fn histogram_vec_routes_nan_only_to_the_implicit_infinite_bucket() {
let hv: HistogramVec<1, 2> = HistogramVec::new(&PEER, &[0.5, 5.0]);
hv.observe(&["ap-1"], f64::NAN);
let ap = hv.with(&["ap-1"]);
assert_eq!(ap.bucket(0), 0);
assert_eq!(ap.bucket(1), 0);
assert_eq!(ap.count(), 1);
assert!(ap.sum().is_nan());
}
#[cfg(feature = "float")]
#[test]
#[should_panic(expected = "histogram vectors require at least one label")]
fn histogram_vec_requires_at_least_one_label() {
let _ = HistogramVec::<1, 1, 0>::new(&[], &[0.5]);
}
#[cfg(feature = "float")]
#[test]
fn float_histogram_vec_sink_discards_observations_when_full() {
let hv: HistogramVec<1, 2> = HistogramVec::new(&PEER, &[0.5, 5.0]);
hv.observe(&["ap-1"], 0.25);
let sink = hv.with(&["ap-2"]);
sink.observe(2.0);
assert_eq!(hv.series_count(), 1);
assert_eq!(sink.count(), 0);
assert_eq!(sink.sum().to_bits(), 0.0_f64.to_bits());
assert_eq!(hv.with(&["ap-1"]).count(), 1);
assert_eq!(hv.with(&["ap-1"]).sum().to_bits(), 0.25_f64.to_bits());
}
}