-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilters.go
138 lines (131 loc) · 4.65 KB
/
filters.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
/*This file is part of sisyphus.
*
* Copyright Datto, Inc.
* Author: John Seekins <jseekins@datto.com>
*
* Licensed under the GNU General Public License Version 3
* Fedora-License-Identifier: GPLv3+
* SPDX-2.0-License-Identifier: GPL-3.0+
* SPDX-3.0-License-Identifier: GPL-3.0-or-later
*
* sisyphus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sisyphus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sisyphus. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"fmt"
"regexp"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
var (
allowedNames = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9_:]*$")
allowedFirstChar = regexp.MustCompile("^[a-zA-Z]")
allowedFields = regexp.MustCompile("^[a-zA-Z0-9_:]*$")
replaceChars = regexp.MustCompile("[^a-zA-Z0-9_:]")
allowedTagKeys = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9_]*$")
)
func filterMsg(thread int, msg InfluxMetric, normalize bool) (InfluxMetric, error) {
FilterTimeStart := time.Now()
finalMsg := InfluxMetric{
Name: "", Fields: make(map[string]interface{}),
Tags: make(map[string]string), Timestamp: msg.Timestamp,
}
var name string
// Step one in filtering: make sure metrics follow prometheus data model (https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels)
if !allowedFirstChar.MatchString(msg.Name) {
DroppedMsgs.Inc()
log.WithFields(log.Fields{"threadNum": thread, "name": msg.Name, "metric": msg, "section": "filter"}).Warning("Dropped! Bad first character in name")
FilterTime.Add(float64(time.Now().Sub(FilterTimeStart)) / TimeSegmentDivisor)
return InfluxMetric{}, fmt.Errorf("Dropped metric with bad name")
}
// Also check for metrics with no fields (this occasionally happens with histograms from prometheus data sources)
if len(msg.Fields) < 1 {
DroppedMsgs.Inc()
log.WithFields(log.Fields{"threadNum": thread, "name": msg.Name, "metric": msg, "section": "filter"}).Warning("Dropped! No fields in incoming metric")
FilterTime.Add(float64(time.Now().Sub(FilterTimeStart)) / TimeSegmentDivisor)
return InfluxMetric{}, fmt.Errorf("Dropped metric with bad name")
}
if normalize {
name = strings.ToLower(msg.Name)
} else {
name = msg.Name
}
// replace bad characters in metric name with _ per the data model
if !allowedNames.MatchString(name) {
FilterSteps.Inc()
finalMsg.Name = replaceChars.ReplaceAllString(name, "_")
} else {
finalMsg.Name = name
}
// replace bad characters in tag keys with _ per the data model
for key, value := range msg.Tags {
if normalize {
key = strings.ToLower(key)
value = strings.ToLower(value)
}
if !allowedTagKeys.MatchString(key) {
FilterSteps.Inc()
key = replaceChars.ReplaceAllString(key, "_")
}
// tags that start with __ are considered custom stats for internal prometheus stuff, we should drop them
if strings.HasPrefix(key, "__") {
FilterSteps.Inc()
} else {
finalMsg.Tags[key] = value
}
}
// replace bad characters in field names with _ per the data model
for key, value := range msg.Fields {
if normalize {
key = strings.ToLower(key)
}
if !allowedFields.MatchString(key) {
FilterSteps.Inc()
key = replaceChars.ReplaceAllString(key, "_")
finalMsg.Fields[key] = value
} else {
finalMsg.Fields[key] = value
}
}
MetricsCounted.Add(len(finalMsg.Fields))
FilterTime.Add(float64(time.Now().Sub(FilterTimeStart)) / TimeSegmentDivisor)
return finalMsg, nil
}
// FilterMessages is our main loop for ensuring incoming messages match our expected format
func FilterMessages(ctx context.Context, thread int, inChannel chan InfluxMetric, outChannel chan InfluxMetric, wg *sync.WaitGroup, normalize bool) {
log.WithFields(log.Fields{"threadNum": thread, "section": "filter"}).Info("Starting Message Filtering thread...")
defer wg.Done()
filterloop:
for {
select {
case msg := <-inChannel:
output, err := filterMsg(thread, msg, normalize)
if err == nil {
outChannel <- output
}
case <-ctx.Done():
log.WithFields(log.Fields{"threadNum": thread, "section": "filter"}).Info("Closing filter thread...")
for msg := range inChannel {
output, err := filterMsg(thread, msg, normalize)
if err == nil {
outChannel <- output
}
}
break filterloop
}
}
}