Skip to content

Commit

Permalink
fix(feed): emit cache metrics of the differences from the last time
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 22, 2024
1 parent a182914 commit 5ea57af
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions crates/synd_feed/src/feed/cache/periodic_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ impl<S> PeriodicRefresher<S> {
}
}

fn emit_metrics(&self) {
fn emit_metrics(&self, prev: &Metrics) -> Metrics {
// Should call cache.run_pending_tasks() ?
let entry_count = self.cache.entry_count();
let size = self.cache.weighted_size();
let current = Metrics {
cache_count: self.cache.entry_count().try_into().unwrap_or(0),
cache_size: self.cache.weighted_size().try_into().unwrap_or(0),
};

metric!(counter.cache.feed.count = entry_count);
metric!(counter.cache.feed.size = size);
metric!(counter.cache.feed.count = current.cache_count - prev.cache_count);
metric!(
counter.cache.feed.size = current.cache_size,
prev.cache_size
);

current
}
}

Expand Down Expand Up @@ -68,6 +75,7 @@ where
info!(?interval, "Run periodic feed cache refresher");

let mut interval = tokio::time::interval(interval);
let mut prev = Metrics::default();

// Consume initial tick which return ready immediately
interval.tick().await;
Expand All @@ -76,7 +84,7 @@ where
interval.tick().await;

if self.emit_metrics {
self.emit_metrics();
prev = self.emit_metrics(&prev);
}
if let Err(err) = self.refresh().await {
error!("Periodic refresh error: {err}");
Expand All @@ -86,3 +94,9 @@ where
}
}
}

#[derive(Default)]
struct Metrics {
cache_count: i64,
cache_size: i64,
}

0 comments on commit 5ea57af

Please sign in to comment.