Skip to content

Add a histogram for cached value sizes. #1569

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

Merged
merged 2 commits into from
Aug 13, 2019
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
36 changes: 30 additions & 6 deletions pkg/chunk/cache/instrumented.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,51 @@ var (
Name: "cache_hits",
Help: "Total count of keys found in cache.",
}, []string{"name"})

valueSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "cortex",
Name: "cache_value_size_bytes",
Help: "Size of values in the cache.",
// Cached chunks are generally in the KBs, but cached index can
// get big. Histogram goes from 1KB to 4MB.
// 1024 * 4^(7-1) = 4MB
Buckets: prometheus.ExponentialBuckets(1024, 4, 7),
}, []string{"name", "method"})
)

func init() {
requestDuration.Register()
prometheus.MustRegister(fetchedKeys)
prometheus.MustRegister(hits)
prometheus.MustRegister(valueSize)
}

// Instrument returns an instrumented cache.
func Instrument(name string, cache Cache) Cache {
return &instrumentedCache{
name: name,
fetchedKeys: fetchedKeys.WithLabelValues(name),
hits: hits.WithLabelValues(name),
Cache: cache,
name: name,
Cache: cache,

fetchedKeys: fetchedKeys.WithLabelValues(name),
hits: hits.WithLabelValues(name),
storedValueSize: valueSize.WithLabelValues(name, "store"),
fetchedValueSize: valueSize.WithLabelValues(name, "fetch"),
}
}

type instrumentedCache struct {
name string
fetchedKeys, hits prometheus.Counter
name string
Cache

fetchedKeys, hits prometheus.Counter
storedValueSize, fetchedValueSize prometheus.Observer
}

func (i *instrumentedCache) Store(ctx context.Context, keys []string, bufs [][]byte) {
for j := range bufs {
i.storedValueSize.Observe(float64(len(bufs[j])))
}

method := i.name + ".store"
instr.CollectedRequest(ctx, method, requestDuration, instr.ErrorCode, func(ctx context.Context) error {
sp := ot.SpanFromContext(ctx)
Expand Down Expand Up @@ -82,6 +102,10 @@ func (i *instrumentedCache) Fetch(ctx context.Context, keys []string) ([]string,

i.fetchedKeys.Add(float64(len(keys)))
i.hits.Add(float64(len(found)))
for j := range bufs {
i.fetchedValueSize.Observe(float64(len(bufs[j])))
}

return found, bufs, missing
}

Expand Down
1 change: 1 addition & 0 deletions pkg/chunk/chunk_store_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type decodeResponse struct {

// NewChunkFetcher makes a new ChunkFetcher.
func NewChunkFetcher(cfg cache.Config, cacheStubs bool, storage ObjectClient) (*Fetcher, error) {
cfg.Prefix = "chunks"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the effect that the name label in cortex_cache_hits changes from "memcache" to "chunksmemcache".
Which is a (minor) breaking change that should go in the changelog.

cache, err := cache.New(cfg)
if err != nil {
return nil, err
Expand Down