forked from messagebird/beanstalkd_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
302 lines (256 loc) · 7.35 KB
/
exporter.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
package main
import (
"io"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/kr/beanstalk"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
const (
dialTimeout = 30 * time.Second
)
type Exporter struct {
// use to protect against concurrent collection
mutex sync.RWMutex
conn io.ReadWriteCloser
address string
connectionTimeout time.Duration
nameReplacer *regexp.Regexp
labelReplacer *regexp.Regexp
graceDuration time.Duration
// scrape metrics
scrapeCountMetric *prometheus.CounterVec
scrapeConnectionErrorMetric prometheus.Counter
scrapeHistogramMetric prometheus.Histogram
// use to collects all the errors asynchronously
cherrs chan error
}
func NewExporter(address string) *Exporter {
cherrs := make(chan error)
exporter := &Exporter{
address: address,
scrapeCountMetric: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "beanstalkd",
Subsystem: "exporter",
Name: "requests_total",
Help: "The number of request to beanstalkd.",
},
[]string{"outcome"},
),
scrapeConnectionErrorMetric: prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "beanstalkd",
Subsystem: "exporter",
Name: "scrape_connection_errors_total",
Help: "Total number of connection errors to beanstalkd.",
},
),
scrapeHistogramMetric: prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: "beanstalkd",
Subsystem: "exporter",
Name: "scrape_seconds",
Help: "Scrape time buckets.",
},
),
cherrs: cherrs,
}
go func(e *Exporter) {
for {
log.Errorln(<-cherrs)
e.scrapeCountMetric.WithLabelValues("failure").Inc()
}
}(exporter)
return exporter
}
// SetConnectionTimeout sets the connection timeout value
func (e *Exporter) SetConnectionTimeout(timeout time.Duration) {
e.connectionTimeout = timeout
}
// Describe implements the prometheus.Collector interface, emits on the chan
// the descriptors of all the possible metrics.
// Since it's impossible to know in advance the metrics that going to be
// collected Describe is equivalent of a Collect call.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.scrapeCountMetric.Describe(ch)
e.scrapeConnectionErrorMetric.Describe(ch)
e.scrapeHistogramMetric.Describe(ch)
mapper.configLoadsMetric.Describe(ch)
mapper.mappingsCountMetric.Describe(ch)
// TODO: move this init to the NewExporter
// if we release a new major version.
if e.conn == nil {
conn, err := newLazyConn(e.address, dialTimeout, e.connectionTimeout)
if err != nil {
e.scrapeConnectionErrorMetric.Inc()
log.Warnf("unable to connect to beanstalkd: %s", err)
return
}
e.conn = conn
}
client := beanstalk.NewConn(e.conn)
collectors := e.scrape(client)
for _, collector := range collectors {
collector.Describe(ch)
}
}
// Collect implements the prometheus.Collector interface, emits on the chan all
// the metrics.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.scrapeCountMetric.Collect(ch)
e.scrapeConnectionErrorMetric.Collect(ch)
e.scrapeHistogramMetric.Collect(ch)
mapper.configLoadsMetric.Collect(ch)
mapper.mappingsCountMetric.Collect(ch)
// TODO: move this init to the NewExporter
// if we release a new major version.
if e.conn == nil {
conn, err := newLazyConn(e.address, dialTimeout, e.connectionTimeout)
if err != nil {
e.scrapeConnectionErrorMetric.Inc()
log.Warnf("unable to connect to beanstalkd: %s", err)
return
}
e.conn = conn
}
client := beanstalk.NewConn(e.conn)
collectors := e.scrape(client)
for _, collector := range collectors {
collector.Collect(ch)
}
}
// scrape retrieves all the available metrics and invoke the given callback on each of them.
func (e *Exporter) scrape(conn *beanstalk.Conn) []prometheus.Collector {
var collectors []prometheus.Collector
start := time.Now()
defer func() {
e.scrapeHistogramMetric.Observe(time.Since(start).Seconds())
}()
if *logLevel == "debug" {
log.Debugf("Debug: Calling %s stats()", e.address)
}
stats, err := conn.Stats()
if err != nil {
log.Errorf("Error requesting Stats(): %v", err)
e.scrapeCountMetric.WithLabelValues("failure").Inc()
return collectors
}
e.scrapeCountMetric.WithLabelValues("success").Inc()
for key, value := range stats {
// ignore these stats
if key == "hostname" || key == "id" || key == "pid" {
continue
}
name := strings.Replace(key, "-", "_", -1)
help := systemStatsHelp[key]
if help == "" {
help = key
}
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: name,
Help: help,
ConstLabels: prometheus.Labels{"instance": e.address},
})
iValue, _ := strconv.ParseFloat(value, 64)
gauge.Set(iValue)
collectors = append(collectors, gauge)
}
if *logLevel == "debug" {
log.Debugf("Debug: Calling %s ListTubes()", e.address)
}
// stat every tube
tubes, err := conn.ListTubes()
if err != nil {
log.Errorf("Error requesting ListTubes(): %v", err)
e.scrapeCountMetric.WithLabelValues("failure").Inc()
return collectors
}
e.scrapeCountMetric.WithLabelValues("success").Inc()
var outs []<-chan []prometheus.Collector
for i, tube := range tubes {
out := e.scrapeWorker(i, conn, tube)
outs = append(outs, out)
}
for _, out := range outs {
tubeCollectors := <-out
collectors = append(collectors, tubeCollectors...)
}
return collectors
}
func (e *Exporter) scrapeWorker(i int, c *beanstalk.Conn, name string) <-chan []prometheus.Collector {
out := make(chan []prometheus.Collector)
go func() {
defer close(out)
if *logLevel == "debug" {
log.Debugf("Debug: scrape worker %d started", i)
}
if *logLevel == "debug" {
log.Debugf("Debug: scrape worker %d fetching tube %s", i, name)
}
out <- e.statTube(c, name)
if *logLevel == "debug" {
log.Debugf("Debug: scrape worker %d finished", i)
}
}()
return out
}
func (e *Exporter) statTube(c *beanstalk.Conn, tubeName string) []prometheus.Collector {
var collectors []prometheus.Collector
if *logLevel == "debug" {
log.Debugf("Debug: Calling %s Tube{name: %s}.Stats()", e.address, tubeName)
}
var labels prometheus.Labels
mappedLabels, mappingPresent := mapper.getMapping(tubeName)
if mappingPresent {
labels = mappedLabels
labels["tube"] = labels["name"]
delete(labels, "name")
} else {
labels = prometheus.Labels{"tube": tubeName}
}
labels["instance"] = e.address
// be sure all labels are set
allLabelNames := append(mapper.getAllLabels(), "instance", "tube")
for _, l := range allLabelNames {
if labels[l] == "" {
labels[l] = ""
}
}
tube := beanstalk.Tube{Conn: c, Name: tubeName}
stats, err := tube.Stats()
if err != nil {
log.Errorf("Error tubes stats: %v", err)
e.scrapeCountMetric.WithLabelValues("failure").Inc()
return collectors
}
e.scrapeCountMetric.WithLabelValues("success").Inc()
for key, value := range stats {
// ignore these stats
if key == "tube-name" || key == "name" {
continue
}
name := "tube_" + strings.Replace(key, "-", "_", -1)
help := tubeStatsHelp[key]
if help == "" {
help = key
}
gaugeVec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: name,
Help: help,
}, allLabelNames)
gauge := gaugeVec.With(labels)
iValue, _ := strconv.ParseFloat(value, 64)
gauge.Set(iValue)
collectors = append(collectors, gauge)
}
return collectors
}