forked from iimos/saramaprom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsaramaprom.go
76 lines (63 loc) · 2.06 KB
/
saramaprom.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
package saramaprom
import (
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// StopFunc represents function for stopping scheduled task.
type StopFunc func()
// Options holds optional params for ExportMetrics.
type Options struct {
// PrometheusRegistry is prometheus registry. Default prometheus.DefaultRegisterer.
PrometheusRegistry prometheus.Registerer
// Namespace and Subsystem form the metric name prefix.
// Default Subsystem is "sarama".
Namespace string
Subsystem string
// Extra labels to be added to all metrics. Beware, this will override the default label value if configured with same name.
ExtraLabels prometheus.Labels
// RefreshInterval specifies interval between updating metrics. Default 1s.
RefreshInterval time.Duration
}
// ExportMetrics exports metrics from go-metrics to prometheus by starting background task,
// which periodically sync sarama metrics to prometheus registry.
func ExportMetrics(metricsRegistry MetricsRegistry, opt Options) StopFunc {
if opt.PrometheusRegistry == nil {
opt.PrometheusRegistry = prometheus.DefaultRegisterer
}
if opt.Subsystem == "" {
opt.Subsystem = "sarama"
}
if opt.RefreshInterval == 0 {
opt.RefreshInterval = time.Second
}
exp := &exporter{
opt: opt,
registry: metricsRegistry,
promRegistry: opt.PrometheusRegistry,
gauges: make(map[string]prometheus.Gauge),
customMetrics: make(map[string]*customCollector),
histogramQuantiles: []float64{0.05, 0.1, 0.25, 0.50, 0.75, 0.9, 0.95, 0.99},
timerQuantiles: []float64{0.50, 0.95, 0.99, 0.999},
mutex: new(sync.Mutex),
}
err := exp.update()
if err != nil {
panic(err)
}
scheduler := StartScheduler(opt.RefreshInterval, func() {
err := exp.update()
if err != nil {
panic(err)
}
})
return func() {
_ = exp.unregisterGauges()
scheduler.Stop()
}
}
// MetricsRegistry is an interface for 'github.com/rcrowley/go-metrics'.Registry
// which is used for metrics in sarama.
type MetricsRegistry interface {
Each(func(name string, i interface{}))
}