feat: observe jikan cache stats
This commit is contained in:
22
integrations/jikan/cache/store.go
vendored
22
integrations/jikan/cache/store.go
vendored
@@ -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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user