-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingestor.go
289 lines (225 loc) · 6.84 KB
/
ingestor.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
package main
import (
"bytes"
"compress/zlib"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
paho "github.com/eclipse/paho.mqtt.golang"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
"github.com/rs/zerolog/log"
)
const (
// Delay between connection attempts to MQTT.
mqttRetryDelay = 10 * time.Second
// Delay between write attempts to the remote storage.
storageRetryDelay = 10 * time.Second
)
var (
errParseFQDN = errors.New("could not parse FQDN")
errNotPem = errors.New("not a PEM file")
)
var dataTopicRegex = regexp.MustCompile("^v1/agent/(.*)/data")
// Ingestor reads metrics from MQTT and writes them to a remote storage.
type Ingestor struct {
client paho.Client
writer *Writer
opts Options
// It's bad practise to store a context in a struct,
// but we need to use it in paho callbacks.
ctx context.Context //nolint:containedctx
}
type metricPayload struct {
LabelsText string `json:"labels_text"`
TimestampMS int64 `json:"time_ms"`
Value float64 `json:"value"`
}
// NewIngestor returns a new initialized ingestor.
func NewIngestor(opts Options) *Ingestor {
c := &Ingestor{
opts: opts,
writer: NewWriter(opts.RemoteWriteURL),
}
pahoOpts := paho.NewClientOptions()
pahoOpts.SetCleanSession(false)
pahoOpts.SetClientID(opts.MQTTUsername)
pahoOpts.SetUsername(opts.MQTTUsername)
pahoOpts.SetPassword(opts.MQTTPassword)
pahoOpts.SetOnConnectHandler(c.onConnect)
pahoOpts.SetConnectionLostHandler(onConnectionLost)
for _, broker := range opts.MQTTBrokerURL {
pahoOpts.AddBroker(broker)
}
if opts.MQTTSSLInsecure || opts.MQTTCAFile != "" {
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: opts.MQTTSSLInsecure, //nolint:gosec // G402: TLS InsecureSkipVerify set true.
}
if opts.MQTTCAFile != "" {
if rootCAs, err := loadRootCAs(opts.MQTTCAFile); err != nil {
log.Err(err).Msgf("Unable to load CAs from %s", opts.MQTTCAFile)
} else {
tlsConfig.RootCAs = rootCAs
}
}
pahoOpts.SetTLSConfig(tlsConfig)
}
c.client = paho.NewClient(pahoOpts)
return c
}
func loadRootCAs(caFile string) (*x509.CertPool, error) {
rootCAs := x509.NewCertPool()
certs, err := os.ReadFile(caFile)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
ok := rootCAs.AppendCertsFromPEM(certs)
if !ok {
return nil, errNotPem
}
return rootCAs, nil
}
// Run starts receiving metrics from MQTT and writing them to the remote storage.
func (c *Ingestor) Run(ctx context.Context) {
c.ctx = ctx
c.connect(ctx)
<-ctx.Done()
}
func (c *Ingestor) connect(ctx context.Context) {
err := c.connectOnce(ctx)
for err != nil && ctx.Err() == nil {
log.Warn().Err(err).Msgf("Failed to connect to MQTT, retry in %s", mqttRetryDelay)
select {
case <-time.After(mqttRetryDelay):
case <-ctx.Done():
return
}
err = c.connectOnce(ctx)
}
}
func (c *Ingestor) connectOnce(ctx context.Context) error {
// Use a timeout to return early if the context is canceled.
const timeout = time.Second
token := c.client.Connect()
isTimeout := !token.WaitTimeout(timeout)
for isTimeout && ctx.Err() == nil {
isTimeout = !token.WaitTimeout(timeout)
}
if token.Error() != nil {
return fmt.Errorf("connect: %w", token.Error())
}
return nil
}
func (c *Ingestor) onConnect(_ paho.Client) {
log.Info().Msg("MQTT connection established")
topic := "v1/agent/+/data"
if c.opts.MQTTID != "" {
topic = fmt.Sprintf("%s/%s", topic, c.opts.MQTTID)
}
token := c.client.Subscribe(topic, 1, c.onMessage)
token.Wait()
// If there is an error, the client should reconnect so the subscription will be retried.
if token.Error() != nil {
log.Err(token.Error()).Msgf("Failed to subscribe")
}
}
func onConnectionLost(_ paho.Client, err error) {
log.Warn().Err(err).Msg("MQTT connection lost")
}
func (c *Ingestor) onMessage(_ paho.Client, m paho.Message) {
fqdn, err := fqdnFromTopic(m.Topic())
if err != nil {
log.Warn().Err(err).Msg("Skip data: %v")
return
}
// Decode the zlib encoded JSON payload.
var metrics []metricPayload
err = decode(m.Payload(), &metrics)
if err != nil {
log.Warn().Err(err).Msg("Failed to decode payload")
return
}
log.Debug().Str("instance", fqdn).Msgf("Received %d points", len(metrics))
// Convert the metrics to samples.
samples := make([]sample, 0, len(metrics))
for _, metric := range metrics {
lbls := textToLabels(metric.LabelsText)
// Replace the "instance" label of the metrics by the FQDN contained in the topic.
// MQTT topic can use authentication, so we can trust the topic name.
builder := labels.NewBuilder(lbls)
builder.Set("instance", fqdn)
samples = append(samples, sample{
labels: builder.Labels(),
value: metric.Value,
timestamp: metric.TimestampMS,
})
}
// Write the samples to the remote storage.
err = c.writer.Write(c.ctx, samples)
for err != nil && c.ctx.Err() == nil {
log.Warn().Err(err).Msgf("Failed to write points to the remote storage, retry in %s", storageRetryDelay)
select {
case <-time.After(storageRetryDelay):
case <-c.ctx.Done():
}
err = c.writer.Write(context.Background(), samples)
if err == nil {
log.Info().Msg("Writing to remote storage recovered, resuming normal operation")
}
}
}
// Get the server FQDN from the MQTT topic.
// The topic is expected to be of the form "v1/agent/fqdn/data".
func fqdnFromTopic(topic string) (string, error) {
matches := dataTopicRegex.FindStringSubmatch(topic)
if len(matches) == 2 {
// Glouton replaces '.' with ',' in the FQDN so it can be used
// in a NATS topic, convert it back to a '.'.
topic := strings.ReplaceAll(matches[1], ",", ".")
return topic, nil
}
return "", fmt.Errorf("topic %s: %w", topic, errParseFQDN)
}
// textToLabels converts labels text to a list of label.
func textToLabels(text string) labels.Labels {
lbls, err := parser.ParseMetricSelector("{" + text + "}")
if err != nil {
log.Warn().Err(err).Msgf("Failed to decode labels '%s'", text)
return nil
}
results := make(labels.Labels, 0, len(lbls))
for _, v := range lbls {
results = append(results, labels.Label{Name: v.Name, Value: v.Value})
}
return results
}
// Decode a zlib compressed JSON payload.
func decode(input []byte, obj interface{}) error {
decoder, err := zlib.NewReader(bytes.NewReader(input))
if err != nil {
return fmt.Errorf("zlib reader: %w", err)
}
err = json.NewDecoder(decoder).Decode(obj)
if err != nil {
return fmt.Errorf("decode JSON: %w", err)
}
//nolint:gosec // G110: Potential DoS vulnerability via a decompression bomb.
// False positive: copying to discard can't lead to memory exhaustion.
_, err = io.Copy(io.Discard, decoder)
if err != nil {
return fmt.Errorf("copy: %w", err)
}
if err := decoder.Close(); err != nil {
return fmt.Errorf("close decoder: %w", err)
}
return nil
}