-
Notifications
You must be signed in to change notification settings - Fork 1
/
cacheMetrics.go
129 lines (113 loc) · 3.49 KB
/
cacheMetrics.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
package httpcache
import (
"context"
"flamingo.me/flamingo/v3/framework/opencensus"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
var (
backendTypeCacheKeyType, _ = tag.NewKey("backend_type")
frontendNameCacheKeyType, _ = tag.NewKey("frontend_name")
backendCacheKeyErrorReason, _ = tag.NewKey("error_reason")
backendCacheHitCount = stats.Int64("flamingo/httpcache/backend/hit", "Count of cache-backend hits", stats.UnitDimensionless)
backendCacheMissCount = stats.Int64("flamingo/httpcache/backend/miss", "Count of cache-backend misses", stats.UnitDimensionless)
backendCacheErrorCount = stats.Int64("flamingo/httpcache/backend/error", "Count of cache-backend errors", stats.UnitDimensionless)
backendCacheEntriesCount = stats.Int64("flamingo/httpcache/backend/entries", "Count of cache-backend entries", stats.UnitDimensionless)
)
type (
// MetricsBackend - a Backend that logs metrics
MetricsBackend struct {
}
// Metrics take care of publishing metrics for a specific cache
Metrics struct {
// backendType - the type of the cache backend
backendType string
// frontendName - the name if the cache frontend where the backend is attached
frontendName string
}
)
// NewCacheMetrics creates a backend metrics helper instance
func NewCacheMetrics(backendType string, frontendName string) Metrics {
b := Metrics{
backendType: backendType,
frontendName: frontendName,
}
return b
}
func init() {
if err := opencensus.View(
"flamingo/httpcache/backend/hit",
backendCacheHitCount,
view.Count(),
backendTypeCacheKeyType,
frontendNameCacheKeyType,
); err != nil {
panic(err)
}
if err := opencensus.View(
"flamingo/httpcache/backend/miss",
backendCacheMissCount,
view.Count(),
backendTypeCacheKeyType,
frontendNameCacheKeyType,
); err != nil {
panic(err)
}
if err := opencensus.View(
"flamingo/httpcache/backend/error",
backendCacheErrorCount,
view.Count(),
backendTypeCacheKeyType,
frontendNameCacheKeyType,
backendCacheKeyErrorReason,
); err != nil {
panic(err)
}
if err := opencensus.View(
"flamingo/httpcache/backend/entries",
backendCacheEntriesCount,
view.LastValue(),
backendTypeCacheKeyType,
frontendNameCacheKeyType,
); err != nil {
panic(err)
}
}
func (bi Metrics) countHit() {
ctx, _ := tag.New(
context.Background(),
tag.Upsert(opencensus.KeyArea, "cacheBackend"),
tag.Upsert(backendTypeCacheKeyType, bi.backendType),
tag.Upsert(frontendNameCacheKeyType, bi.frontendName),
)
stats.Record(ctx, backendCacheHitCount.M(1))
}
func (bi Metrics) countMiss() {
ctx, _ := tag.New(
context.Background(),
tag.Upsert(opencensus.KeyArea, "cacheBackend"),
tag.Upsert(backendTypeCacheKeyType, bi.backendType),
tag.Upsert(frontendNameCacheKeyType, bi.frontendName),
)
stats.Record(ctx, backendCacheMissCount.M(1))
}
func (bi Metrics) countError(reason string) {
ctx, _ := tag.New(
context.Background(),
tag.Upsert(opencensus.KeyArea, "cacheBackend"),
tag.Upsert(backendTypeCacheKeyType, bi.backendType),
tag.Upsert(frontendNameCacheKeyType, bi.frontendName),
tag.Upsert(backendCacheKeyErrorReason, reason),
)
stats.Record(ctx, backendCacheErrorCount.M(1))
}
func (bi Metrics) recordEntries(entries int64) {
ctx, _ := tag.New(
context.Background(),
tag.Upsert(opencensus.KeyArea, "cacheBackend"),
tag.Upsert(backendTypeCacheKeyType, bi.backendType),
tag.Upsert(frontendNameCacheKeyType, bi.frontendName),
)
stats.Record(ctx, backendCacheEntriesCount.M(entries))
}