mirror of https://github.com/grafana/loki
Add cache usage statistics (#6317)
* Adding cache statistics Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Adding metrics to metrics.go Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Creating new stats context for use in metric queries middleware Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Clean up unnecessary log fields Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Fixing tests Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Adding stats tests Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * CHANGELOG entry Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Appeasing the linter Documenting function Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Moving CHANGELOG entry to appropriate section Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Implementing a stats collector cache wrapper to simplify stats collection If we keep the stats collection in pkg/storage/chunk/cache/instrumented.go, then any implementation that wraps it will cause the stats collected to be incomplete. For example: NewBackground(cacheName, cfg.Background, Instrument(cacheName, cache, reg), reg)) - the background cache requests are not collected Signed-off-by: Danny Kopping <danny.kopping@grafana.com> * Fixing tests Signed-off-by: Danny Kopping <danny.kopping@grafana.com>pull/6330/head
parent
65e3148bc9
commit
36e0979cf5
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,53 @@ |
||||
package cache |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/loki/pkg/logqlmodel/stats" |
||||
) |
||||
|
||||
type statsCollector struct { |
||||
Cache |
||||
} |
||||
|
||||
// CollectStats returns a new Cache that keeps various statistics on cache usage.
|
||||
func CollectStats(cache Cache) Cache { |
||||
return &statsCollector{ |
||||
Cache: cache, |
||||
} |
||||
} |
||||
|
||||
func (s statsCollector) Store(ctx context.Context, keys []string, bufs [][]byte) error { |
||||
st := stats.FromContext(ctx) |
||||
st.AddCacheRequest(s.Cache.GetCacheType(), 1) |
||||
|
||||
// we blindly count the number of keys to be stored since we can't know if these will actually be written back to
|
||||
// the cache successfully if cache.backgroundCache is in use
|
||||
st.AddCacheEntriesStored(s.Cache.GetCacheType(), len(keys)) |
||||
|
||||
return s.Cache.Store(ctx, keys, bufs) |
||||
} |
||||
|
||||
func (s statsCollector) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string, err error) { |
||||
st := stats.FromContext(ctx) |
||||
st.AddCacheRequest(s.Cache.GetCacheType(), 1) |
||||
|
||||
found, bufs, missing, err = s.Cache.Fetch(ctx, keys) |
||||
|
||||
st.AddCacheEntriesFound(s.Cache.GetCacheType(), len(found)) |
||||
st.AddCacheEntriesRequested(s.Cache.GetCacheType(), len(keys)) |
||||
|
||||
for j := range bufs { |
||||
st.AddCacheBytesRetrieved(s.Cache.GetCacheType(), len(bufs[j])) |
||||
} |
||||
|
||||
return found, bufs, missing, err |
||||
} |
||||
|
||||
func (s statsCollector) Stop() { |
||||
s.Cache.Stop() |
||||
} |
||||
|
||||
func (s statsCollector) GetCacheType() stats.CacheType { |
||||
return s.Cache.GetCacheType() |
||||
} |
Loading…
Reference in new issue