-
Notifications
You must be signed in to change notification settings - Fork 53
/
metrics.go
157 lines (138 loc) · 5.16 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package patch
import (
"sync/atomic"
"github.com/prometheus/client_golang/prometheus"
)
const oldDigestLabel = "old_digest"
const newDigestLabel = "new_digest"
const digestLabel = "digest"
type CacheMetrics struct {
CacheMisses int64
CacheHits int64
PluginCount int64
PatchCount int64
TotalSizeBytes int64
PatchCalcSecondsTotal int64
PatchCalcCount int64
}
type CacheMetricsTracker struct {
metrics CacheMetrics
promCacheMisses *prometheus.CounterVec
promCacheHits *prometheus.CounterVec
promPluginCount prometheus.Gauge
promPatchCount prometheus.Gauge
promTotalSizeBytes *prometheus.GaugeVec
promPatchCalcNanoSecsTotal *prometheus.CounterVec
promPatchCalcCount *prometheus.CounterVec
}
func NewCacheMetricsTracker(constLabels map[string]string) CacheMetricsTracker {
return CacheMetricsTracker{
promCacheMisses: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "opni",
Subsystem: "patch",
Name: "cache_misses_total",
ConstLabels: constLabels,
Help: "Total number of patch requests that were not found in the cache",
}, []string{oldDigestLabel, newDigestLabel}),
promCacheHits: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "opni",
Subsystem: "patch",
Name: "cache_hits_total",
ConstLabels: constLabels,
Help: "Total number of patch requests that were found in the cache",
}, []string{oldDigestLabel, newDigestLabel}),
promPluginCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "opni",
Subsystem: "patch",
Name: "plugin_count",
ConstLabels: constLabels,
Help: "Total number of plugin revisions in the cache",
}),
promPatchCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "opni",
Subsystem: "patch",
Name: "patch_count",
ConstLabels: constLabels,
Help: "Total number of patches in the cache",
}),
promTotalSizeBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "opni",
Subsystem: "patch",
Name: "total_size_bytes",
ConstLabels: constLabels,
Help: "Total size of the cache in bytes",
}, []string{digestLabel}),
promPatchCalcNanoSecsTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "opni",
Subsystem: "patch",
Name: "calc_nano_seconds_total",
ConstLabels: constLabels,
Help: "Total time spent calculating patches in nanoseconds",
}, []string{oldDigestLabel, newDigestLabel}),
promPatchCalcCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "opni",
Subsystem: "patch",
Name: "calc_count_total",
ConstLabels: constLabels,
Help: "Total number of patch calculations requested",
}, []string{oldDigestLabel, newDigestLabel}),
}
}
func (c *CacheMetricsTracker) CacheMiss(oldDigest, newDigest string) {
atomic.AddInt64(&c.metrics.CacheMisses, 1)
c.promCacheMisses.WithLabelValues(oldDigest, newDigest).Inc()
}
func (c *CacheMetricsTracker) CacheHit(oldDigest, newDigest string) {
atomic.AddInt64(&c.metrics.CacheHits, 1)
c.promCacheHits.WithLabelValues(oldDigest, newDigest).Inc()
}
func (c *CacheMetricsTracker) SetPluginCount(count int64) {
atomic.StoreInt64(&c.metrics.PluginCount, count)
c.promPluginCount.Set(float64(count))
}
func (c *CacheMetricsTracker) AddToPluginCount(value int64) {
atomic.AddInt64(&c.metrics.PluginCount, value)
c.promPluginCount.Add(float64(value))
}
func (c *CacheMetricsTracker) SetPatchCount(count int64) {
atomic.StoreInt64(&c.metrics.PatchCount, count)
c.promPatchCount.Set(float64(count))
}
func (c *CacheMetricsTracker) AddToPatchCount(value int64) {
atomic.AddInt64(&c.metrics.PatchCount, value)
c.promPatchCount.Add(float64(value))
}
func (c *CacheMetricsTracker) SetTotalSizeBytes(digest string, size int64) {
atomic.StoreInt64(&c.metrics.TotalSizeBytes, size)
c.promTotalSizeBytes.WithLabelValues(digest).Set(float64(size))
}
func (c *CacheMetricsTracker) AddToTotalSizeBytes(digest string, value int64) {
atomic.AddInt64(&c.metrics.TotalSizeBytes, value)
c.promTotalSizeBytes.WithLabelValues(digest).Add(float64(value))
}
func (c *CacheMetricsTracker) IncPatchCalcSecsTotal(oldDigest, newDigest string, value float64) {
atomic.AddInt64(&c.metrics.PatchCalcSecondsTotal, int64(value))
c.promPatchCalcNanoSecsTotal.WithLabelValues(oldDigest, newDigest).Add(value)
}
func (c *CacheMetricsTracker) IncPatchCalcCount(oldDigest, newDigest string) {
atomic.AddInt64(&c.metrics.PatchCalcCount, 1)
c.promPatchCalcCount.WithLabelValues(oldDigest, newDigest).Inc()
}
func (c *CacheMetricsTracker) MetricsCollectors() []prometheus.Collector {
return []prometheus.Collector{
c.promCacheMisses,
c.promCacheHits,
c.promPluginCount,
c.promPatchCount,
c.promTotalSizeBytes,
}
}
func (c *CacheMetricsTracker) MetricsSnapshot() CacheMetrics {
return CacheMetrics{
CacheMisses: atomic.LoadInt64(&c.metrics.CacheMisses),
CacheHits: atomic.LoadInt64(&c.metrics.CacheHits),
PluginCount: atomic.LoadInt64(&c.metrics.PluginCount),
PatchCount: atomic.LoadInt64(&c.metrics.PatchCount),
TotalSizeBytes: atomic.LoadInt64(&c.metrics.TotalSizeBytes),
}
}