/
processor_metric.go
335 lines (294 loc) · 9.41 KB
/
processor_metric.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
325
326
327
328
329
330
331
332
333
334
335
package pure
import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/benthosdev/benthos/v4/internal/bloblang/field"
"github.com/benthosdev/benthos/v4/internal/bundle"
"github.com/benthosdev/benthos/v4/internal/component/metrics"
"github.com/benthosdev/benthos/v4/internal/component/processor"
"github.com/benthosdev/benthos/v4/internal/docs"
"github.com/benthosdev/benthos/v4/internal/log"
"github.com/benthosdev/benthos/v4/internal/message"
)
func init() {
err := bundle.AllProcessors.Add(func(conf processor.Config, mgr bundle.NewManagement) (processor.V1, error) {
return newMetricProcessor(conf, mgr, mgr.Logger(), mgr.Metrics())
}, docs.ComponentSpec{
Name: "metric",
Categories: []string{
"Utility",
},
Summary: "Emit custom metrics by extracting values from messages.",
Description: `
This processor works by evaluating an [interpolated field ` + "`value`" + `](/docs/configuration/interpolation#bloblang-queries) for each message and updating a emitted metric according to the [type](#types).
Custom metrics such as these are emitted along with Benthos internal metrics, where you can customize where metrics are sent, which metric names are emitted and rename them as/when appropriate. For more information check out the [metrics docs here](/docs/components/metrics/about).`,
Config: docs.FieldComponent().WithChildren(
docs.FieldString("type", "The metric [type](#types) to create.").HasOptions(
"counter",
"counter_by",
"gauge",
"timing",
),
docs.FieldString("name", "The name of the metric to create, this must be unique across all Benthos components otherwise it will overwrite those other metrics."),
docs.FieldString(
"labels", "A map of label names and values that can be used to enrich metrics. Labels are not supported by some metric destinations, in which case the metrics series are combined.",
map[string]string{
"type": "${! json(\"doc.type\") }",
"topic": "${! meta(\"kafka_topic\") }",
},
).IsInterpolated().Map(),
docs.FieldString("value", "For some metric types specifies a value to set, increment.").IsInterpolated(),
).ChildDefaultAndTypesFromStruct(processor.NewMetricConfig()),
Examples: []docs.AnnotatedExample{
{
Title: "Counter",
Summary: "In this example we emit a counter metric called `Foos`, which increments for every message processed, and we label the metric with some metadata about where the message came from and a field from the document that states what type it is. We also configure our metrics to emit to CloudWatch, and explicitly only allow our custom metric and some internal Benthos metrics to emit.",
Config: `
pipeline:
processors:
- metric:
name: Foos
type: counter
labels:
topic: ${! meta("kafka_topic") }
partition: ${! meta("kafka_partition") }
type: ${! json("document.type").or("unknown") }
metrics:
mapping: |
root = if ![
"Foos",
"input_received",
"output_sent"
].contains(this) { deleted() }
aws_cloudwatch:
namespace: ProdConsumer
`,
},
{
Title: "Gauge",
Summary: "In this example we emit a gauge metric called `FooSize`, which is given a value extracted from JSON messages at the path `foo.size`. We then also configure our Prometheus metric exporter to only emit this custom metric and nothing else. We also label the metric with some metadata.",
Config: `
pipeline:
processors:
- metric:
name: FooSize
type: gauge
labels:
topic: ${! meta("kafka_topic") }
value: ${! json("foo.size") }
metrics:
mapping: 'if this != "FooSize" { deleted() }'
prometheus: {}
`,
},
},
Footnotes: `
## Types
### ` + "`counter`" + `
Increments a counter by exactly 1, the contents of ` + "`value`" + ` are ignored
by this type.
### ` + "`counter_by`" + `
If the contents of ` + "`value`" + ` can be parsed as a positive integer value
then the counter is incremented by this value.
For example, the following configuration will increment the value of the
` + "`count.custom.field` metric by the contents of `field.some.value`" + `:
` + "```yaml" + `
pipeline:
processors:
- metric:
type: counter_by
name: CountCustomField
value: ${!json("field.some.value")}
` + "```" + `
### ` + "`gauge`" + `
If the contents of ` + "`value`" + ` can be parsed as a positive integer value
then the gauge is set to this value.
For example, the following configuration will set the value of the
` + "`gauge.custom.field` metric to the contents of `field.some.value`" + `:
` + "```yaml" + `
pipeline:
processors:
- metric:
type: gauge
name: GaugeCustomField
value: ${!json("field.some.value")}
` + "```" + `
### ` + "`timing`" + `
Equivalent to ` + "`gauge`" + ` where instead the metric is a timing. It is recommended that timing values are recorded in nanoseconds in order to be consistent with standard Benthos timing metrics, as in some cases these values are automatically converted into other units such as when exporting timings as histograms with Prometheus metrics.`,
})
if err != nil {
panic(err)
}
}
type metricProcessor struct {
conf processor.Config
log log.Modular
stats metrics.Type
value *field.Expression
labels labels
mCounter metrics.StatCounter
mGauge metrics.StatGauge
mTimer metrics.StatTimer
mCounterVec metrics.StatCounterVec
mGaugeVec metrics.StatGaugeVec
mTimerVec metrics.StatTimerVec
handler func(string, int, *message.Batch) error
}
type labels []label
type label struct {
name string
value *field.Expression
}
func (l *label) val(index int, msg *message.Batch) string {
return l.value.String(index, msg)
}
func (l labels) names() []string {
var names []string
for i := range l {
names = append(names, l[i].name)
}
return names
}
func (l labels) values(index int, msg *message.Batch) []string {
var values []string
for i := range l {
values = append(values, l[i].val(index, msg))
}
return values
}
func newMetricProcessor(conf processor.Config, mgr bundle.NewManagement, log log.Modular, stats metrics.Type) (processor.V1, error) {
value, err := mgr.BloblEnvironment().NewField(conf.Metric.Value)
if err != nil {
return nil, fmt.Errorf("failed to parse value expression: %v", err)
}
m := &metricProcessor{
conf: conf,
log: log,
stats: stats,
value: value,
}
name := conf.Metric.Name
if name == "" {
return nil, errors.New("metric name must not be empty")
}
labelNames := make([]string, 0, len(conf.Metric.Labels))
for n := range conf.Metric.Labels {
labelNames = append(labelNames, n)
}
sort.Strings(labelNames)
for _, n := range labelNames {
v, err := mgr.BloblEnvironment().NewField(conf.Metric.Labels[n])
if err != nil {
return nil, fmt.Errorf("failed to parse label '%v' expression: %v", n, err)
}
m.labels = append(m.labels, label{
name: n,
value: v,
})
}
switch strings.ToLower(conf.Metric.Type) {
case "counter":
if len(m.labels) > 0 {
m.mCounterVec = stats.GetCounterVec(name, m.labels.names()...)
} else {
m.mCounter = stats.GetCounter(name)
}
m.handler = m.handleCounter
case "counter_by":
if len(m.labels) > 0 {
m.mCounterVec = stats.GetCounterVec(name, m.labels.names()...)
} else {
m.mCounter = stats.GetCounter(name)
}
m.handler = m.handleCounterBy
case "gauge":
if len(m.labels) > 0 {
m.mGaugeVec = stats.GetGaugeVec(name, m.labels.names()...)
} else {
m.mGauge = stats.GetGauge(name)
}
m.handler = m.handleGauge
case "timing":
if len(m.labels) > 0 {
m.mTimerVec = stats.GetTimerVec(name, m.labels.names()...)
} else {
m.mTimer = stats.GetTimer(name)
}
m.handler = m.handleTimer
default:
return nil, fmt.Errorf("metric type unrecognised: %v", conf.Metric.Type)
}
return m, nil
}
func (m *metricProcessor) handleCounter(val string, index int, msg *message.Batch) error {
if len(m.labels) > 0 {
m.mCounterVec.With(m.labels.values(index, msg)...).Incr(1)
} else {
m.mCounter.Incr(1)
}
return nil
}
func (m *metricProcessor) handleCounterBy(val string, index int, msg *message.Batch) error {
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
if i < 0 {
return errors.New("value is negative")
}
if len(m.labels) > 0 {
m.mCounterVec.With(m.labels.values(index, msg)...).Incr(i)
} else {
m.mCounter.Incr(i)
}
return nil
}
func (m *metricProcessor) handleGauge(val string, index int, msg *message.Batch) error {
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
if i < 0 {
return errors.New("value is negative")
}
if len(m.labels) > 0 {
m.mGaugeVec.With(m.labels.values(index, msg)...).Set(i)
} else {
m.mGauge.Set(i)
}
return nil
}
func (m *metricProcessor) handleTimer(val string, index int, msg *message.Batch) error {
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
if i < 0 {
return errors.New("value is negative")
}
if len(m.labels) > 0 {
m.mTimerVec.With(m.labels.values(index, msg)...).Timing(i)
} else {
m.mTimer.Timing(i)
}
return nil
}
func (m *metricProcessor) ProcessMessage(msg *message.Batch) ([]*message.Batch, error) {
_ = msg.Iter(func(i int, p *message.Part) error {
value := m.value.String(i, msg)
if err := m.handler(value, i, msg); err != nil {
m.log.Errorf("Handler error: %v\n", err)
}
return nil
})
return []*message.Batch{msg}, nil
}
func (m *metricProcessor) CloseAsync() {
}
func (m *metricProcessor) WaitForClose(timeout time.Duration) error {
return nil
}