Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cache metrics to groupcache #5352

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re

### Added

- [#5352](https://github.com/thanos-io/thanos/pull/5352) Cache: Add cache metrics to groupcache.

### Changed

### Removed
Expand Down
31 changes: 29 additions & 2 deletions pkg/cache/groupcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func NewGroupcacheWithConfig(logger log.Logger, reg prometheus.Registerer, conf
},
))

RegisterCacheStatsCollector(galaxy, reg)
RegisterCacheStatsCollector(galaxy, &conf, reg)

return &Groupcache{
logger: logger,
Expand Down Expand Up @@ -305,8 +305,13 @@ func (c *Groupcache) Name() string {

type CacheStatsCollector struct {
galaxy *galaxycache.Galaxy
conf *GroupcacheConfig

// GalaxyCache Metric descriptions.
bytes *prometheus.Desc
evictions *prometheus.Desc
items *prometheus.Desc
maxBytes *prometheus.Desc
gets *prometheus.Desc
loads *prometheus.Desc
peerLoads *prometheus.Desc
Expand All @@ -317,7 +322,16 @@ type CacheStatsCollector struct {
}

// RegisterCacheStatsCollector registers a groupcache metrics collector.
func RegisterCacheStatsCollector(galaxy *galaxycache.Galaxy, reg prometheus.Registerer) {
func RegisterCacheStatsCollector(galaxy *galaxycache.Galaxy, conf *GroupcacheConfig, reg prometheus.Registerer) {
// Cache metrics.
bytes := prometheus.NewDesc("thanos_cache_groupcache_bytes", "The number of bytes in the main cache.", []string{"cache"}, nil)
evictions := prometheus.NewDesc("thanos_cache_groupcache_evictions_total", "The number items evicted from the cache.", []string{"cache"}, nil)
items := prometheus.NewDesc("thanos_cache_groupcache_items", "The number of items in the cache.", []string{"cache"}, nil)

// Configuration Metrics.
maxBytes := prometheus.NewDesc("thanos_cache_groupcache_max_bytes", "The max number of bytes in the cache.", nil, nil)

// GroupCache metrics.
gets := prometheus.NewDesc("thanos_cache_groupcache_get_requests_total", "Total number of get requests, including from peers.", nil, nil)
loads := prometheus.NewDesc("thanos_cache_groupcache_loads_total", "Total number of loads from backend (gets - cacheHits).", nil, nil)
peerLoads := prometheus.NewDesc("thanos_cache_groupcache_peer_loads_total", "Total number of loads from peers (remote load or remote cache hit).", nil, nil)
Expand All @@ -328,6 +342,11 @@ func RegisterCacheStatsCollector(galaxy *galaxycache.Galaxy, reg prometheus.Regi

collector := &CacheStatsCollector{
galaxy: galaxy,
conf: conf,
bytes: bytes,
evictions: evictions,
items: items,
maxBytes: maxBytes,
gets: gets,
loads: loads,
peerLoads: peerLoads,
Expand All @@ -340,6 +359,14 @@ func RegisterCacheStatsCollector(galaxy *galaxycache.Galaxy, reg prometheus.Regi
}

func (s *CacheStatsCollector) Collect(ch chan<- prometheus.Metric) {
for _, cache := range []galaxycache.CacheType{galaxycache.MainCache, galaxycache.HotCache} {
cacheStats := s.galaxy.CacheStats(cache)
ch <- prometheus.MustNewConstMetric(s.bytes, prometheus.GaugeValue, float64(cacheStats.Bytes), cache.String())
ch <- prometheus.MustNewConstMetric(s.evictions, prometheus.GaugeValue, float64(cacheStats.Evictions), cache.String())
ch <- prometheus.MustNewConstMetric(s.items, prometheus.GaugeValue, float64(cacheStats.Items), cache.String())
}

ch <- prometheus.MustNewConstMetric(s.maxBytes, prometheus.GaugeValue, float64(s.conf.MaxSize))
ch <- prometheus.MustNewConstMetric(s.gets, prometheus.CounterValue, float64(s.galaxy.Stats.Gets.Get()))
ch <- prometheus.MustNewConstMetric(s.loads, prometheus.CounterValue, float64(s.galaxy.Stats.Loads.Get()))
ch <- prometheus.MustNewConstMetric(s.peerLoads, prometheus.CounterValue, float64(s.galaxy.Stats.PeerLoads.Get()))
Expand Down