-
Notifications
You must be signed in to change notification settings - Fork 10
/
prometheus_exporter.go
221 lines (187 loc) · 5.04 KB
/
prometheus_exporter.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
package main
import (
"fmt"
"net/http"
"regexp"
"sort"
"strings"
"sync"
"time"
na_pb "github.com/Juniper/jtimon/telemetry"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
promNameRegex = regexp.MustCompile(`[^a-zA-Z0-9_]`)
)
// Prometheus does not like special characters, handle them.
func promName(input string) string {
return promNameRegex.ReplaceAllString(input, "_")
}
// Convert the XML path of a field to a tag
// Example: /interfaces/interface/state/oper-status
// to /interfaces/interface/state/@oper-status
func convertToTag(input string) string {
lastIndex := strings.LastIndex(input, "/");
if lastIndex != -1 {
input = input[:lastIndex] + "/@" + input[lastIndex + 1:]
}
return input
}
type jtimonMetric struct {
mapKey string
metricName string
metricLabels map[string]string
metricValue float64
metricExpiration time.Time
}
type jtimonPExporter struct {
m map[string]*jtimonMetric
mu sync.Mutex
ch chan *jtimonMetric
}
func newJTIMONPExporter() *jtimonPExporter {
return (&jtimonPExporter{
ch: make(chan *jtimonMetric),
m: map[string]*jtimonMetric{},
})
}
func (c *jtimonPExporter) processJTIMONMetric() {
ticker := time.NewTicker(time.Minute).C
for {
select {
case s := <-c.ch:
c.mu.Lock()
c.m[s.mapKey] = s
c.mu.Unlock()
case <-ticker:
ageLimit := time.Now().Add(-time.Minute)
c.mu.Lock()
for k, metric := range c.m {
if ageLimit.After(metric.metricExpiration) {
delete(c.m, k)
}
}
c.mu.Unlock()
}
}
}
// Collect implements prometheus.Collector
func (c *jtimonPExporter) Collect(ch chan<- prometheus.Metric) {
c.mu.Lock()
metrics := make([]*jtimonMetric, 0, len(c.m))
for _, metric := range c.m {
metrics = append(metrics, metric)
}
c.mu.Unlock()
for _, metric := range metrics {
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(metric.metricName, "JTIMON Metric", []string{}, metric.metricLabels),
prometheus.UntypedValue,
metric.metricValue,
)
}
}
// Describe implements prometheus.Describe
func (c *jtimonPExporter) Describe(ch chan<- *prometheus.Desc) {
prometheus.NewGauge(prometheus.GaugeOpts{Name: "Dummy", Help: "Dummy"}).Describe(ch)
}
func getMapKey(metric *jtimonMetric) string {
labels := make([]string, 0, len(metric.metricLabels))
for k := range metric.metricLabels {
labels = append(labels, k)
}
sort.Strings(labels)
mapKey := make([]string, 0, len(metric.metricLabels)*2+1)
mapKey = append(mapKey, metric.metricName)
for _, l := range labels {
mapKey = append(mapKey, l, metric.metricLabels[l])
}
return fmt.Sprintf("%q", mapKey)
}
func addPrometheus(ocData *na_pb.OpenConfigData, jctx *JCtx) {
exporter := jctx.pExporter
cfg := jctx.config
prefix := ""
for _, v := range ocData.Kv {
switch {
case v.Key == "__prefix__":
prefix = v.GetStrValue()
continue
case strings.HasPrefix(v.Key, "__"):
continue
}
key := v.Key
if !strings.HasPrefix(key, "/") {
key = prefix + v.Key
}
field, tags := spitTagsNPath(jctx, key)
tags["device"] = cfg.Host
var fieldValue float64
switch v.Value.(type) {
case *na_pb.KeyValue_DoubleValue:
fieldValue = v.GetDoubleValue()
case *na_pb.KeyValue_IntValue:
fieldValue = float64(v.GetIntValue())
case *na_pb.KeyValue_UintValue:
fieldValue = float64(v.GetUintValue())
case *na_pb.KeyValue_SintValue:
fieldValue = float64(v.GetSintValue())
case *na_pb.KeyValue_BoolValue:
boolValue := v.GetBoolValue()
if boolValue {
fieldValue = 1
} else {
fieldValue = 0
}
case *na_pb.KeyValue_StrValue:
// store leaf of type string as the tag
leafStr := convertToTag(field)
if _, ok := tags[leafStr]; !ok {
tags[leafStr] = v.GetStrValue()
} else {
continue // do not insert existing tag
}
fieldValue = 0 // insert the leaf with 0 as float value
case *na_pb.KeyValue_LeaflistValue:
e := v.GetLeaflistValue().Element
csLLStrValue := ""
for _, elem := range e {
switch elem.Value.(type) {
case *na_pb.TypedValue_LeaflistStrValue:
llStrValue := elem.GetLeaflistStrValue()
csLLStrValue = csLLStrValue + llStrValue + ","
}
}
if csLLStrValue != "" {
csLLStrValue = strings.TrimSuffix(csLLStrValue, ",")
}
tags["csLLStrValue"] = csLLStrValue // comma separated leaflist string value as a tag
fieldValue = 0 // and ofcourse the value is useless so we set it to 0
default:
continue
}
metric := &jtimonMetric{
metricName: promName(field),
metricExpiration: time.Now(),
metricValue: fieldValue,
metricLabels: map[string]string{},
}
for k, v := range tags {
metric.metricLabels[promName(k)] = v
}
metric.mapKey = getMapKey(metric)
exporter.ch <- metric
}
}
func promInit() *jtimonPExporter {
c := newJTIMONPExporter()
prometheus.MustRegister(c)
go func() {
go c.processJTIMONMetric()
addr := fmt.Sprintf("%s:%d", *promHost, *promPort)
http.Handle("/metrics", promhttp.Handler())
fmt.Println(http.ListenAndServe(addr, nil))
}()
return c
}