From 055eef40894b43c59841f0cfdd27279edd0099bf Mon Sep 17 00:00:00 2001 From: Adam Cattermole Date: Tue, 14 May 2024 13:53:17 +0100 Subject: [PATCH] Add metrics on cache eviction_listener --- limitador/src/storage/redis/counters_cache.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/limitador/src/storage/redis/counters_cache.rs b/limitador/src/storage/redis/counters_cache.rs index 3bc1fc40..c1e14cf9 100644 --- a/limitador/src/storage/redis/counters_cache.rs +++ b/limitador/src/storage/redis/counters_cache.rs @@ -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; @@ -309,9 +310,23 @@ impl CountersCacheBuilder { self } + fn eviction_listener( + _key: Arc, + value: Arc, + _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), } }