Skip to content

Commit

Permalink
opencensus metrics: add cache-{size,entries} stats
Browse files Browse the repository at this point in the history
Also define a (generated) stringer for `CacheType`.
  • Loading branch information
dfinkel committed Dec 9, 2019
1 parent da6a423 commit 3085b3f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
26 changes: 26 additions & 0 deletions cachetype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions galaxycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ func (universe *Universe) NewGalaxy(name string, cacheBytes int64, getter Backen
peerPicker: universe.peerPicker,
cacheBytes: cacheBytes,
mainCache: cache{
lru: lru.New(0),
ctype: MainCache,
lru: lru.New(0),
},
hotCache: cache{
lru: lru.New(0),
ctype: HotCache,
lru: lru.New(0),
},
candidateCache: cache{
lru: lru.New(gOpts.maxCandidates),
ctype: CandidateCache,
lru: lru.New(gOpts.maxCandidates),
},
hcStatsWithTime: HCStatsWithTime{
hcs: &promoter.HCStats{
Expand Down Expand Up @@ -498,7 +501,7 @@ func (g *Galaxy) load(ctx context.Context, key string, dest Codec) (value *valWi
stats.Record(ctx, MCoalescedBackendLoads.M(1))
destPopulated = true // only one caller of load gets this return value
value = newValWithStat(data, nil)
g.populateCache(key, value, &g.mainCache)
g.populateCache(ctx, key, value, &g.mainCache)
return &valWithLevel{value, hitBackend, authoritative, peerErr, err}, nil
})
if err == nil {
Expand Down Expand Up @@ -537,7 +540,7 @@ func (g *Galaxy) getFromPeer(ctx context.Context, peer RemoteFetcher, key string
}
value := newValWithStat(data, kStats)
if g.opts.promoter.ShouldPromote(key, value.data, stats) {
g.populateCache(key, value, &g.hotCache)
g.populateCache(ctx, key, value, &g.hotCache)
}
return value, nil
}
Expand All @@ -558,11 +561,16 @@ func (g *Galaxy) lookupCache(key string) (*valWithStat, hitLevel) {
return vi.(*valWithStat), hitHotcache
}

func (g *Galaxy) populateCache(key string, value *valWithStat, cache *cache) {
func (g *Galaxy) populateCache(ctx context.Context, key string, value *valWithStat, cache *cache) {
if g.cacheBytes <= 0 {
return
}
cache.add(key, value)
// Record the size of this cache after we've finished evicting any necessary values.
defer func() {
stats.RecordWithTags(ctx, []tag.Mutator{tag.Upsert(CacheLevelKey, cache.ctype.String())},
MCacheSize.M(cache.bytes()), MCacheEntries.M(cache.items()))
}()

// Evict items from cache(s) if necessary.
for {
Expand Down Expand Up @@ -619,6 +627,7 @@ func (g *Galaxy) CacheStats(which CacheType) CacheStats {
// and counts the size of all keys and values. Candidate cache only
// utilizes the lru.Cache and mutex, not the included stats.
type cache struct {
ctype CacheType
mu sync.Mutex
nbytes int64 // of all keys and values
lru *lru.Cache
Expand Down Expand Up @@ -737,3 +746,5 @@ type CacheStats struct {
Hits int64
Evictions int64
}

//go:generate stringer -type=CacheType
5 changes: 5 additions & 0 deletions observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ var (
MValueLength = stats.Int64("galaxycache/value_length", "The length of values", stats.UnitBytes)

MRoundtripLatencyMilliseconds = stats.Float64("galaxycache/roundtrip_latency", "Roundtrip latency in milliseconds", stats.UnitMilliseconds)

MCacheSize = stats.Int64("galaxycache/cache_bytes", "The number of bytes used for storing Keys and Values in the cache", stats.UnitBytes)
MCacheEntries = stats.Int64("galaxycache/cache_entries", "The number of entries in the cache", stats.UnitDimensionless)
)

// GalaxyKey tags the name of the galaxy
Expand Down Expand Up @@ -80,6 +83,8 @@ var AllViews = []*view.View{
{Measure: MValueLength, TagKeys: []tag.Key{GalaxyKey}, Aggregation: defaultBytesDistribution},

{Measure: MRoundtripLatencyMilliseconds, TagKeys: []tag.Key{GalaxyKey}, Aggregation: defaultMillisecondsDistribution},
{Measure: MCacheSize, TagKeys: []tag.Key{GalaxyKey, CacheLevelKey}, Aggregation: view.LastValue()},
{Measure: MCacheEntries, TagKeys: []tag.Key{GalaxyKey, CacheLevelKey}, Aggregation: view.LastValue()},
}

func sinceInMilliseconds(start time.Time) float64 {
Expand Down

0 comments on commit 3085b3f

Please sign in to comment.