forked from flant/shell-operator
/
collector.go
203 lines (164 loc) · 4.56 KB
/
collector.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package vault
import (
"hash/fnv"
"sync"
"sync/atomic"
"github.com/prometheus/client_golang/prometheus"
. "github.com/flant/shell-operator/pkg/utils/labels"
)
type ConstMetricCollector interface {
Describe(ch chan<- *prometheus.Desc)
Collect(ch chan<- prometheus.Metric)
Type() string
LabelNames() []string
Name() string
ExpireGroupMetrics(group string)
}
var (
_ ConstMetricCollector = (*ConstCounterCollector)(nil)
_ ConstMetricCollector = (*ConstGaugeCollector)(nil)
)
type GroupedCounterMetric struct {
Value uint64
LabelValues []string
Group string
}
type GroupedGaugeMetric struct {
Value float64
LabelValues []string
Group string
}
type ConstCounterCollector struct {
mtx sync.RWMutex
collection map[uint64]GroupedCounterMetric
desc *prometheus.Desc
name string
labelNames []string
}
func NewConstCounterCollector(name string, labelNames []string) *ConstCounterCollector {
desc := prometheus.NewDesc(name, name, labelNames, nil)
return &ConstCounterCollector{
name: name,
labelNames: labelNames,
desc: desc,
collection: make(map[uint64]GroupedCounterMetric),
}
}
// Add increases a counter metric by a value. Metric is identified by label values and a group.
func (c *ConstCounterCollector) Add(group string, value float64, labels map[string]string) {
c.mtx.Lock()
defer c.mtx.Unlock()
labelValues := LabelValues(labels, c.labelNames)
labelsHash := HashLabelValues(labelValues)
// TODO add group to hash
storedMetric, ok := c.collection[labelsHash]
if !ok {
storedMetric = GroupedCounterMetric{
Value: uint64(value),
LabelValues: labelValues,
Group: group,
}
} else {
atomic.AddUint64(&storedMetric.Value, uint64(value))
}
c.collection[labelsHash] = storedMetric
}
func (c *ConstCounterCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.desc
}
func (c *ConstCounterCollector) Collect(ch chan<- prometheus.Metric) {
c.mtx.RLock()
defer c.mtx.RUnlock()
for _, s := range c.collection {
ch <- prometheus.MustNewConstMetric(c.desc, prometheus.CounterValue, float64(s.Value), s.LabelValues...)
}
}
func (c *ConstCounterCollector) Type() string {
return "counter"
}
func (c *ConstCounterCollector) LabelNames() []string {
return c.labelNames
}
func (c *ConstCounterCollector) Name() string {
return c.name
}
// ExpireGroupMetrics deletes all metrics from collection with matched group.
func (c *ConstCounterCollector) ExpireGroupMetrics(group string) {
c.mtx.Lock()
defer c.mtx.Unlock()
for hash, m := range c.collection {
if m.Group == group {
delete(c.collection, hash)
}
}
}
type ConstGaugeCollector struct {
mtx sync.RWMutex
name string
labelNames []string
desc *prometheus.Desc
collection map[uint64]GroupedGaugeMetric
}
func NewConstGaugeCollector(name string, labelNames []string) *ConstGaugeCollector {
desc := prometheus.NewDesc(name, name, labelNames, nil)
return &ConstGaugeCollector{
name: name,
labelNames: labelNames,
desc: desc,
collection: make(map[uint64]GroupedGaugeMetric),
}
}
func (c *ConstGaugeCollector) Set(group string, value float64, labels map[string]string) {
c.mtx.Lock()
defer c.mtx.Unlock()
labelValues := LabelValues(labels, c.labelNames)
labelsHash := HashLabelValues(labelValues)
storedMetric, ok := c.collection[labelsHash]
if !ok {
storedMetric = GroupedGaugeMetric{
Value: value,
LabelValues: labelValues,
Group: group,
}
}
storedMetric.Value = value
c.collection[labelsHash] = storedMetric
}
func (c *ConstGaugeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.desc
}
func (c *ConstGaugeCollector) Collect(ch chan<- prometheus.Metric) {
c.mtx.RLock()
defer c.mtx.RUnlock()
for _, s := range c.collection {
ch <- prometheus.MustNewConstMetric(c.desc, prometheus.GaugeValue, s.Value, s.LabelValues...)
}
}
func (c *ConstGaugeCollector) Type() string {
return "gauge"
}
func (c *ConstGaugeCollector) LabelNames() []string {
return c.labelNames
}
func (c *ConstGaugeCollector) Name() string {
return c.name
}
// ExpireGroupMetrics deletes all metrics from collection with matched group.
func (c *ConstGaugeCollector) ExpireGroupMetrics(group string) {
c.mtx.Lock()
defer c.mtx.Unlock()
for hash, m := range c.collection {
if m.Group == group {
delete(c.collection, hash)
}
}
}
const labelsSeparator = byte(255)
func HashLabelValues(labelValues []string) uint64 {
hasher := fnv.New64a()
for _, labelValue := range labelValues {
_, _ = hasher.Write([]byte(labelValue))
_, _ = hasher.Write([]byte{labelsSeparator})
}
return hasher.Sum64()
}