use std::collections::{HashMap, VecDeque};
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock, PoisonError, RwLock};
use std::time::{Duration, Instant};
use super::observe_exit_with_fiber;
use super::registry::{MetricRegistry, MetricUpdate};
use crate::failure::Cause;
use crate::kernel::{Effect, ExitKind, Instr, erase};
pub type MetricTags = Arc<[(String, String)]>;
pub(crate) fn merge_tags(base: &[(String, String)], extra: &[(String, String)]) -> MetricTags {
let mut out: Vec<(String, String)> = base.to_vec();
for (k, v) in extra {
match out.iter_mut().find(|(ok, _)| ok == k) {
Some(slot) => slot.1.clone_from(v),
None => out.push((k.clone(), v.clone())),
}
}
out.sort_by(|a, b| a.0.cmp(&b.0));
out.into()
}
#[derive(Clone, Debug)]
pub struct Boundaries(Arc<[f64]>);
impl Boundaries {
pub fn new(bounds: impl IntoIterator<Item = f64>) -> Self {
let mut v: Vec<f64> = bounds.into_iter().filter(|b| b.is_finite()).collect();
v.sort_by(f64::total_cmp);
v.dedup();
Self(v.into())
}
pub fn linear(start: f64, width: f64, count: usize) -> Self {
Self::new((0..count).map(|i| start + width * i as f64))
}
pub fn exponential(start: f64, factor: f64, count: usize) -> Self {
Self::new((0..count).map(|i| start * factor.powi(i as i32)))
}
pub fn as_slice(&self) -> &[f64] {
&self.0
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl PartialEq for Boundaries {
fn eq(&self, other: &Self) -> bool {
self.0.len() == other.0.len()
&& self
.0
.iter()
.zip(other.0.iter())
.all(|(a, b)| a.to_bits() == b.to_bits())
}
}
impl Eq for Boundaries {}
impl Hash for Boundaries {
fn hash<H: Hasher>(&self, state: &mut H) {
for b in self.0.iter() {
b.to_bits().hash(state);
}
}
}
#[derive(Clone, Debug)]
pub struct SummaryConfig {
max_age: Duration,
max_size: usize,
quantiles: Arc<[f64]>,
}
impl SummaryConfig {
pub fn new(max_age: Duration, max_size: usize, quantiles: impl IntoIterator<Item = f64>) -> Self {
let mut q: Vec<f64> = quantiles
.into_iter()
.filter(|q| (0.0..=1.0).contains(q))
.collect();
q.sort_by(f64::total_cmp);
q.dedup();
Self {
max_age,
max_size: max_size.max(1),
quantiles: q.into(),
}
}
pub fn max_age(&self) -> Duration {
self.max_age
}
pub fn max_size(&self) -> usize {
self.max_size
}
pub fn quantiles(&self) -> &[f64] {
&self.quantiles
}
}
impl PartialEq for SummaryConfig {
fn eq(&self, other: &Self) -> bool {
self.max_age == other.max_age
&& self.max_size == other.max_size
&& self.quantiles.len() == other.quantiles.len()
&& self
.quantiles
.iter()
.zip(other.quantiles.iter())
.all(|(a, b)| a.to_bits() == b.to_bits())
}
}
impl Eq for SummaryConfig {}
impl Hash for SummaryConfig {
fn hash<H: Hasher>(&self, state: &mut H) {
self.max_age.hash(state);
self.max_size.hash(state);
for q in self.quantiles.iter() {
q.to_bits().hash(state);
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum MetricKind {
Counter,
Gauge,
Histogram(Boundaries),
Summary(SummaryConfig),
Frequency,
}
impl MetricKind {
pub fn label(&self) -> &'static str {
match self {
Self::Counter => "counter",
Self::Gauge => "gauge",
Self::Histogram(_) => "histogram",
Self::Summary(_) => "summary",
Self::Frequency => "frequency",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct MetricKey {
name: Arc<str>,
kind: MetricKind,
tags: MetricTags,
}
impl MetricKey {
pub fn new(name: impl Into<Arc<str>>, kind: MetricKind) -> Self {
Self {
name: name.into(),
kind,
tags: Arc::from(Vec::new()),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn kind(&self) -> &MetricKind {
&self.kind
}
pub fn tags(&self) -> &[(String, String)] {
&self.tags
}
pub fn with_tags(&self, extra: &[(String, String)]) -> Self {
Self {
name: Arc::clone(&self.name),
kind: self.kind.clone(),
tags: merge_tags(&self.tags, extra),
}
}
}
fn cas_u64(a: &AtomicU64, f: impl Fn(u64) -> Option<u64>) {
let mut cur = a.load(Ordering::Relaxed);
while let Some(next) = f(cur) {
match a.compare_exchange_weak(cur, next, Ordering::Relaxed, Ordering::Relaxed) {
Ok(_) => return,
Err(seen) => cur = seen,
}
}
}
#[derive(Debug)]
struct AtomicF64(AtomicU64);
impl AtomicF64 {
fn new(v: f64) -> Self {
Self(AtomicU64::new(v.to_bits()))
}
fn get(&self) -> f64 {
f64::from_bits(self.0.load(Ordering::Relaxed))
}
fn update(&self, f: impl Fn(f64) -> Option<f64>) {
cas_u64(&self.0, |b| f(f64::from_bits(b)).map(f64::to_bits));
}
fn add(&self, v: f64) {
self.update(|cur| Some(cur + v));
}
fn min(&self, v: f64) {
self.update(|cur| (v < cur).then_some(v));
}
fn max(&self, v: f64) {
self.update(|cur| (v > cur).then_some(v));
}
}
#[derive(Debug)]
struct Moments {
count: AtomicU64,
sum: AtomicF64,
min: AtomicF64,
max: AtomicF64,
}
impl Moments {
fn new() -> Self {
Self {
count: AtomicU64::new(0),
sum: AtomicF64::new(0.0),
min: AtomicF64::new(f64::INFINITY),
max: AtomicF64::new(f64::NEG_INFINITY),
}
}
fn record(&self, v: f64) {
self.count.fetch_add(1, Ordering::Relaxed);
self.sum.add(v);
self.min.min(v);
self.max.max(v);
}
fn read(&self) -> (u64, f64, Option<f64>, Option<f64>) {
let count = self.count.load(Ordering::Relaxed);
if count == 0 {
(0, 0.0, None, None)
} else {
(
count,
self.sum.get(),
Some(self.min.get()),
Some(self.max.get()),
)
}
}
}
#[derive(Debug)]
pub(crate) struct CounterState(AtomicU64);
impl CounterState {
fn add(&self, n: u64) {
cas_u64(&self.0, |v| Some(v.saturating_add(n)));
}
}
#[derive(Debug)]
pub(crate) struct GaugeState(AtomicI64);
impl GaugeState {
fn adjust(&self, delta: i64) -> i64 {
let mut cur = self.0.load(Ordering::Relaxed);
loop {
let next = cur.saturating_add(delta);
match self
.0
.compare_exchange_weak(cur, next, Ordering::Relaxed, Ordering::Relaxed)
{
Ok(_) => return next,
Err(seen) => cur = seen,
}
}
}
}
#[derive(Debug)]
pub(crate) struct HistogramState {
bounds: Boundaries,
buckets: Box<[AtomicU64]>,
moments: Moments,
}
impl HistogramState {
fn new(bounds: Boundaries) -> Self {
let buckets = (0..=bounds.len()).map(|_| AtomicU64::new(0)).collect();
Self {
bounds,
buckets,
moments: Moments::new(),
}
}
fn observe(&self, v: f64) -> bool {
if v.is_nan() {
return false;
}
let i = self.bounds.as_slice().partition_point(|b| *b < v);
self.buckets[i].fetch_add(1, Ordering::Relaxed);
self.moments.record(v);
true
}
fn snapshot(&self) -> HistogramSnapshot {
let buckets: Vec<(f64, u64)> = self
.bounds
.as_slice()
.iter()
.copied()
.chain(std::iter::once(f64::INFINITY))
.zip(self.buckets.iter().map(|c| c.load(Ordering::Relaxed)))
.collect();
let (_, sum, min, max) = self.moments.read();
HistogramSnapshot {
count: buckets.iter().map(|(_, c)| c).sum(),
buckets,
sum,
min,
max,
}
}
}
#[derive(Debug)]
pub(crate) struct SummaryState {
config: SummaryConfig,
window: Mutex<VecDeque<(Instant, f64)>>,
moments: Moments,
}
impl SummaryState {
fn new(config: SummaryConfig) -> Self {
Self {
window: Mutex::new(VecDeque::with_capacity(config.max_size.min(1024))),
config,
moments: Moments::new(),
}
}
fn expired(&self, at: Instant, now: Instant) -> bool {
now.saturating_duration_since(at) > self.config.max_age
}
fn prune(&self, w: &mut VecDeque<(Instant, f64)>, now: Instant) {
while w.len() > self.config.max_size {
w.pop_front();
}
while w.front().is_some_and(|(at, _)| self.expired(*at, now)) {
w.pop_front();
}
}
fn observe(&self, v: f64, now: Instant) -> bool {
if v.is_nan() {
return false;
}
let mut w = self.window.lock().unwrap_or_else(PoisonError::into_inner);
w.push_back((now, v));
self.prune(&mut w, now);
drop(w);
self.moments.record(v);
true
}
fn snapshot(&self, now: Instant) -> SummarySnapshot {
let mut values: Vec<f64> = {
let mut w = self.window.lock().unwrap_or_else(PoisonError::into_inner);
self.prune(&mut w, now);
w.iter()
.filter(|(at, _)| !self.expired(*at, now))
.map(|(_, v)| *v)
.collect()
};
values.sort_by(f64::total_cmp);
let quantiles = self
.config
.quantiles
.iter()
.map(|&q| (q, nearest_rank(&values, q)))
.collect();
let (count, sum, min, max) = self.moments.read();
SummarySnapshot {
quantiles,
window_len: values.len(),
count,
sum,
min,
max,
}
}
}
fn nearest_rank(values: &[f64], q: f64) -> Option<f64> {
if values.is_empty() {
return None;
}
let rank = (q * values.len() as f64).ceil() as usize;
Some(values[rank.clamp(1, values.len()) - 1])
}
#[derive(Debug, Default)]
pub(crate) struct FrequencyState(RwLock<HashMap<String, AtomicU64>>);
impl FrequencyState {
fn increment(&self, key: &str) {
{
let m = self.0.read().unwrap_or_else(PoisonError::into_inner);
if let Some(c) = m.get(key) {
c.fetch_add(1, Ordering::Relaxed);
return;
}
}
let mut m = self.0.write().unwrap_or_else(PoisonError::into_inner);
m.entry(key.to_owned())
.or_default()
.fetch_add(1, Ordering::Relaxed);
}
fn snapshot(&self) -> Vec<(String, u64)> {
let m = self.0.read().unwrap_or_else(PoisonError::into_inner);
let mut v: Vec<(String, u64)> = m
.iter()
.map(|(k, c)| (k.clone(), c.load(Ordering::Relaxed)))
.collect();
drop(m);
v.sort();
v
}
}
#[derive(Clone, Debug)]
pub(crate) enum MetricCell {
Counter(Arc<CounterState>),
Gauge(Arc<GaugeState>),
Histogram(Arc<HistogramState>),
Summary(Arc<SummaryState>),
Frequency(Arc<FrequencyState>),
}
impl MetricCell {
pub(crate) fn for_kind(kind: &MetricKind) -> Self {
match kind {
MetricKind::Counter => Self::Counter(Arc::new(CounterState(AtomicU64::new(0)))),
MetricKind::Gauge => Self::Gauge(Arc::new(GaugeState(AtomicI64::new(0)))),
MetricKind::Histogram(b) => Self::Histogram(Arc::new(HistogramState::new(b.clone()))),
MetricKind::Summary(c) => Self::Summary(Arc::new(SummaryState::new(c.clone()))),
MetricKind::Frequency => Self::Frequency(Arc::default()),
}
}
pub(crate) fn snapshot(&self, now: Instant) -> MetricState {
match self {
Self::Counter(s) => MetricState::Counter {
count: s.0.load(Ordering::Relaxed),
},
Self::Gauge(s) => MetricState::Gauge {
value: s.0.load(Ordering::Relaxed),
},
Self::Histogram(s) => MetricState::Histogram(s.snapshot()),
Self::Summary(s) => MetricState::Summary(s.snapshot(now)),
Self::Frequency(s) => MetricState::Frequency {
occurrences: s.snapshot(),
},
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct HistogramSnapshot {
pub buckets: Vec<(f64, u64)>,
pub count: u64,
pub sum: f64,
pub min: Option<f64>,
pub max: Option<f64>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SummarySnapshot {
pub quantiles: Vec<(f64, Option<f64>)>,
pub window_len: usize,
pub count: u64,
pub sum: f64,
pub min: Option<f64>,
pub max: Option<f64>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum MetricState {
Counter {
count: u64,
},
Gauge {
value: i64,
},
Histogram(HistogramSnapshot),
Summary(SummarySnapshot),
Frequency {
occurrences: Vec<(String, u64)>,
},
}
fn metric_step<E, R>(
f: impl FnOnce(&[(String, String)], Instant) + Send + 'static,
) -> Effect<(), E, R>
where
E: Send + 'static,
R: Send + Sync + 'static,
{
Effect::from_instr(Instr::WithFiber(Box::new(move |_, fiber| {
let tags = MetricRegistry::fiber_tags().get_in(fiber);
let now = fiber.executor().now();
Instr::Sync(Box::new(move |_| {
f(&tags, now);
Instr::Pure(Ok(erase(())))
}))
})))
}
fn first_fail<E>(c: &Cause<E>) -> Option<&E> {
match c {
Cause::Fail(e) => Some(e),
Cause::Both(l, r) | Cause::Then(l, r) => first_fail(l).or_else(|| first_fail(r)),
_ => None,
}
}
pub trait Metric: Clone + Send + Sync + 'static {
type Input: Send + 'static;
fn key(&self) -> &MetricKey;
fn registry(&self) -> &MetricRegistry;
fn tagged_all(&self, tags: &[(String, String)]) -> Self;
fn record(&self, input: Self::Input, now: Instant);
fn state(&self) -> MetricState;
fn name(&self) -> &str {
self.key().name()
}
fn tags(&self) -> &[(String, String)] {
self.key().tags()
}
fn tagged(&self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.tagged_all(&[(key.into(), value.into())])
}
fn update_now(&self, input: Self::Input) {
self.record(input, Instant::now());
}
fn update<E, R>(&self, input: Self::Input) -> Effect<(), E, R>
where
E: Send + 'static,
R: Send + Sync + 'static,
{
let m = self.clone();
metric_step(move |tags, now| m.record_tagged(tags, input, now))
}
fn record_tagged(&self, fiber_tags: &[(String, String)], input: Self::Input, now: Instant) {
if fiber_tags.is_empty() {
self.record(input, now);
} else {
self.tagged_all(fiber_tags).record(input, now);
}
}
fn track_exit_with<A, E, R>(
&self,
eff: Effect<A, E, R>,
f: impl FnOnce(ExitKind) -> Option<Self::Input> + Send + 'static,
) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
let m = self.clone();
observe_exit_with_fiber(eff, move |exit, fiber| {
if let Some(input) = f(ExitKind::of_result(exit)) {
let tags = MetricRegistry::fiber_tags().get_in(fiber);
m.record_tagged(&tags, input, fiber.executor().now());
}
})
}
fn track_all<A, E, R>(&self, input: Self::Input, eff: Effect<A, E, R>) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
self.track_exit_with(eff, move |_| Some(input))
}
fn track_success_with<A, E, R>(
&self,
eff: Effect<A, E, R>,
f: impl FnOnce(&A) -> Self::Input + Send + 'static,
) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
let m = self.clone();
observe_exit_with_fiber(eff, move |exit, fiber| {
if let Ok(a) = exit {
let tags = MetricRegistry::fiber_tags().get_in(fiber);
m.record_tagged(&tags, f(a), fiber.executor().now());
}
})
}
fn track<E, R>(&self, eff: Effect<Self::Input, E, R>) -> Effect<Self::Input, E, R>
where
Self::Input: Clone,
E: Send + 'static,
R: Send + Sync + 'static,
{
self.track_success_with(eff, Clone::clone)
}
fn track_error_with<A, E, R>(
&self,
eff: Effect<A, E, R>,
f: impl FnOnce(&E) -> Self::Input + Send + 'static,
) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
let m = self.clone();
observe_exit_with_fiber(eff, move |exit, fiber| {
if let Some(e) = exit.as_ref().err().and_then(first_fail) {
let tags = MetricRegistry::fiber_tags().get_in(fiber);
m.record_tagged(&tags, f(e), fiber.executor().now());
}
})
}
fn track_duration_with<A, E, R>(
&self,
eff: Effect<A, E, R>,
f: impl FnOnce(Duration) -> Self::Input + Send + 'static,
) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
let m = self.clone();
Effect::from_instr(Instr::WithFiber(Box::new(move |_, fiber| {
let start = fiber.executor().now();
observe_exit_with_fiber(eff, move |_, fiber| {
let now = fiber.executor().now();
let tags = MetricRegistry::fiber_tags().get_in(fiber);
m.record_tagged(&tags, f(now.saturating_duration_since(start)), now);
})
.into_instr()
})))
}
}
macro_rules! handle_common {
($ty:ident, $variant:ident, $state:ty) => {
impl $ty {
pub(crate) fn from_key(registry: &MetricRegistry, key: MetricKey) -> Self {
Self {
key,
state: Arc::new(OnceLock::new()),
registry: registry.clone(),
}
}
fn st(&self) -> &$state {
self
.state
.get_or_init(|| match self.registry.cell(&self.key) {
MetricCell::$variant(state) => state,
_ => unreachable!("id_effect: metric key kind and cell kind disagree"),
})
}
}
impl std::fmt::Debug for $ty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(stringify!($ty))
.field("name", &self.key.name())
.field("tags", &self.key.tags())
.finish()
}
}
};
}
macro_rules! metric_impl {
($ty:ident, $input:ty, |$this:ident, $inp:ident, $now:ident| $record:block) => {
impl Metric for $ty {
type Input = $input;
fn key(&self) -> &MetricKey {
&self.key
}
fn registry(&self) -> &MetricRegistry {
&self.registry
}
fn tagged_all(&self, tags: &[(String, String)]) -> Self {
Self::from_key(&self.registry, self.key.with_tags(tags))
}
fn record(&self, $inp: $input, $now: Instant) {
let $this = self;
$record
}
fn state(&self) -> MetricState {
self.registry.cell(&self.key).snapshot(Instant::now())
}
}
};
}
#[derive(Clone)]
pub struct Counter {
key: MetricKey,
state: Arc<OnceLock<Arc<CounterState>>>,
registry: MetricRegistry,
}
handle_common!(Counter, Counter, CounterState);
metric_impl!(Counter, u64, |this, n, _now| {
this.st().add(n);
this.registry.notify(&this.key, MetricUpdate::Counter(n));
});
impl Counter {
pub fn new(name: impl Into<Arc<str>>) -> Self {
MetricRegistry::global().counter(name)
}
pub fn count(&self) -> u64 {
self.st().0.load(Ordering::Relaxed)
}
pub fn increment<E: Send + 'static, R: Send + Sync + 'static>(&self) -> Effect<(), E, R> {
self.update(1)
}
pub fn increment_by<E: Send + 'static, R: Send + Sync + 'static>(
&self,
n: u64,
) -> Effect<(), E, R> {
self.update(n)
}
}
#[derive(Clone)]
pub struct Gauge {
key: MetricKey,
state: Arc<OnceLock<Arc<GaugeState>>>,
registry: MetricRegistry,
}
handle_common!(Gauge, Gauge, GaugeState);
metric_impl!(Gauge, i64, |this, v, _now| {
this.st().0.store(v, Ordering::Relaxed);
this.registry.notify(&this.key, MetricUpdate::Gauge(v));
});
impl Gauge {
pub fn new(name: impl Into<Arc<str>>) -> Self {
MetricRegistry::global().gauge(name)
}
pub fn value(&self) -> i64 {
self.st().0.load(Ordering::Relaxed)
}
pub fn set<E: Send + 'static, R: Send + Sync + 'static>(&self, v: i64) -> Effect<(), E, R> {
self.update(v)
}
pub fn adjust_now(&self, delta: i64) -> i64 {
let v = self.st().adjust(delta);
self.registry.notify(&self.key, MetricUpdate::Gauge(v));
v
}
pub fn adjust<E: Send + 'static, R: Send + Sync + 'static>(
&self,
delta: i64,
) -> Effect<(), E, R> {
let m = self.clone();
metric_step(move |tags, _| {
if tags.is_empty() {
m.adjust_now(delta);
} else {
m.tagged_all(tags).adjust_now(delta);
}
})
}
}
#[derive(Clone)]
pub struct Histogram {
key: MetricKey,
state: Arc<OnceLock<Arc<HistogramState>>>,
registry: MetricRegistry,
}
handle_common!(Histogram, Histogram, HistogramState);
metric_impl!(Histogram, f64, |this, v, _now| {
if this.st().observe(v) {
this.registry.notify(&this.key, MetricUpdate::Histogram(v));
}
});
impl Histogram {
pub fn new(name: impl Into<Arc<str>>, boundaries: Boundaries) -> Self {
MetricRegistry::global().histogram(name, boundaries)
}
pub fn boundaries(&self) -> &Boundaries {
&self.st().bounds
}
pub fn snapshot(&self) -> HistogramSnapshot {
self.st().snapshot()
}
pub fn observe<E: Send + 'static, R: Send + Sync + 'static>(&self, v: f64) -> Effect<(), E, R> {
self.update(v)
}
pub fn track_duration<A, E, R>(&self, eff: Effect<A, E, R>) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
self.track_duration_with(eff, |d| d.as_secs_f64())
}
}
#[derive(Clone)]
pub struct Summary {
key: MetricKey,
state: Arc<OnceLock<Arc<SummaryState>>>,
registry: MetricRegistry,
}
handle_common!(Summary, Summary, SummaryState);
metric_impl!(Summary, f64, |this, v, now| {
if this.st().observe(v, now) {
this.registry.notify(&this.key, MetricUpdate::Summary(v));
}
});
impl Summary {
pub fn new(name: impl Into<Arc<str>>, config: SummaryConfig) -> Self {
MetricRegistry::global().summary(name, config)
}
pub fn config(&self) -> &SummaryConfig {
&self.st().config
}
pub fn snapshot_at(&self, now: Instant) -> SummarySnapshot {
self.st().snapshot(now)
}
pub fn snapshot(&self) -> SummarySnapshot {
self.snapshot_at(Instant::now())
}
pub fn observe<E: Send + 'static, R: Send + Sync + 'static>(&self, v: f64) -> Effect<(), E, R> {
self.update(v)
}
pub fn track_duration<A, E, R>(&self, eff: Effect<A, E, R>) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
self.track_duration_with(eff, |d| d.as_secs_f64())
}
}
#[derive(Clone)]
pub struct Frequency {
key: MetricKey,
state: Arc<OnceLock<Arc<FrequencyState>>>,
registry: MetricRegistry,
}
handle_common!(Frequency, Frequency, FrequencyState);
metric_impl!(Frequency, String, |this, v, _now| {
this.st().increment(&v);
this.registry.notify(&this.key, MetricUpdate::Frequency(&v));
});
impl Frequency {
pub fn new(name: impl Into<Arc<str>>) -> Self {
MetricRegistry::global().frequency(name)
}
pub fn occurrences(&self) -> Vec<(String, u64)> {
self.st().snapshot()
}
pub fn occur<E: Send + 'static, R: Send + Sync + 'static>(
&self,
value: impl Into<String>,
) -> Effect<(), E, R> {
self.update(value.into())
}
}