From 01564ffd5233ab8aa2238f131f9d8b14d76a2bf0 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Sun, 21 Jun 2026 17:19:01 +0200 Subject: [PATCH] feat: observe jikan cache stats --- integrations/jikan/cache/store.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/integrations/jikan/cache/store.go b/integrations/jikan/cache/store.go index a90058f..3dd8fe7 100644 --- a/integrations/jikan/cache/store.go +++ b/integrations/jikan/cache/store.go @@ -22,6 +22,7 @@ func NewStore(queries db.Querier, metrics *observability.Metrics) *Store { func (s *Store) Get(parentCtx context.Context, key string, out any) bool { ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second) defer cancel() + defer s.observeStats(parentCtx) data, err := s.db.GetJikanCache(ctx, key) if err != nil { @@ -42,6 +43,7 @@ func (s *Store) Get(parentCtx context.Context, key string, out any) bool { func (s *Store) GetStale(parentCtx context.Context, key string, out any) bool { ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second) defer cancel() + defer s.observeStats(parentCtx) data, err := s.db.GetJikanCacheStale(ctx, key) if err != nil { @@ -62,6 +64,7 @@ func (s *Store) GetStale(parentCtx context.Context, key string, out any) bool { func (s *Store) Set(parentCtx context.Context, key string, data any, ttl time.Duration) { ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second) defer cancel() + defer s.observeStats(parentCtx) bytes, err := json.Marshal(data) if err != nil { @@ -84,3 +87,22 @@ func (s *Store) Set(parentCtx context.Context, key string, data any, ttl time.Du ) } } + +func (s *Store) observeStats(parentCtx context.Context) { + ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second) + defer cancel() + + stats, err := s.db.GetJikanCacheStats(ctx) + if err != nil { + observability.LogJSON( + observability.LogLevelWarn, + "jikan_cache_stats", + "jikan", + "", + nil, + err, + ) + return + } + s.metrics.ObserveJikanCacheStats(stats.TotalRows, stats.ExpiredRows, stats.OldestExpiresAtSeconds) +}