Skip to content

Commit

Permalink
Add a new relay metric to more accurately describe cache behavior. (L…
Browse files Browse the repository at this point in the history
…ayr-Labs#1092)

Signed-off-by: Cody Littley <cody@eigenlabs.org>
  • Loading branch information
cody-littley authored Jan 9, 2025
1 parent e8d2311 commit 7728d52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion relay/cache/cache_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ func (c *cacheAccessor[K, V]) Get(ctx context.Context, key K) (V, error) {
c.cacheLock.Unlock()

if c.metrics != nil {
c.metrics.ReportCacheMiss()
if alreadyLoading {
// A lookup is currently in progress. Not a cache hit, but this call won't duplicate the work.
c.metrics.ReportCacheNearMiss()
} else {
// The data is not in the cache and no lookup is in progress. We must fetch the data from the source.
c.metrics.ReportCacheMiss()
}
}

if alreadyLoading {
Expand Down
15 changes: 15 additions & 0 deletions relay/cache/cache_accessor_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const namespace = "eigenda_relay"
// CacheAccessorMetrics provides metrics for a CacheAccessor.
type CacheAccessorMetrics struct {
cacheHits *prometheus.CounterVec
cacheNearMisses *prometheus.CounterVec
cacheMisses *prometheus.CounterVec
size *prometheus.GaugeVec
weight *prometheus.GaugeVec
Expand All @@ -34,6 +35,15 @@ func NewCacheAccessorMetrics(
[]string{},
)

cacheNearMisses := promauto.With(registry).NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: fmt.Sprintf("%s_cache_near_miss_count", cacheName),
Help: "Number of near cache misses (i.e. a lookup is already in progress)",
},
[]string{},
)

cacheMisses := promauto.With(registry).NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Expand Down Expand Up @@ -82,6 +92,7 @@ func NewCacheAccessorMetrics(

return &CacheAccessorMetrics{
cacheHits: cacheHits,
cacheNearMisses: cacheNearMisses,
cacheMisses: cacheMisses,
size: size,
weight: weight,
Expand All @@ -94,6 +105,10 @@ func (m *CacheAccessorMetrics) ReportCacheHit() {
m.cacheHits.WithLabelValues().Inc()
}

func (m *CacheAccessorMetrics) ReportCacheNearMiss() {
m.cacheNearMisses.WithLabelValues().Inc()
}

func (m *CacheAccessorMetrics) ReportCacheMiss() {
m.cacheMisses.WithLabelValues().Inc()
}
Expand Down

0 comments on commit 7728d52

Please sign in to comment.