forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_prometheus.go
402 lines (335 loc) · 11.6 KB
/
metrics_prometheus.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package prometheus
import (
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus/push"
"github.com/prometheus/common/model"
"github.com/benthosdev/benthos/v4/internal/bundle"
"github.com/benthosdev/benthos/v4/internal/component/metrics"
"github.com/benthosdev/benthos/v4/internal/docs"
"github.com/benthosdev/benthos/v4/internal/log"
)
func init() {
_ = bundle.AllMetrics.Add(newPrometheus, docs.ComponentSpec{
Name: "prometheus",
Type: docs.TypeMetrics,
Status: docs.StatusStable,
Summary: `
Host endpoints (` + "`/metrics` and `/stats`" + `) for Prometheus scraping.`,
Footnotes: `
## Push Gateway
The field ` + "`push_url`" + ` is optional and when set will trigger a push of
metrics to a [Prometheus Push Gateway](https://prometheus.io/docs/instrumenting/pushing/)
once Benthos shuts down. It is also possible to specify a
` + "`push_interval`" + ` which results in periodic pushes.
The Push Gateway is useful for when Benthos instances are short lived. Do not
include the "/metrics/jobs/..." path in the push URL.
If the Push Gateway requires HTTP Basic Authentication it can be configured with
` + "`push_basic_auth`.",
Config: docs.FieldComponent().WithChildren(
docs.FieldBool("use_histogram_timing", "Whether to export timing metrics as a histogram, if `false` a summary is used instead. When exporting histogram timings the delta values are converted from nanoseconds into seconds in order to better fit within bucket definitions. For more information on histograms and summaries refer to: https://prometheus.io/docs/practices/histograms/.").HasDefault(false).Advanced().AtVersion("3.63.0"),
docs.FieldFloat("histogram_buckets", "Timing metrics histogram buckets (in seconds). If left empty defaults to DefBuckets (https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#pkg-variables)").Array().HasDefault([]any{}).Advanced().AtVersion("3.63.0"),
docs.FieldBool("add_process_metrics", "Whether to export process metrics such as CPU and memory usage in addition to Benthos metrics.").Advanced().HasDefault(false),
docs.FieldBool("add_go_metrics", "Whether to export Go runtime metrics such as GC pauses in addition to Benthos metrics.").Advanced().HasDefault(false),
docs.FieldURL("push_url", "An optional [Push Gateway URL](#push-gateway) to push metrics to.").Advanced().HasDefault(""),
docs.FieldString("push_interval", "The period of time between each push when sending metrics to a Push Gateway.").Advanced().HasDefault(""),
docs.FieldString("push_job_name", "An identifier for push jobs.").Advanced().HasDefault("benthos_push"),
docs.FieldObject("push_basic_auth", "The Basic Authentication credentials.").WithChildren(
docs.FieldString("username", "The Basic Authentication username.").HasDefault(""),
docs.FieldString("password", "The Basic Authentication password.").HasDefault("").Secret(),
).Advanced(),
docs.FieldString("file_output_path", "An optional file path to write all prometheus metrics on service shutdown.").Advanced().HasDefault(""),
),
})
}
//------------------------------------------------------------------------------
type promGauge struct {
ctr prometheus.Gauge
}
func (p *promGauge) Incr(count int64) {
p.ctr.Add(float64(count))
}
func (p *promGauge) Decr(count int64) {
p.ctr.Add(float64(-count))
}
func (p *promGauge) Set(value int64) {
p.ctr.Set(float64(value))
}
type promCounter struct {
ctr prometheus.Counter
}
func (p *promCounter) Incr(count int64) {
p.ctr.Add(float64(count))
}
type promTiming struct {
sum prometheus.Observer
asSeconds bool
}
func (p *promTiming) Timing(val int64) {
vFloat := float64(val)
if p.asSeconds {
vFloat /= 1_000_000_000
}
p.sum.Observe(vFloat)
}
//------------------------------------------------------------------------------
type promCounterVec struct {
ctr *prometheus.CounterVec
count int
}
func (p *promCounterVec) With(labelValues ...string) metrics.StatCounter {
return &promCounter{
ctr: p.ctr.WithLabelValues(labelValues...),
}
}
type promTimingVec struct {
sum *prometheus.SummaryVec
count int
}
func (p *promTimingVec) With(labelValues ...string) metrics.StatTimer {
return &promTiming{
sum: p.sum.WithLabelValues(labelValues...),
}
}
type promTimingHistVec struct {
sum *prometheus.HistogramVec
count int
}
func (p *promTimingHistVec) With(labelValues ...string) metrics.StatTimer {
return &promTiming{
asSeconds: true,
sum: p.sum.WithLabelValues(labelValues...),
}
}
type promGaugeVec struct {
ctr *prometheus.GaugeVec
count int
}
func (p *promGaugeVec) With(labelValues ...string) metrics.StatGauge {
return &promGauge{
ctr: p.ctr.WithLabelValues(labelValues...),
}
}
//------------------------------------------------------------------------------
type prometheusMetrics struct {
log log.Modular
closedChan chan struct{}
running int32
fileOutputPath string
useHistogramTiming bool
histogramBuckets []float64
pusher *push.Pusher
reg *prometheus.Registry
counters map[string]*promCounterVec
gauges map[string]*promGaugeVec
timers map[string]*promTimingVec
timersHist map[string]*promTimingHistVec
mut sync.Mutex
}
func newPrometheus(config metrics.Config, nm bundle.NewManagement) (metrics.Type, error) {
promConf := config.Prometheus
p := &prometheusMetrics{
log: nm.Logger(),
running: 1,
closedChan: make(chan struct{}),
useHistogramTiming: promConf.UseHistogramTiming,
histogramBuckets: promConf.HistogramBuckets,
reg: prometheus.NewRegistry(),
counters: map[string]*promCounterVec{},
gauges: map[string]*promGaugeVec{},
timers: map[string]*promTimingVec{},
timersHist: map[string]*promTimingHistVec{},
}
if len(p.histogramBuckets) == 0 {
p.histogramBuckets = prometheus.DefBuckets
}
if promConf.AddProcessMetrics {
if err := p.reg.Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})); err != nil {
return nil, err
}
}
if promConf.AddGoMetrics {
if err := p.reg.Register(collectors.NewGoCollector()); err != nil {
return nil, err
}
}
if len(promConf.PushURL) > 0 {
p.pusher = push.New(promConf.PushURL, promConf.PushJobName).Gatherer(p.reg)
if len(promConf.PushBasicAuth.Username) > 0 && len(promConf.PushBasicAuth.Password) > 0 {
p.pusher = p.pusher.BasicAuth(promConf.PushBasicAuth.Username, promConf.PushBasicAuth.Password)
}
if len(promConf.PushInterval) > 0 {
interval, err := time.ParseDuration(promConf.PushInterval)
if err != nil {
return nil, fmt.Errorf("failed to parse push interval: %v", err)
}
go func() {
for {
select {
case <-p.closedChan:
return
case <-time.After(interval):
if err = p.pusher.Push(); err != nil {
p.log.Errorf("Failed to push metrics: %v\n", err)
}
}
}
}()
}
}
if len(promConf.FileOutputPath) > 0 {
p.fileOutputPath = promConf.FileOutputPath
}
return p, nil
}
//------------------------------------------------------------------------------
func (p *prometheusMetrics) HandlerFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
promhttp.HandlerFor(p.reg, promhttp.HandlerOpts{}).ServeHTTP(w, r)
}
}
func (p *prometheusMetrics) GetCounter(path string) metrics.StatCounter {
return p.GetCounterVec(path).With()
}
func (p *prometheusMetrics) GetCounterVec(path string, labelNames ...string) metrics.StatCounterVec {
if !model.IsValidMetricName(model.LabelValue(path)) {
p.log.Errorf("Ignoring metric '%v' due to invalid name", path)
return metrics.FakeCounterVec(func(l ...string) metrics.StatCounter {
return metrics.DudStat{}
})
}
var pv *promCounterVec
p.mut.Lock()
var exists bool
if pv, exists = p.counters[path]; !exists {
ctr := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: path,
Help: "Benthos Counter metric",
}, labelNames)
p.reg.MustRegister(ctr)
pv = &promCounterVec{
ctr: ctr,
count: len(labelNames),
}
p.counters[path] = pv
}
p.mut.Unlock()
if pv.count != len(labelNames) {
p.log.Errorf("Metrics label mismatch %v versus %v %v for name '%v', skipping metric", pv.count, len(labelNames), labelNames, path)
return metrics.Noop().GetCounterVec(path, labelNames...)
}
return pv
}
func (p *prometheusMetrics) GetTimer(path string) metrics.StatTimer {
return p.GetTimerVec(path).With()
}
func (p *prometheusMetrics) GetTimerVec(path string, labelNames ...string) metrics.StatTimerVec {
if !model.IsValidMetricName(model.LabelValue(path)) {
p.log.Errorf("Ignoring metric '%v' due to invalid name", path)
return metrics.FakeTimerVec(func(l ...string) metrics.StatTimer {
return &metrics.DudStat{}
})
}
if p.useHistogramTiming {
return p.getTimerHistVec(path, labelNames...)
}
var pv *promTimingVec
p.mut.Lock()
var exists bool
if pv, exists = p.timers[path]; !exists {
tmr := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: path,
Help: "Benthos Timing metric",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, labelNames)
p.reg.MustRegister(tmr)
pv = &promTimingVec{
sum: tmr,
count: len(labelNames),
}
p.timers[path] = pv
}
p.mut.Unlock()
if pv.count != len(labelNames) {
p.log.Errorf("Metrics label mismatch %v versus %v %v for name '%v', skipping metric", pv.count, len(labelNames), labelNames, path)
return metrics.Noop().GetTimerVec(path, labelNames...)
}
return pv
}
func (p *prometheusMetrics) getTimerHistVec(path string, labelNames ...string) metrics.StatTimerVec {
var pv *promTimingHistVec
p.mut.Lock()
var exists bool
if pv, exists = p.timersHist[path]; !exists {
tmr := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: path,
Help: "Benthos Timing metric",
Buckets: p.histogramBuckets,
}, labelNames)
p.reg.MustRegister(tmr)
pv = &promTimingHistVec{
sum: tmr,
count: len(labelNames),
}
p.timersHist[path] = pv
}
p.mut.Unlock()
if pv.count != len(labelNames) {
p.log.Errorf("Metrics label mismatch %v versus %v %v for name '%v', skipping metric", pv.count, len(labelNames), labelNames, path)
return metrics.Noop().GetTimerVec(path, labelNames...)
}
return pv
}
func (p *prometheusMetrics) GetGauge(path string) metrics.StatGauge {
return p.GetGaugeVec(path).With()
}
func (p *prometheusMetrics) GetGaugeVec(path string, labelNames ...string) metrics.StatGaugeVec {
if !model.IsValidMetricName(model.LabelValue(path)) {
p.log.Errorf("Ignoring metric '%v' due to invalid name", path)
return metrics.FakeGaugeVec(func(l ...string) metrics.StatGauge {
return &metrics.DudStat{}
})
}
var pv *promGaugeVec
p.mut.Lock()
var exists bool
if pv, exists = p.gauges[path]; !exists {
ctr := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: path,
Help: "Benthos Gauge metric",
}, labelNames)
p.reg.MustRegister(ctr)
pv = &promGaugeVec{
ctr: ctr,
count: len(labelNames),
}
p.gauges[path] = pv
}
p.mut.Unlock()
if pv.count != len(labelNames) {
p.log.Errorf("Metrics label mismatch %v versus %v %v for name '%v', skipping metric", pv.count, len(labelNames), labelNames, path)
return metrics.Noop().GetGaugeVec(path, labelNames...)
}
return pv
}
func (p *prometheusMetrics) Close() error {
if atomic.CompareAndSwapInt32(&p.running, 1, 0) {
close(p.closedChan)
}
if p.pusher != nil {
err := p.pusher.Push()
if err != nil {
return err
}
}
if p.fileOutputPath != "" {
return prometheus.WriteToTextfile(p.fileOutputPath, p.reg)
}
return nil
}