Skip to content

Commit

Permalink
Add prom metrics to cached directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvolp12 committed Sep 23, 2023
1 parent fc845bf commit 1677525
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions atproto/identity/cache_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 1677525

Please sign in to comment.