This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
prometheus_common.go
153 lines (142 loc) · 3.52 KB
/
prometheus_common.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
package prometheus_output
import (
"errors"
"math"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/karimra/gnmic/formatters"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/prompb"
)
const (
metricNameRegex = "[^a-zA-Z0-9_]+"
)
var (
MetricNameRegex = regexp.MustCompile(metricNameRegex)
)
type MetricBuilder struct {
Prefix string
AppendSubscriptionName bool
StringsAsLabels bool
}
func (m *MetricBuilder) GetLabels(ev *formatters.EventMsg) []prompb.Label {
labels := make([]prompb.Label, 0, len(ev.Tags))
addedLabels := make(map[string]struct{})
for k, v := range ev.Tags {
labelName := MetricNameRegex.ReplaceAllString(filepath.Base(k), "_")
if _, ok := addedLabels[labelName]; ok {
continue
}
labels = append(labels, prompb.Label{Name: labelName, Value: v})
addedLabels[labelName] = struct{}{}
}
if !m.StringsAsLabels {
return labels
}
var err error
for k, v := range ev.Values {
_, err = toFloat(v)
if err == nil {
continue
}
if vs, ok := v.(string); ok {
labelName := MetricNameRegex.ReplaceAllString(filepath.Base(k), "_")
if _, ok := addedLabels[labelName]; ok {
continue
}
labels = append(labels, prompb.Label{Name: labelName, Value: vs})
}
}
return labels
}
func toFloat(v interface{}) (float64, error) {
switch i := v.(type) {
case float64:
return float64(i), nil
case float32:
return float64(i), nil
case int64:
return float64(i), nil
case int32:
return float64(i), nil
case int16:
return float64(i), nil
case int8:
return float64(i), nil
case uint64:
return float64(i), nil
case uint32:
return float64(i), nil
case uint16:
return float64(i), nil
case uint8:
return float64(i), nil
case int:
return float64(i), nil
case uint:
return float64(i), nil
case string:
f, err := strconv.ParseFloat(i, 64)
if err != nil {
return math.NaN(), err
}
return f, err
case *gnmi.Decimal64:
return float64(i.Digits) / math.Pow10(int(i.Precision)), nil
default:
return math.NaN(), errors.New("getFloat: unknown value is of incompatible type")
}
}
// MetricName generates the prometheus metric name based on the output plugin,
// the measurement name and the value name.
// it makes sure the name matches the regex "[^a-zA-Z0-9_]+"
func (m *MetricBuilder) MetricName(measName, valueName string) string {
sb := strings.Builder{}
if m.Prefix != "" {
sb.WriteString(MetricNameRegex.ReplaceAllString(m.Prefix, "_"))
sb.WriteString("_")
}
if m.AppendSubscriptionName {
sb.WriteString(strings.TrimRight(MetricNameRegex.ReplaceAllString(measName, "_"), "_"))
sb.WriteString("_")
}
sb.WriteString(strings.TrimLeft(MetricNameRegex.ReplaceAllString(valueName, "_"), "_"))
return sb.String()
}
type NamedTimeSeries struct {
Name string
TS *prompb.TimeSeries
}
func (m *MetricBuilder) TimeSeriesFromEvent(ev *formatters.EventMsg) []*NamedTimeSeries {
promTS := make([]*NamedTimeSeries, 0, len(ev.Values))
tsLabels := m.GetLabels(ev)
for k, v := range ev.Values {
fv, err := toFloat(v)
if err != nil {
continue
}
tsName := m.MetricName(ev.Name, k)
nts := &NamedTimeSeries{
Name: tsName,
TS: &prompb.TimeSeries{
Labels: append(tsLabels,
prompb.Label{
Name: labels.MetricName,
Value: m.MetricName(ev.Name, k),
}),
Samples: []prompb.Sample{
{
Value: fv,
Timestamp: ev.Timestamp / int64(time.Millisecond),
},
},
},
}
promTS = append(promTS, nts)
}
return promTS
}