-
Notifications
You must be signed in to change notification settings - Fork 13
/
metrics.go
138 lines (118 loc) · 3.71 KB
/
metrics.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package sturdyc
type MetricsRecorder interface {
// CacheHit is called for every key that results in a cache hit.
CacheHit()
// CacheMiss is called for every key that results in a cache miss.
CacheMiss()
// Refresh is called when a get operation results in a refresh.
Refresh()
// MissingRecord is called every time the cache is asked to
// look up a key which has been marked as missing.
MissingRecord()
// ForcedEviction is called when the cache reaches its capacity, and has to
// evict keys in order to write a new one.
ForcedEviction()
// EntriesEvicted is called when the cache evicts keys from a shard.
EntriesEvicted(int)
// ShardIndex is called to report which shard it was that performed an operation.
ShardIndex(int)
// CacheBatchRefreshSize is called to report the size of the batch refresh.
CacheBatchRefreshSize(size int)
// ObserveCacheSize is called to report the size of the cache.
ObserveCacheSize(callback func() int)
}
type DistributedMetricsRecorder interface {
MetricsRecorder
// DistributedCacheHit is called for every key that results in a cache hit.
DistributedCacheHit()
// DistributedCacheMiss is called for every key that results in a cache miss.
DistributedCacheMiss()
// DistributedRefresh is called when we retrieve a record from
// the distributed storage that should be refreshed.
DistributedRefresh()
// DistributedMissingRecord is called when we retrieve a record from the
// distributed storage that has been marked as a missing record.
DistributedMissingRecord()
// DistributedFallback is called when you are using a distributed storage
// with early refreshes, and the call for a value was supposed to refresh it,
// but the call failed. When that happens, the cache fallbacks to the latest
// value from the distributed storage.
DistributedFallback()
}
type distributedMetricsRecorder struct {
MetricsRecorder
}
func (d *distributedMetricsRecorder) DistributedCacheHit() {}
func (d *distributedMetricsRecorder) DistributedCacheMiss() {}
func (d *distributedMetricsRecorder) DistributedRefresh() {}
func (d *distributedMetricsRecorder) DistributedMissingRecord() {}
func (d *distributedMetricsRecorder) DistributedFallback() {}
func (s *shard[T]) reportForcedEviction() {
if s.metricsRecorder == nil {
return
}
s.metricsRecorder.ForcedEviction()
}
func (s *shard[T]) reportEntriesEvicted(n int) {
if s.metricsRecorder == nil {
return
}
s.metricsRecorder.EntriesEvicted(n)
}
// reportCacheHits is used to report cache hits and misses to the metrics recorder.
func (c *Client[T]) reportCacheHits(cacheHit, missingRecord, refresh bool) {
if c.metricsRecorder == nil {
return
}
if missingRecord {
c.metricsRecorder.MissingRecord()
}
if refresh {
c.metricsRecorder.Refresh()
}
if !cacheHit {
c.metricsRecorder.CacheMiss()
return
}
c.metricsRecorder.CacheHit()
}
func (c *Client[T]) reportShardIndex(index int) {
if c.metricsRecorder == nil {
return
}
c.metricsRecorder.ShardIndex(index)
}
func (c *Client[T]) reportBatchRefreshSize(n int) {
if c.metricsRecorder == nil {
return
}
c.metricsRecorder.CacheBatchRefreshSize(n)
}
func (c *Client[T]) reportDistributedCacheHit(cacheHit bool) {
if c.metricsRecorder == nil {
return
}
if !cacheHit {
c.metricsRecorder.DistributedCacheMiss()
return
}
c.metricsRecorder.DistributedCacheHit()
}
func (c *Client[T]) reportDistributedRefresh() {
if c.metricsRecorder == nil {
return
}
c.metricsRecorder.DistributedRefresh()
}
func (c *Client[T]) reportDistributedMissingRecord() {
if c.metricsRecorder == nil {
return
}
c.metricsRecorder.DistributedMissingRecord()
}
func (c *Client[T]) reportDistributedStaleFallback() {
if c.metricsRecorder == nil {
return
}
c.metricsRecorder.DistributedFallback()
}