Skip to content

Commit

Permalink
Add disk cache size metrics
Browse files Browse the repository at this point in the history
Add Prometheus metrics for monitoring:

- How large is the disk cache now?

- How fast has the disk cache been growing?

- At what rate are cache data evicted?

- How common is it with multiple put requests of the same key?
  • Loading branch information
ulrfa authored and mostynb committed Sep 11, 2020
1 parent 2d9211c commit fc1c80b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cache/disk/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ import (
"container/list"
"errors"
"fmt"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
gaugeCacheSizeBytes = promauto.NewGauge(prometheus.GaugeOpts{
Name: "bazel_remote_disk_cache_size_bytes",
Help: "The current number of bytes in the disk backend",
})

counterEvictedBytes = promauto.NewCounter(prometheus.CounterOpts{
Name: "bazel_remote_disk_cache_evicted_bytes_total",
Help: "The total number of bytes evicted from disk backend, due to full cache",
})

counterOverwrittenBytes = promauto.NewCounter(prometheus.CounterOpts{
Name: "bazel_remote_disk_cache_overwritten_bytes_total",
Help: "The total number of bytes removed from disk backend, due to put of already existing key",
})
)

type sizedItem interface {
Expand Down Expand Up @@ -88,6 +108,7 @@ func (c *sizedLRU) Add(key Key, value sizedItem) (ok bool) {
return false
}
c.ll.MoveToFront(ee)
counterOverwrittenBytes.Add(float64(ee.Value.(*entry).value.Size()))
ee.Value.(*entry).value = value
} else {
sizeDelta = value.Size()
Expand All @@ -109,6 +130,8 @@ func (c *sizedLRU) Add(key Key, value sizedItem) (ok bool) {

c.currentSize += sizeDelta

gaugeCacheSizeBytes.Set(float64(c.currentSize))

return true
}

Expand All @@ -126,6 +149,7 @@ func (c *sizedLRU) Get(key Key) (value sizedItem, ok bool) {
func (c *sizedLRU) Remove(key Key) {
if ele, hit := c.cache[key]; hit {
c.removeElement(ele)
gaugeCacheSizeBytes.Set(float64(c.currentSize))
}
}

Expand Down Expand Up @@ -221,6 +245,7 @@ func (c *sizedLRU) removeElement(e *list.Element) {
kv := e.Value.(*entry)
delete(c.cache, kv.key)
c.currentSize -= kv.value.Size()
counterEvictedBytes.Add(float64(kv.value.Size()))

if c.onEvict != nil {
c.onEvict(kv.key, kv.value)
Expand Down

0 comments on commit fc1c80b

Please sign in to comment.