Skip to content

Commit dfdfce8

Browse files
committed
Add metric for "age at time of eviction"
1 parent 9e6ec0d commit dfdfce8

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/s3_cache.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ impl S3Cache {
310310
shard.size.fetch_sub(evicted_size, Ordering::Relaxed);
311311
self.global_size.fetch_sub(evicted_size, Ordering::Relaxed);
312312
telemetry::record_cache_eviction(evicted_size as u64);
313+
let age = evicted_value.inserted_at().elapsed().min(self.ttl);
314+
telemetry::record_cache_eviction_age(age.as_secs_f64());
313315
}
314316

315317
// Drop own shard's write lock before touching other shards.
@@ -373,6 +375,8 @@ impl S3Cache {
373375
target.size.fetch_sub(evicted_size, Ordering::Relaxed);
374376
self.global_size.fetch_sub(evicted_size, Ordering::Relaxed);
375377
telemetry::record_cache_eviction(evicted_size as u64);
378+
let age = evicted_value.inserted_at().elapsed().min(self.ttl);
379+
telemetry::record_cache_eviction_age(age.as_secs_f64());
376380
}
377381
}
378382

src/telemetry.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ const OVERSIZED_OBJECT_SIZE_BUCKETS: &[f64] = &[
5050
1_000_000_000.0, // 1000000 KiB
5151
];
5252

53+
const EVICTION_AGE_BUCKETS: &[f64] = &[
54+
60.0, // 1 min
55+
300.0, // 5 min
56+
600.0, // 10 min
57+
1_800.0, // 30 min
58+
3_600.0, // 1 h
59+
7_200.0, // 2 h
60+
14_400.0, // 4 h
61+
28_800.0, // 8 h
62+
86_400.0, // 1 day
63+
172_800.0, // 2 days
64+
604_800.0, // 1 week
65+
1_209_600.0, // 2 weeks
66+
2_592_000.0, // 1 month
67+
5_184_000.0, // 2 months
68+
7_776_000.0, // 3 months
69+
15_552_000.0, // 6 months
70+
31_536_000.0, // 1 year
71+
];
72+
5373
pub(crate) fn initialize_telemetry(
5474
config: &Config,
5575
) -> crate::Result<(
@@ -301,6 +321,37 @@ pub(crate) fn record_cache_eviction(bytes: u64) {
301321
PROM_CACHE_EVICTION_BYTES_TOTAL.inc_by(bytes);
302322
}
303323

324+
// MARK: Eviction Age
325+
326+
pub(crate) fn record_cache_eviction_age(age_secs: f64) {
327+
static CACHE_EVICTION_AGE_HISTOGRAM: LazyLock<Histogram<f64>> = LazyLock::new(|| {
328+
opentelemetry::global::meter(CARGO_CRATE_NAME)
329+
.f64_histogram("cache.eviction_age_histogram")
330+
.with_description("Age of objects (in seconds) at the time of eviction, capped at TTL")
331+
.with_unit("s")
332+
.build()
333+
});
334+
335+
static PROM_CACHE_EVICTION_AGE_HISTOGRAM: LazyLock<prometheus::Histogram> =
336+
LazyLock::new(|| {
337+
let histogram = prometheus::Histogram::with_opts(
338+
HistogramOpts::new(
339+
"cache_eviction_age_histogram",
340+
"Age of objects (in seconds) at the time of eviction, capped at TTL",
341+
)
342+
.buckets(EVICTION_AGE_BUCKETS.to_vec()),
343+
)
344+
.unwrap();
345+
PROMETHEUS_REGISTRY
346+
.register(Box::new(histogram.clone()))
347+
.unwrap();
348+
histogram
349+
});
350+
351+
CACHE_EVICTION_AGE_HISTOGRAM.record(age_secs, &[]);
352+
PROM_CACHE_EVICTION_AGE_HISTOGRAM.observe(age_secs);
353+
}
354+
304355
// MARK: Oversized Objects
305356

306357
pub(crate) fn record_cache_oversized(bytes: u64) {

0 commit comments

Comments
 (0)