Skip to content

Commit

Permalink
Add metrics on cache eviction_listener
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-cattermole committed May 14, 2024
1 parent dd73fc3 commit 055eef4
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion limitador/src/storage/redis/counters_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::storage::redis::DEFAULT_MAX_CACHED_COUNTERS;
use dashmap::mapref::entry::Entry;
use dashmap::DashMap;
use metrics::{gauge, histogram};
use moka::notification::RemovalCause;
use moka::sync::Cache;
use std::collections::HashMap;
use std::future::Future;
Expand Down Expand Up @@ -309,9 +310,23 @@ impl CountersCacheBuilder {
self
}

fn eviction_listener(
_key: Arc<Counter>,
value: Arc<CachedCounterValue>,
_removal_cause: RemovalCause,
) {
gauge!("cache_size").decrement(1);
if let Ok(writes) = value.pending_writes() {
histogram!("evicted_pending_writes").record(writes as f64);
}
}

pub fn build(&self, period: Duration) -> CountersCache {
CountersCache {
cache: Cache::new(self.max_cached_counters as u64),
cache: Cache::builder()
.max_capacity(self.max_cached_counters as u64)
.eviction_listener(Self::eviction_listener)
.build(),
batcher: Batcher::new(period),
}
}
Expand Down

0 comments on commit 055eef4

Please sign in to comment.