diff --git a/atproto/identity/cache_directory.go b/atproto/identity/cache_directory.go index 5cecb66aa..1e4a78542 100644 --- a/atproto/identity/cache_directory.go +++ b/atproto/identity/cache_directory.go @@ -6,6 +6,8 @@ import ( "time" "github.com/bluesky-social/indigo/atproto/syntax" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/hashicorp/golang-lru/v2/expirable" ) @@ -29,6 +31,26 @@ type IdentityEntry struct { Err error } +var handleCacheHits = promauto.NewCounter(prometheus.CounterOpts{ + Name: "atproto_directory_handle_cache_hits", + Help: "Number of cache hits for ATProto handle lookups", +}) + +var handleCacheMisses = promauto.NewCounter(prometheus.CounterOpts{ + Name: "atproto_directory_handle_cache_misses", + Help: "Number of cache misses for ATProto handle lookups", +}) + +var identityCacheHits = promauto.NewCounter(prometheus.CounterOpts{ + Name: "atproto_directory_identity_cache_hits", + Help: "Number of cache hits for ATProto identity lookups", +}) + +var identityCacheMisses = promauto.NewCounter(prometheus.CounterOpts{ + Name: "atproto_directory_identity_cache_misses", + Help: "Number of cache misses for ATProto identity lookups", +}) + var _ Directory = (*CacheDirectory)(nil) // Capacity of zero means unlimited size. Similarly, ttl of zero means unlimited duration. @@ -89,6 +111,7 @@ func (d *CacheDirectory) ResolveHandle(ctx context.Context, h syntax.Handle) (sy maybeEntry, ok := d.handleCache.Get(h.String()) if !ok { + handleCacheMisses.Inc() entry, err = d.updateHandle(ctx, h) if err != nil { return "", err @@ -97,10 +120,13 @@ func (d *CacheDirectory) ResolveHandle(ctx context.Context, h syntax.Handle) (sy entry = &maybeEntry } if d.IsHandleStale(entry) { + handleCacheMisses.Inc() entry, err = d.updateHandle(ctx, h) if err != nil { return "", err } + } else { + handleCacheHits.Inc() } return entry.DID, entry.Err } @@ -136,6 +162,7 @@ func (d *CacheDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identi maybeEntry, ok := d.identityCache.Get(did.String()) if !ok { + identityCacheMisses.Inc() entry, err = d.updateDID(ctx, did) if err != nil { return nil, err @@ -144,10 +171,13 @@ func (d *CacheDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identi entry = &maybeEntry } if d.IsIdentityStale(entry) { + identityCacheMisses.Inc() entry, err = d.updateDID(ctx, did) if err != nil { return nil, err } + } else { + identityCacheHits.Inc() } return entry.Identity, entry.Err }