-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric.go
467 lines (407 loc) · 12.9 KB
/
metric.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package main
import (
"fmt"
"log/slog"
"sort"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)
// MetricDesc is a descriptor for a family of metrics, sharing the same name, help, labes, type.
type MetricDesc interface {
Name() string
Help() string
ValueType() prometheus.ValueType
ConstLabels() []*dto.LabelPair
// Labels() []string
LogContext() []interface{}
}
//
// MetricFamily
//
// MetricFamily implements MetricDesc for SQL metrics, with logic for populating its labels and values from sql.Rows.
type MetricFamily struct {
config *MetricConfig
constLabels []*dto.LabelPair
labels []*Label // raw string or template
valueslabels []*Label // raw string or template for key and value
logContext []interface{}
}
// NewMetricFamily creates a new MetricFamily with the given metric config and const labels (e.g. job and instance).
func NewMetricFamily(
logContext []interface{},
mc *MetricConfig,
constLabels []*dto.LabelPair,
customTemplate *exporterTemplate) (*MetricFamily, error) {
var (
err error
labels []*Label
)
logContext = append(logContext, "metric", mc.Name)
if len(mc.Values) == 0 {
logContext = append(logContext, "errmsg", "NewMetricFamily(): multiple values but no value label")
return nil, fmt.Errorf("%s", logContext...)
}
if len(mc.Values) > 1 && len(mc.ValueLabel) == 0 {
logContext = append(logContext, "errmsg", "NewMetricFamily(): multiple values but no value label")
}
// all labels are stored in variable 'labels': size of slice if size of KeyLabels + 1 if size of Values is greater than 1
if len(mc.key_labels_map) > 0 {
label_len := len(mc.key_labels_map)
if len(mc.Values) > 1 {
label_len++
}
labels = make([]*Label, label_len)
i := 0
for key, val := range mc.key_labels_map {
labels[i], err = NewLabel(key, val, mc.Name, "key_label", customTemplate)
if err != nil {
return nil, err
}
i++
}
// add an element in labels for value_label (the name of the label); value will be set later
if len(mc.Values) > 1 {
labels[i], err = NewLabel(mc.ValueLabel, "", mc.Name, "value_label", customTemplate)
if err != nil {
return nil, err
}
}
}
// values are stored in variable 'valueslabels': it is meanfull only when values are greater than 1.
// original config struct is a map of [value label] : [value template];
valueslabels := make([]*Label, len(mc.Values))
i := 0
for key, val := range mc.Values {
valueslabels[i], err = NewLabel(key, val, mc.Name, "'values'", customTemplate)
if err != nil {
return nil, err
}
i++
}
// Create a copy of original slice to avoid modifying constLabels
sortedLabels := append(constLabels[:0:0], constLabels...)
for k, v := range mc.StaticLabels {
sortedLabels = append(sortedLabels, &dto.LabelPair{
Name: proto.String(k),
Value: proto.String(v),
})
}
sort.Sort(labelPairSorter(sortedLabels))
return &MetricFamily{
config: mc,
constLabels: sortedLabels,
labels: labels,
valueslabels: valueslabels,
logContext: logContext,
}, nil
}
// Collect is the equivalent of prometheus.Collector.Collect() but takes a Query output map to populate values from.
func (mf MetricFamily) Collect(rawdatas any, logger *slog.Logger, ch chan<- Metric) {
var set_root bool = false
// reset logcontxt for MetricFamily: remove previous errors if any
mf.logContext = make([]any, 2)
mf.logContext[0] = "metric"
mf.logContext[1] = mf.config.Name
item, ok := rawdatas.(map[string]any)
if !ok {
ch <- NewInvalidMetric(mf.logContext, fmt.Errorf("metrtic %s invalid type", mf.config.Name))
return
}
// set default scope to "loop_var" entry: item[item["loop_var"]]
if mf.config.Scope == "" {
if loop_var, ok := item["loop_var"].(string); ok {
if loop_var == "empty_item" {
loop_var = "item"
}
if datas, ok := item[loop_var].(map[string]any); ok {
item = datas
item["root"] = rawdatas
set_root = true
}
}
} else if mf.config.Scope != "none" {
var err error
item, err = SetScope(mf.config.Scope, item)
if err != nil {
ch <- NewInvalidMetric(mf.logContext, err)
return
}
item["root"] = rawdatas
set_root = true
}
// build the labels family with the content of the var(*Field)
if len(mf.labels) == 0 && mf.config.key_labels != nil {
if labelsmap_raw, err := ValorizeValue(item, mf.config.key_labels, logger, mf.config.Name, 0); err == nil {
if key_labels_map, ok := labelsmap_raw.(map[string]any); ok {
label_len := len(key_labels_map)
if len(mf.config.Values) > 1 {
label_len++
}
mf.labels = make([]*Label, label_len)
i := 0
for key, val_raw := range key_labels_map {
if val, ok := val_raw.(string); ok {
mf.labels[i], err = NewLabel(key, val, mf.config.Name, "key_label", nil)
if err != nil {
logger.Warn(fmt.Sprintf("invalid template for key_values for metric %s: %s (maybe use |toRawJson.)", mf.config.Name, err))
continue
}
}
i++
}
// add an element in labels for value_label (the name of the label); value will be set later
if len(mf.config.Values) > 1 {
mf.labels[i], _ = NewLabel(mf.config.ValueLabel, "", mf.config.Name, "value_label", nil)
if err != nil {
logger.Warn(fmt.Sprintf("invalid templatefor value_label for metric %s: %s (maybe use |toRawJson.)", mf.config.Name, err))
// return nil, err
}
}
}
} else {
logger.Warn(fmt.Sprintf("invalid template for key_values for metric %s: %s (maybe use |toRawJson.)", mf.config.Name, err))
}
}
labelNames := make([]string, len(mf.labels))
labelValues := make([]string, len(mf.labels))
for i, label := range mf.labels {
var err error
// last label may be null if metric has no value label, because it has only one value
if label == nil {
continue
}
if item != nil {
if label.Key != nil {
labelNames[i], err = label.Key.GetValueString(item, nil, false)
if err != nil {
ch <- NewInvalidMetric(mf.logContext, fmt.Errorf("invalid template label name metric{ name: %s, key: %s} : %s", mf.config.Name, label.Key.String(), err))
continue
}
}
if label.Value != nil {
sub := make(map[string]string)
sub["_"] = labelNames[i]
labelValues[i], err = label.Value.GetValueString(item, sub, true)
if err != nil {
ch <- NewInvalidMetric(mf.logContext, fmt.Errorf("invalid template label value metric{ name: %s, value: %s} : %s", mf.config.Name, label.Value.String(), err))
continue
}
}
} else {
labelNames[i] = fmt.Sprintf("undef_%d", i)
labelValues[i] = ""
}
i++
}
for _, label := range mf.valueslabels {
var f_value float64
var err error
// fill the label value for value_label with the current name of the value if there is a label !
if len(mf.config.Values) > 1 {
labelValues[len(labelValues)-1], err = label.Key.GetValueString(item, nil, false)
if err != nil {
ch <- NewInvalidMetric(mf.logContext, fmt.Errorf("invalid template label name metric{ name: %s, key: %s} : %s", mf.config.Name, label.Key.String(), err))
continue
}
}
f_value, err = label.Value.GetValueFloat(item)
if err != nil {
ch <- NewInvalidMetric(mf.logContext, fmt.Errorf("invalid template label value metric{ name: %s, value: %s} : %s", mf.config.Name, label.Value.String(), err))
continue
}
ch <- NewMetric(&mf, f_value, labelNames, labelValues)
}
if set_root {
delete(item, "root")
}
}
// Name implements MetricDesc.
func (mf MetricFamily) Name() string {
return mf.config.Name
}
// Help implements MetricDesc.
func (mf MetricFamily) Help() string {
return mf.config.Help
}
// ValueType implements MetricDesc.
func (mf MetricFamily) ValueType() prometheus.ValueType {
return mf.config.ValueType()
}
// ConstLabels implements MetricDesc.
func (mf MetricFamily) ConstLabels() []*dto.LabelPair {
return mf.constLabels
}
// LogContext implements MetricDesc.
func (mf MetricFamily) LogContext() []interface{} {
return mf.logContext
}
//
// automaticMetricDesc
//
// automaticMetric is a MetricDesc for automatically generated metrics (e.g. `up` and `scrape_duration`).
type automaticMetricDesc struct {
name string
help string
valueType prometheus.ValueType
labels []string
constLabels []*dto.LabelPair
logContext []interface{}
}
// NewAutomaticMetricDesc creates a MetricDesc for automatically generated metrics.
func NewAutomaticMetricDesc(
logContext []interface{}, name, help string, valueType prometheus.ValueType, constLabels []*dto.LabelPair, labels ...string) MetricDesc {
return &automaticMetricDesc{
name: name,
help: help,
valueType: valueType,
constLabels: constLabels,
labels: labels,
logContext: logContext,
}
}
// Name implements MetricDesc.
func (a automaticMetricDesc) Name() string {
return a.name
}
// Help implements MetricDesc.
func (a automaticMetricDesc) Help() string {
return a.help
}
// ValueType implements MetricDesc.
func (a automaticMetricDesc) ValueType() prometheus.ValueType {
return a.valueType
}
// ConstLabels implements MetricDesc.
func (a automaticMetricDesc) ConstLabels() []*dto.LabelPair {
return a.constLabels
}
// Labels implements MetricDesc.
func (a automaticMetricDesc) Labels() []string {
return a.labels
}
// LogContext implements MetricDesc.
func (a automaticMetricDesc) LogContext() []interface{} {
return a.logContext
}
//
// Metric
//
// A Metric models a single sample value with its meta data being exported to Prometheus.
type Metric interface {
Desc() MetricDesc
Write(out *dto.Metric) error
}
// NewMetric returns a metric with one fixed value that cannot be changed.
//
// NewMetric panics if the length of labelValues is not consistent with desc.labels().
func NewMetric(desc MetricDesc, value float64, labelNames []string, labelValues []string) Metric {
if len(labelNames) != len(labelValues) {
panic(fmt.Sprintf("[%s] expected %d labels, got %d", desc.LogContext(), len(labelNames), len(labelValues)))
}
return &constMetric{
desc: desc,
val: value,
labelPairs: makeLabelPairs(desc, labelNames, labelValues),
}
}
// constMetric is a metric with one fixed value that cannot be changed.
type constMetric struct {
desc MetricDesc
val float64
labelPairs []*dto.LabelPair
}
// Desc implements Metric.
func (m *constMetric) Desc() MetricDesc {
return m.desc
}
// Write implements Metric.
func (m *constMetric) Write(out *dto.Metric) error {
out.Label = m.labelPairs
switch t := m.desc.ValueType(); t {
case prometheus.CounterValue:
out.Counter = &dto.Counter{Value: proto.Float64(m.val)}
case prometheus.GaugeValue:
out.Gauge = &dto.Gauge{Value: proto.Float64(m.val)}
default:
var logContext []interface{}
logContext = append(logContext, m.desc.LogContext()...)
logContext = append(logContext, "errmsg", fmt.Sprintf("encountered unknown type %v", t))
return fmt.Errorf("%s", logContext...)
}
return nil
}
func makeLabelPairs(desc MetricDesc, labelNames []string, labelValues []string) []*dto.LabelPair {
labels := labelNames
constLabels := desc.ConstLabels()
totalLen := len(labels) + len(constLabels)
if totalLen == 0 {
// Super fast path.
return nil
}
if len(labels) == 0 {
// Moderately fast path.
return constLabels
}
labelPairs := make([]*dto.LabelPair, 0, totalLen)
for i, label := range labels {
labelPairs = append(labelPairs, &dto.LabelPair{
Name: proto.String(label),
Value: proto.String(labelValues[i]),
})
}
labelPairs = append(labelPairs, constLabels...)
sort.Sort(labelPairSorter(labelPairs))
return labelPairs
}
// labelPairSorter implements sort.Interface.
// It provides a sortable version of a slice of dto.LabelPair pointers.
type labelPairSorter []*dto.LabelPair
func (s labelPairSorter) Len() int {
return len(s)
}
func (s labelPairSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s labelPairSorter) Less(i, j int) bool {
return s[i].GetName() < s[j].GetName()
}
type invalidMetric struct {
logContext []interface{}
err error
}
// NewInvalidMetric returns a metric whose Write method always returns the provided error.
func NewInvalidMetric(logContext []interface{}, err error) Metric {
return invalidMetric{
logContext: logContext,
err: err,
}
}
func (m invalidMetric) Desc() MetricDesc { return nil }
func (m invalidMetric) Write(*dto.Metric) error {
return m.err
}
type Label struct {
Key *Field
Value *Field
}
func NewLabel(key string, value string, mName string, errStr string, customTemplate *exporterTemplate) (*Label, error) {
var (
keyField, valueField *Field
err error
)
keyField, err = NewField(key, customTemplate)
if err != nil {
return nil, fmt.Errorf("NewMetricFamily(): name of %s for metric %q: %s", errStr, mName, err)
}
if value != "" {
valueField, err = NewField(value, customTemplate)
if err != nil {
return nil, fmt.Errorf("NewMetricFamily(): value of %s for metric %q: %s", errStr, mName, err)
}
}
return &Label{
Key: keyField,
Value: valueField,
}, nil
}