-
Notifications
You must be signed in to change notification settings - Fork 27
/
observability.go
67 lines (57 loc) · 1.34 KB
/
observability.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package nds
import (
"context"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
var (
// Measures
mCacheHit = stats.Int64("cache_hit", "The number of cache hits", stats.UnitDimensionless)
mCacheMiss = stats.Int64("cache_miss", "The number of cache misses", stats.UnitDimensionless)
// Tag Keys
KeyKind, _ = tag.NewKey("kind")
// Views
AllViews = []*view.View{
{
Name: "nds/cache_hit",
Description: "The number of cache hits",
Measure: mCacheHit,
Aggregation: view.Sum(),
TagKeys: []tag.Key{KeyKind},
},
{
Name: "nds/cache_miss",
Description: "The number of cache misses",
Measure: mCacheMiss,
Aggregation: view.Sum(),
TagKeys: []tag.Key{KeyKind},
},
}
)
func cacheStatsByKind(ctx context.Context, items []cacheItem) error {
cacheStats := make(map[string]*[2]int64)
for _, item := range items {
if _, ok := cacheStats[item.key.Kind]; !ok {
cacheStats[item.key.Kind] = &[2]int64{0, 0}
}
switch item.state {
case done: // Hit
cacheStats[item.key.Kind][0]++
default: // Miss
cacheStats[item.key.Kind][1]++
}
}
for key, s := range cacheStats {
if err := stats.RecordWithTags(ctx,
[]tag.Mutator{
tag.Upsert(KeyKind, key),
},
mCacheHit.M(s[0]),
mCacheMiss.M(s[1]),
); err != nil {
return err
}
}
return nil
}