feat: observe jikan cache stats

This commit is contained in:
2026-06-21 17:19:01 +02:00
committed by Milas Holsting
parent 1250c591b7
commit 01564ffd52

View File

@@ -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 { func (s *Store) Get(parentCtx context.Context, key string, out any) bool {
ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second) ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second)
defer cancel() defer cancel()
defer s.observeStats(parentCtx)
data, err := s.db.GetJikanCache(ctx, key) data, err := s.db.GetJikanCache(ctx, key)
if err != nil { 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 { func (s *Store) GetStale(parentCtx context.Context, key string, out any) bool {
ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second) ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second)
defer cancel() defer cancel()
defer s.observeStats(parentCtx)
data, err := s.db.GetJikanCacheStale(ctx, key) data, err := s.db.GetJikanCacheStale(ctx, key)
if err != nil { 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) { func (s *Store) Set(parentCtx context.Context, key string, data any, ttl time.Duration) {
ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second) ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second)
defer cancel() defer cancel()
defer s.observeStats(parentCtx)
bytes, err := json.Marshal(data) bytes, err := json.Marshal(data)
if err != nil { 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)
}