-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsyslog_stats.go
324 lines (265 loc) · 8.22 KB
/
rsyslog_stats.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Export rsyslog counters as prometheus metrics
*
* Copyright (c) 2021, Yury Bushmelev <jay4mail@gmail.com>
* All rights reserved.
*
* 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 (
"encoding/json"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
// Sanitise metric name
func sanitiseMetricName(name string) string {
reNonAlNum := regexp.MustCompile("[^_a-zA-Z0-9]")
reUnderscores := regexp.MustCompile("_+")
nn := strings.ToLower(name)
// replace all non-alnum chars by underscore
nn = reNonAlNum.ReplaceAllLiteralString(nn, "_")
// squash multiple underscores
nn = reUnderscores.ReplaceAllLiteralString(nn, "_")
// strip trailing underscore
nn = strings.TrimRight(nn, "_")
return nn
}
// Split dynstats counter stats by "." from right
func splitRight(str string) (string, string) {
i := strings.LastIndexAny(str, ".")
return str[:i], str[i+1:]
}
func appendMetric(m RsyslogStatsMetrics, metricName string, labels RsyslogStatsLabels, value interface{}) RsyslogStatsMetrics {
saneMetricName := sanitiseMetricName(metricName)
saneValue := RsyslogStatsValue(value.(float64))
if _, found := m[saneMetricName]; !found {
m[saneMetricName] = make(RsyslogStatsLabeledValues)
}
m[saneMetricName][labels] = saneValue
return m
}
func getValue(value interface{}) (rv float64, e error) {
switch v := value.(type) {
case float64:
rv = v
case string:
rv, e = strconv.ParseFloat(v, 64)
default:
e = fmt.Errorf("cannot convert '%T' to float64: %w", value, strconv.ErrSyntax)
}
return rv, e
}
// RsyslogStatsValue is the metric value type
type RsyslogStatsValue int
// RsyslogStatsLabels holds the metric value labels
// Label: {name="main Q"} -> { Name: "name", Value: "main Q" }
// Just one label per value is used at the moment
type RsyslogStatsLabels struct {
Name string
Value string
}
// RsyslogStatsLabeledValues is the map of labeled metric values
// Map of metric values with their labels: { {name="main Q"}: 123, ...}
type RsyslogStatsLabeledValues map[RsyslogStatsLabels]RsyslogStatsValue
// RsyslogStatsMetrics holds the metrics with their labeled values
// Map of metrics: '{ "rsyslog_core_queue_discarded_full": { {"name":"main Q"}: 123 }, ... }, ...'
type RsyslogStatsMetrics map[string]RsyslogStatsLabeledValues
// RsyslogStats is the main structure to store the rsyslog metrics
type RsyslogStats struct {
sync.RWMutex
Metrics RsyslogStatsMetrics
ParserFailures int
ParsedMessages int
ParseTimestamp int64
MetricPrefix string
NameField string
OriginField string
parsersByType map[rsyslogStatType]parserForType
}
// NewRsyslogStats is the RsyslogStats constructor
func NewRsyslogStats() *RsyslogStats {
rs := new(RsyslogStats)
rs.MetricPrefix = "rsyslog"
rs.NameField = "name"
rs.OriginField = "origin"
rs.ParserFailures = 0
rs.ParsedMessages = 0
rs.Metrics = make(RsyslogStatsMetrics)
rs.parsersByType = map[rsyslogStatType]parserForType{
rtDynstatGlobal: rs.parseDynstatsGlobal,
rtDynstatBucket: rs.parseDynstatsBucket,
rtSender: rs.parseSenderStats,
rtNamed: rs.parseNamedStats,
rtDefault: rs.parseDefault,
}
return rs
}
// Add collected metrics from `m`
func (rs *RsyslogStats) add(m RsyslogStatsMetrics) {
for metric, data := range m {
rs.Lock()
for labels, value := range data {
if _, found := rs.Metrics[metric]; !found {
rs.Metrics[metric] = RsyslogStatsLabeledValues{}
}
rs.Metrics[metric][labels] = value
}
rs.Unlock()
}
}
// Parsing error wrapper
func (rs *RsyslogStats) failToParse(err error, source string) {
log.Printf("%s! JSON string is %s", err, source)
rs.ParserFailures++
}
// Parsers
type rsyslogStatType int32
const (
rtDefault rsyslogStatType = iota
rtDynstatGlobal
rtDynstatBucket
rtNamed
rtSender
)
type parserForType func(string, string, map[string]interface{}) (RsyslogStatsMetrics, []error)
// Parse global dynstats counters
func (rs *RsyslogStats) parseDynstatsGlobal(name, origin string, data map[string]interface{}) (RsyslogStatsMetrics, []error) {
m := RsyslogStatsMetrics{}
metricName := rs.MetricPrefix + "_" + origin + "_" + name
for field, value := range data["values"].(map[string]interface{}) {
cname, counter := splitRight(field)
appendMetric(m, metricName+"_"+counter, RsyslogStatsLabels{"counter", cname}, value)
}
return m, nil
}
// Parse dynstats.bucket counters
func (rs *RsyslogStats) parseDynstatsBucket(name, origin string, data map[string]interface{}) (RsyslogStatsMetrics, []error) {
m := RsyslogStatsMetrics{}
metricName := rs.MetricPrefix + "_" + origin + "_" + name
for counter, value := range data["values"].(map[string]interface{}) {
appendMetric(m, metricName, RsyslogStatsLabels{"bucket", counter}, value)
}
return m, nil
}
// Parse sender stats
func (rs *RsyslogStats) parseSenderStats(name, origin string, data map[string]interface{}) (RsyslogStatsMetrics, []error) {
errs := []error{}
v, e := getValue(data["messages"])
if e != nil {
return nil, append(errs, e)
}
m := RsyslogStatsMetrics{}
l := RsyslogStatsLabels{"sender", data["sender"].(string)}
metricName := rs.MetricPrefix + "_" + "sender_stat_messages"
appendMetric(m, metricName, l, v)
return m, nil
}
// Parse "named" counters (core.queue, core.action)
func (rs *RsyslogStats) parseNamedStats(name, origin string, data map[string]interface{}) (RsyslogStatsMetrics, []error) {
errs := []error{}
m := RsyslogStatsMetrics{}
l := RsyslogStatsLabels{"name", name}
metricName := rs.MetricPrefix + "_" + origin
for counter, value := range data {
if counter == rs.NameField || counter == rs.OriginField {
continue
}
if v, e := getValue(value); e != nil {
errs = append(errs, e)
} else {
appendMetric(m, metricName+"_"+counter, l, v)
}
}
return m, errs
}
// Parse common (unlabeled) counters
func (rs *RsyslogStats) parseDefault(name, origin string, data map[string]interface{}) (RsyslogStatsMetrics, []error) {
errs := []error{}
m := RsyslogStatsMetrics{}
l := RsyslogStatsLabels{}
metricName := rs.MetricPrefix + "_" + origin + "_" + name
for counter, value := range data {
if counter == rs.NameField || counter == rs.OriginField {
continue
}
if v, e := getValue(value); e != nil {
errs = append(errs, e)
} else {
appendMetric(m, metricName+"_"+counter, l, v)
}
}
return m, errs
}
// Identify statLine type
func (rs *RsyslogStats) identify(data map[string]interface{}) (name string, origin string, st rsyslogStatType, e error) {
var found bool
name, found = data[rs.NameField].(string)
if !found {
e = fmt.Errorf("'%s' field is required but not found", rs.NameField)
}
origin, found = data[rs.OriginField].(string)
if !found {
switch name {
case "omkafka": // omkafka missing origin hack (issue #1508, pre-8.27)
origin = "omkafka"
case "_sender_stat": // senders.keepTrack stats hack - https://github.com/rsyslog/rsyslog/pull/4601
origin = "impstats"
default:
e = fmt.Errorf("'%s' field is required but not found", rs.OriginField)
}
}
st = rtNamed // default type
switch origin {
case "dynstats":
st = rtDynstatGlobal
case "dynstats.bucket":
st = rtDynstatBucket
default:
switch name {
case "_sender_stat":
st = rtSender
}
}
return
}
// Parse JSON line and store metrics
func (rs *RsyslogStats) Parse(statLine string) {
var (
data map[string]interface{}
name string
origin string
)
err := json.Unmarshal([]byte(statLine), &data)
if err != nil {
rs.failToParse(fmt.Errorf("cannot parse JSON: %w", err), statLine)
return
}
name, origin, rsType, err := rs.identify(data)
if err != nil {
rs.failToParse(err, statLine)
return
}
m, errs := rs.parsersByType[rsType](name, origin, data)
for _, e := range errs {
rs.failToParse(e, statLine)
}
rs.add(m)
rs.ParsedMessages++
rs.ParseTimestamp = time.Now().Unix()
}