Skip to content

Commit ea848b6

Browse files
authored
Create cortex_ingester_expanded_postings_cache_miss metric (#6455)
* Create metric Signed-off-by: alanprot <[email protected]> * Adding total on all metric Signed-off-by: alanprot <[email protected]> --------- Signed-off-by: alanprot <[email protected]>
1 parent ba6b14a commit ea848b6

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

pkg/ingester/ingester_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5553,22 +5553,22 @@ func TestExpendedPostingsCache(t *testing.T) {
55535553

55545554
if c.expectedHeadPostingCall > 0 || c.expectedBlockPostingCall > 0 {
55555555
metric := `
5556-
# HELP cortex_ingester_expanded_postings_cache_requests Total number of requests to the cache.
5557-
# TYPE cortex_ingester_expanded_postings_cache_requests counter
5556+
# HELP cortex_ingester_expanded_postings_cache_requests_total Total number of requests to the cache.
5557+
# TYPE cortex_ingester_expanded_postings_cache_requests_total counter
55585558
`
55595559
if c.expectedBlockPostingCall > 0 {
55605560
metric += `
5561-
cortex_ingester_expanded_postings_cache_requests{cache="block"} 4
5561+
cortex_ingester_expanded_postings_cache_requests_total{cache="block"} 4
55625562
`
55635563
}
55645564

55655565
if c.expectedHeadPostingCall > 0 {
55665566
metric += `
5567-
cortex_ingester_expanded_postings_cache_requests{cache="head"} 4
5567+
cortex_ingester_expanded_postings_cache_requests_total{cache="head"} 4
55685568
`
55695569
}
55705570

5571-
err = testutil.GatherAndCompare(r, bytes.NewBufferString(metric), "cortex_ingester_expanded_postings_cache_requests")
5571+
err = testutil.GatherAndCompare(r, bytes.NewBufferString(metric), "cortex_ingester_expanded_postings_cache_requests_total")
55725572
require.NoError(t, err)
55735573
}
55745574

@@ -5583,22 +5583,22 @@ func TestExpendedPostingsCache(t *testing.T) {
55835583

55845584
if c.expectedHeadPostingCall > 0 || c.expectedBlockPostingCall > 0 {
55855585
metric := `
5586-
# HELP cortex_ingester_expanded_postings_cache_hits Total number of hit requests to the cache.
5587-
# TYPE cortex_ingester_expanded_postings_cache_hits counter
5586+
# HELP cortex_ingester_expanded_postings_cache_hits_total Total number of hit requests to the cache.
5587+
# TYPE cortex_ingester_expanded_postings_cache_hits_total counter
55885588
`
55895589
if c.expectedBlockPostingCall > 0 {
55905590
metric += `
5591-
cortex_ingester_expanded_postings_cache_hits{cache="block"} 4
5591+
cortex_ingester_expanded_postings_cache_hits_total{cache="block"} 4
55925592
`
55935593
}
55945594

55955595
if c.expectedHeadPostingCall > 0 {
55965596
metric += `
5597-
cortex_ingester_expanded_postings_cache_hits{cache="head"} 4
5597+
cortex_ingester_expanded_postings_cache_hits_total{cache="head"} 4
55985598
`
55995599
}
56005600

5601-
err = testutil.GatherAndCompare(r, bytes.NewBufferString(metric), "cortex_ingester_expanded_postings_cache_hits")
5601+
err = testutil.GatherAndCompare(r, bytes.NewBufferString(metric), "cortex_ingester_expanded_postings_cache_hits_total")
56025602
require.NoError(t, err)
56035603
}
56045604

@@ -5644,10 +5644,10 @@ func TestExpendedPostingsCache(t *testing.T) {
56445644
require.Equal(t, postingsForMatchersCalls.Load(), int64(c.expectedBlockPostingCall))
56455645
if c.cacheConfig.Head.Enabled {
56465646
err = testutil.GatherAndCompare(r, bytes.NewBufferString(`
5647-
# HELP cortex_ingester_expanded_postings_non_cacheable_queries Total number of non cacheable queries.
5648-
# TYPE cortex_ingester_expanded_postings_non_cacheable_queries counter
5649-
cortex_ingester_expanded_postings_non_cacheable_queries{cache="head"} 1
5650-
`), "cortex_ingester_expanded_postings_non_cacheable_queries")
5647+
# HELP cortex_ingester_expanded_postings_non_cacheable_queries_total Total number of non cacheable queries.
5648+
# TYPE cortex_ingester_expanded_postings_non_cacheable_queries_total counter
5649+
cortex_ingester_expanded_postings_non_cacheable_queries_total{cache="head"} 1
5650+
`), "cortex_ingester_expanded_postings_non_cacheable_queries_total")
56515651
require.NoError(t, err)
56525652
}
56535653

pkg/storage/tsdb/expanded_postings_cache.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,30 @@ type ExpandedPostingsCacheMetrics struct {
4040
CacheRequests *prometheus.CounterVec
4141
CacheHits *prometheus.CounterVec
4242
CacheEvicts *prometheus.CounterVec
43+
CacheMiss *prometheus.CounterVec
4344
NonCacheableQueries *prometheus.CounterVec
4445
}
4546

4647
func NewPostingCacheMetrics(r prometheus.Registerer) *ExpandedPostingsCacheMetrics {
4748
return &ExpandedPostingsCacheMetrics{
4849
CacheRequests: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
49-
Name: "cortex_ingester_expanded_postings_cache_requests",
50+
Name: "cortex_ingester_expanded_postings_cache_requests_total",
5051
Help: "Total number of requests to the cache.",
5152
}, []string{"cache"}),
5253
CacheHits: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
53-
Name: "cortex_ingester_expanded_postings_cache_hits",
54+
Name: "cortex_ingester_expanded_postings_cache_hits_total",
5455
Help: "Total number of hit requests to the cache.",
5556
}, []string{"cache"}),
57+
CacheMiss: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
58+
Name: "cortex_ingester_expanded_postings_cache_miss_total",
59+
Help: "Total number of miss requests to the cache.",
60+
}, []string{"cache", "reason"}),
5661
CacheEvicts: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
57-
Name: "cortex_ingester_expanded_postings_cache_evicts",
62+
Name: "cortex_ingester_expanded_postings_cache_evicts_total",
5863
Help: "Total number of evictions in the cache, excluding items that got evicted due to TTL.",
5964
}, []string{"cache", "reason"}),
6065
NonCacheableQueries: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
61-
Name: "cortex_ingester_expanded_postings_non_cacheable_queries",
66+
Name: "cortex_ingester_expanded_postings_non_cacheable_queries_total",
6267
Help: "Total number of non cacheable queries.",
6368
}, []string{"cache"}),
6469
}
@@ -374,6 +379,7 @@ func (c *fifoCache[V]) getPromiseForKey(k string, fetch func() (V, int64, error)
374379
loaded, ok := c.cachedValues.LoadOrStore(k, r)
375380

376381
if !ok {
382+
c.metrics.CacheMiss.WithLabelValues(c.name, "miss").Inc()
377383
r.v, r.sizeBytes, r.err = fetch()
378384
r.sizeBytes += int64(len(k))
379385
r.ts = c.timeNow()
@@ -387,7 +393,7 @@ func (c *fifoCache[V]) getPromiseForKey(k string, fetch func() (V, int64, error)
387393

388394
// If is cached but is expired, lets try to replace the cache value.
389395
if loaded.(*cacheEntryPromise[V]).isExpired(c.cfg.Ttl, c.timeNow()) && c.cachedValues.CompareAndSwap(k, loaded, r) {
390-
c.metrics.CacheEvicts.WithLabelValues(c.name, "expired").Inc()
396+
c.metrics.CacheMiss.WithLabelValues(c.name, "expired").Inc()
391397
r.v, r.sizeBytes, r.err = fetch()
392398
r.sizeBytes += int64(len(k))
393399
c.updateSize(loaded.(*cacheEntryPromise[V]).sizeBytes, r.sizeBytes)

pkg/storage/tsdb/expanded_postings_cache_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ func TestFifoCacheExpire(t *testing.T) {
154154

155155
if c.expectedFinalItems != numberOfKeys {
156156
err := testutil.GatherAndCompare(r, bytes.NewBufferString(fmt.Sprintf(`
157-
# HELP cortex_ingester_expanded_postings_cache_evicts Total number of evictions in the cache, excluding items that got evicted due to TTL.
158-
# TYPE cortex_ingester_expanded_postings_cache_evicts counter
159-
cortex_ingester_expanded_postings_cache_evicts{cache="test",reason="full"} %v
160-
`, numberOfKeys-c.expectedFinalItems)), "cortex_ingester_expanded_postings_cache_evicts")
157+
# HELP cortex_ingester_expanded_postings_cache_evicts_total Total number of evictions in the cache, excluding items that got evicted due to TTL.
158+
# TYPE cortex_ingester_expanded_postings_cache_evicts_total counter
159+
cortex_ingester_expanded_postings_cache_evicts_total{cache="test",reason="full"} %v
160+
`, numberOfKeys-c.expectedFinalItems)), "cortex_ingester_expanded_postings_cache_evicts_total")
161161
require.NoError(t, err)
162162

163163
}
@@ -181,10 +181,11 @@ func TestFifoCacheExpire(t *testing.T) {
181181
}
182182

183183
err := testutil.GatherAndCompare(r, bytes.NewBufferString(fmt.Sprintf(`
184-
# HELP cortex_ingester_expanded_postings_cache_evicts Total number of evictions in the cache, excluding items that got evicted due to TTL.
185-
# TYPE cortex_ingester_expanded_postings_cache_evicts counter
186-
cortex_ingester_expanded_postings_cache_evicts{cache="test",reason="expired"} %v
187-
`, numberOfKeys)), "cortex_ingester_expanded_postings_cache_evicts")
184+
# HELP cortex_ingester_expanded_postings_cache_miss_total Total number of miss requests to the cache.
185+
# TYPE cortex_ingester_expanded_postings_cache_miss_total counter
186+
cortex_ingester_expanded_postings_cache_miss_total{cache="test",reason="expired"} %v
187+
cortex_ingester_expanded_postings_cache_miss_total{cache="test",reason="miss"} %v
188+
`, numberOfKeys, numberOfKeys)), "cortex_ingester_expanded_postings_cache_miss_total")
188189
require.NoError(t, err)
189190

190191
cache.timeNow = func() time.Time {
@@ -195,12 +196,12 @@ func TestFifoCacheExpire(t *testing.T) {
195196
return 2, 18, nil
196197
})
197198

198-
// Should expire all keys again as ttl is expired
199+
// Should expire all keys expired keys
199200
err = testutil.GatherAndCompare(r, bytes.NewBufferString(fmt.Sprintf(`
200-
# HELP cortex_ingester_expanded_postings_cache_evicts Total number of evictions in the cache, excluding items that got evicted due to TTL.
201-
# TYPE cortex_ingester_expanded_postings_cache_evicts counter
202-
cortex_ingester_expanded_postings_cache_evicts{cache="test",reason="expired"} %v
203-
`, numberOfKeys*2)), "cortex_ingester_expanded_postings_cache_evicts")
201+
# HELP cortex_ingester_expanded_postings_cache_evicts_total Total number of evictions in the cache, excluding items that got evicted due to TTL.
202+
# TYPE cortex_ingester_expanded_postings_cache_evicts_total counter
203+
cortex_ingester_expanded_postings_cache_evicts_total{cache="test",reason="expired"} %v
204+
`, numberOfKeys)), "cortex_ingester_expanded_postings_cache_evicts_total")
204205
require.NoError(t, err)
205206
}
206207
})

0 commit comments

Comments
 (0)