-
Notifications
You must be signed in to change notification settings - Fork 20
/
exporter.go
187 lines (172 loc) · 5.01 KB
/
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
// Copyright 2020 The MQTTGateway authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"strconv"
"strings"
"sync"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
var mutex sync.RWMutex
type mqttExporter struct {
client mqtt.Client
connectDesc *prometheus.Desc
metrics map[string]*prometheus.GaugeVec // hold the metrics collected
counterMetrics map[string]*prometheus.CounterVec // hold the metrics collected
metricsLabels map[string][]string // holds the labels set for each metric to be able to invalidate them
}
func newMQTTExporter() *mqttExporter {
// create a MQTT client
options := mqtt.NewClientOptions()
log.Infof("Connecting to %v", *brokerAddress)
options.AddBroker(*brokerAddress)
if *username != "" {
options.SetUsername(*username)
}
if *password != "" {
options.SetPassword(*password)
}
if *clientID != "" {
options.SetClientID(*clientID)
}
m := mqtt.NewClient(options)
if token := m.Connect(); token.Wait() && token.Error() != nil {
log.Fatal(token.Error())
}
// create an exporter
c := &mqttExporter{
client: m,
connectDesc: prometheus.NewDesc(
prometheus.BuildFQName(progname, "mqtt", "connected"),
"Is the exporter connected to mqtt broker",
nil,
nil),
}
c.metrics = make(map[string]*prometheus.GaugeVec)
c.counterMetrics = make(map[string]*prometheus.CounterVec)
c.metricsLabels = make(map[string][]string)
m.Subscribe(*topic, 2, c.receiveMessage())
return c
}
func (c *mqttExporter) Describe(ch chan<- *prometheus.Desc) {
mutex.RLock()
defer mutex.RUnlock()
ch <- c.connectDesc
for _, m := range c.counterMetrics {
m.Describe(ch)
}
for _, m := range c.metrics {
m.Describe(ch)
}
}
func (c *mqttExporter) Collect(ch chan<- prometheus.Metric) {
mutex.RLock()
defer mutex.RUnlock()
connected := 0.
if c.client.IsConnectionOpen() {
connected = 1.
}
ch <- prometheus.MustNewConstMetric(
c.connectDesc,
prometheus.GaugeValue,
connected,
)
for _, m := range c.counterMetrics {
m.Collect(ch)
}
for _, m := range c.metrics {
m.Collect(ch)
}
}
func (e *mqttExporter) receiveMessage() func(mqtt.Client, mqtt.Message) {
return func(c mqtt.Client, m mqtt.Message) {
mutex.Lock()
defer mutex.Unlock()
t := strings.TrimPrefix(m.Topic(), *prefix)
t = strings.TrimPrefix(t, "/")
parts := strings.Split(t, "/")
if len(parts)%2 == 0 {
log.Warnf("Invalid topic: %s: odd number of levels, ignoring", t)
return
}
metric_name := parts[len(parts)-1]
pushed_metric_name := fmt.Sprintf("mqtt_%s_last_pushed_timestamp", metric_name)
count_metric_name := fmt.Sprintf("mqtt_%s_push_total", metric_name)
metric_labels := parts[:len(parts)-1]
var labels []string
labelValues := prometheus.Labels{}
log.Debugf("Metric name: %v", metric_name)
for i, l := range metric_labels {
if i%2 == 1 {
continue
}
labels = append(labels, l)
labelValues[l] = metric_labels[i+1]
}
invalidate := false
if _, ok := e.metricsLabels[metric_name]; ok {
l := e.metricsLabels[metric_name]
if !compareLabels(l, labels) {
log.Warnf("Label names are different: %v and %v, invalidating existing metric", l, labels)
prometheus.Unregister(e.metrics[metric_name])
invalidate = true
}
}
e.metricsLabels[metric_name] = labels
if _, ok := e.metrics[metric_name]; ok && !invalidate {
log.Debugf("Metric already exists")
} else {
log.Debugf("Creating new metric: %s %v", metric_name, labels)
e.metrics[metric_name] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metric_name,
Help: "Metric pushed via MQTT",
},
labels,
)
e.counterMetrics[count_metric_name] = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: count_metric_name,
Help: fmt.Sprintf("Number of times %s was pushed via MQTT", metric_name),
},
labels,
)
e.metrics[pushed_metric_name] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: pushed_metric_name,
Help: fmt.Sprintf("Last time %s was pushed via MQTT", metric_name),
},
labels,
)
}
if s, err := strconv.ParseFloat(string(m.Payload()), 64); err == nil {
e.metrics[metric_name].With(labelValues).Set(s)
e.metrics[pushed_metric_name].With(labelValues).SetToCurrentTime()
e.counterMetrics[count_metric_name].With(labelValues).Inc()
}
}
}
func compareLabels(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}