This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
312 lines (251 loc) · 10.4 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package metrics
import (
"errors"
"fmt"
"net"
"sort"
"strings"
"time"
)
// MetricType is the enum for tye types of metrics supported
type MetricType string
const (
// MetricTypeCounter is a data type that contains ever increasing numbers from the start of the server.
MetricTypeCounter MetricType = "counter"
// MetricTypeGauge is a metric type that can increase or decrease depending on the current value.
MetricTypeGauge MetricType = "gauge"
)
// Metric is a descriptor for metrics.
type Metric struct {
// Name is the name for the metric.
Name string
// Help is the help text for this metric.
Help string
// Unit describes the unit of the metric.
Unit string
// Created describes the time the metric was created. This is important for counters.
Created time.Time
// Type describes how the metric behaves.
Type MetricType
}
// String formats a metric as the OpenMetrics metadata
func (metric Metric) String() string {
return fmt.Sprintf(
"# HELP %s %s\n"+
"# UNIT %s %s\n"+
"# TYPE %s %s\n",
metric.Name,
metric.Help,
metric.Name,
metric.Unit,
metric.Name,
metric.Type,
)
}
// MetricValue is a structure that contains a value for a specific metric name and set of values.
type MetricValue struct {
// Name contains the name of the value.
Name string
// Labels contains a key-value map of labels to which the Value is specific.
Labels map[string]string
// Value contains the specific value stored.
Value float64
}
// MetricLabel is a struct that can be used with the metrics to pass additional labels. Create it using the
// Label function.
type MetricLabel struct {
name string
value string
}
type metricLabels []MetricLabel
func (labels metricLabels) toMap() map[string]string {
result := map[string]string{}
for _, label := range labels {
result[label.name] = label.value
}
return result
}
// Label creates a MetricLabel for use with the metric functions. Panics if name or value are empty. The name "country"
// is reserved.
func Label(name string, value string) MetricLabel {
if name == "" {
panic("BUG: the name cannot be empty")
}
if name == "country" {
panic("BUG: the name 'country' is reserved for GeoIP lookups")
}
if value == "" {
panic("BUG: the value cannot be empty")
}
return MetricLabel{
name: name,
value: value,
}
}
// Name returns the name of the label.
func (m MetricLabel) Name() string {
return m.name
}
// Value returns the value of the label.
func (m MetricLabel) Value() string {
return m.value
}
// CombinedName returns the name and labels combined.
func (metricValue MetricValue) CombinedName() string {
var labelList []string
keys := make([]string, 0, len(metricValue.Labels))
for k := range metricValue.Labels {
keys = append(keys, k)
}
sort.Strings(keys)
replacer := strings.NewReplacer(`"`, `\"`, `\`, `\\`)
for _, k := range keys {
labelList = append(labelList, k+"=\""+replacer.Replace(metricValue.Labels[k])+"\"")
}
var labels string
if len(labelList) > 0 {
labels = "{" + strings.Join(labelList, ",") + "}"
} else {
labels = ""
}
return metricValue.Name + labels
}
// String creates a string out of the name, labels, and value.
func (metricValue MetricValue) String() string {
return fmt.Sprintf("%s %f\n", metricValue.CombinedName(), metricValue.Value)
}
// MetricAlreadyExists is an error that is returned from the Create functions when the metric already exists.
var MetricAlreadyExists = errors.New("the specified metric already exists")
// CounterCannotBeIncrementedByNegative is an error returned by counters when they are incremented with a negative
// number.
var CounterCannotBeIncrementedByNegative = errors.New("a counter cannot be incremented by a negative number")
// Collector is the main interface for interacting with the metrics collector.
type Collector interface {
// CreateCounter creates a monotonic (increasing) counter with the specified name and help text.
CreateCounter(name string, unit string, help string) (Counter, error)
// MustCreateCounter creates a monotonic (increasing) counter with the specified name and help text. Panics if an
// error occurs.
MustCreateCounter(name string, unit string, help string) Counter
// CreateCounterGeo creates a monotonic (increasing) counter that is labeled with the country from the GeoIP lookup
// with the specified name and help text.
CreateCounterGeo(name string, unit string, help string) (GeoCounter, error)
// MustCreateCounterGeo creates a monotonic (increasing) counter that is labeled with the country from the GeoIP
// lookup with the specified name and help text. Panics if an error occurs.
MustCreateCounterGeo(name string, unit string, help string) GeoCounter
// CreateGauge creates a freely modifiable numeric gauge with the specified name and help text.
CreateGauge(name string, unit string, help string) (Gauge, error)
// MustCreateGauge creates a freely modifiable numeric gauge with the specified name and help text. Panics if an
// error occurs.
MustCreateGauge(name string, unit string, help string) Gauge
// CreateGaugeGeo creates a freely modifiable numeric gauge that is labeled with the country from the GeoIP lookup
// with the specified name and help text.
CreateGaugeGeo(name string, unit string, help string) (GeoGauge, error)
// MustCreateGaugeGeo creates a freely modifiable numeric gauge that is labeled with the country from the GeoIP
// lookup with the specified name and help text. Panics if an error occurs.
MustCreateGaugeGeo(name string, unit string, help string) GeoGauge
// ListMetrics returns a list of metrics metadata stored in the collector.
ListMetrics() []Metric
// GetMetric returns a set of values with labels for a specified metric name.
GetMetric(name string) []MetricValue
// String returns a Prometheus/OpenMetrics-compatible document with all metrics.
String() string
}
// SimpleCounter is a simple counter that can only be incremented.
type SimpleCounter interface {
// Increment increments the counter by 1
//
// - labels is a set of labels to apply. Can be created using the Label function.
Increment(labels ...MetricLabel)
// IncrementBy increments the counter by the specified number. Only returns an error if the passed by parameter is
// negative.
//
// - labels is a set of labels to apply. Can be created using the Label function.
IncrementBy(by float64, labels ...MetricLabel) error
}
// Counter extends the SimpleCounter interface by adding a WithLabels function to create a copy of the counter that
// is primed with a set of labels.
type Counter interface {
SimpleCounter
// WithLabels adds labels to the counter
WithLabels(labels ...MetricLabel) Counter
}
// SimpleGeoCounter is a simple counter that can only be incremented and is labeled with the country from a GeoIP
// lookup.
type SimpleGeoCounter interface {
// Increment increments the counter for the country from the specified ip by 1.
//
// - labels is a set of labels to apply. Can be created using the Label function.
Increment(ip net.IP, labels ...MetricLabel)
// IncrementBy increments the counter for the country from the specified ip by the specified value.
// Only returns an error if the passed by parameter is negative.
//
// - labels is a set of labels to apply. Can be created using the Label function.
IncrementBy(ip net.IP, by float64, labels ...MetricLabel) error
}
// GeoCounter extends the SimpleGeoCounter interface by adding a WithLabels function to create a copy of the counter
// that is primed with a set of labels.
type GeoCounter interface {
SimpleGeoCounter
// WithLabels adds labels to the counter
WithLabels(labels ...MetricLabel) GeoCounter
}
// SimpleGauge is a metric that can be incremented and decremented.
type SimpleGauge interface {
// Increment increments the counter by 1.
//
// - labels is a set of labels to apply. Can be created using the Label function.
Increment(labels ...MetricLabel)
// IncrementBy increments the counter by the specified number.
//
// - labels is a set of labels to apply. Can be created using the Label function.
IncrementBy(by float64, labels ...MetricLabel)
// Decrement decreases the metric by 1.
//
// - labels is a set of labels to apply. Can be created using the Label function.
Decrement(labels ...MetricLabel)
// Decrement decreases the metric by the specified value.
//
// - labels is a set of labels to apply. Can be created using the Label function.
DecrementBy(by float64, labels ...MetricLabel)
// Set sets the value of the metric to an exact value.
//
// - labels is a set of labels to apply. Can be created using the Label function.
Set(value float64, labels ...MetricLabel)
}
// Gauge extends the SimpleGauge interface by adding a WithLabels function to create a copy of the counter
// that is primed with a set of labels.
type Gauge interface {
SimpleGauge
// WithLabels adds labels to the counter
WithLabels(labels ...MetricLabel) Gauge
}
// SimpleGeoGauge is a metric that can be incremented and decremented and is labeled by the country from a GeoIP lookup.
type SimpleGeoGauge interface {
// Increment increments the counter for the country from the specified ip by 1.
//
// - labels is a set of labels to apply. Can be created using the Label function.
Increment(ip net.IP, labels ...MetricLabel)
// IncrementBy increments the counter for the country from the specified ip by the specified value.
//
// - labels is a set of labels to apply. Can be created using the Label function.
IncrementBy(ip net.IP, by float64, labels ...MetricLabel)
// Decrement decreases the value for the country looked up from the specified IP by 1.
//
// - labels is a set of labels to apply. Can be created using the Label function.
Decrement(ip net.IP, labels ...MetricLabel)
// DecrementBy decreases the value for the country looked up from the specified IP by the specified value.
//
// - labels is a set of labels to apply. Can be created using the Label function.
DecrementBy(ip net.IP, by float64, labels ...MetricLabel)
// Set sets the value of the metric for the country looked up from the specified IP.
//
// - labels is a set of labels to apply. Can be created using the Label function.
Set(ip net.IP, value float64, labels ...MetricLabel)
}
// GeoGauge extends the SimpleGeoGauge interface by adding a WithLabels function to create a copy of the counter
// that is primed with a set of labels.
type GeoGauge interface {
SimpleGeoGauge
// WithLabels adds labels to the counter
WithLabels(labels ...MetricLabel) GeoGauge
}