-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathresque_exporter.go
308 lines (269 loc) · 7.37 KB
/
resque_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
303
304
305
306
307
308
package main
import (
"flag"
"fmt"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/go-redis/redis"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
const (
namespace = "resque"
)
var (
redisNamespace = flag.String(
"redis.namespace",
"resque",
"Namespace used by Resque to prefix all its Redis keys.",
)
redisURL = flag.String(
"redis.url",
"redis://localhost:6379",
"URL to the Redis backing the Resque.",
)
printVersion = flag.Bool(
"version",
false,
"Print version information.",
)
listenAddress = flag.String(
"web.listen-address",
":9447",
"Address to listen on for web interface and telemetry.",
)
metricPath = flag.String(
"web.telemetry-path",
"/metrics",
"Path under which to expose metrics.",
)
)
var (
failedJobExecutionsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "failed_job_executions_total"),
"Total number of failed job executions.",
nil, nil,
)
jobExecutionsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "job_executions_total"),
"Total number of job executions.",
nil, nil,
)
jobsInFailedQueueDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "jobs_in_failed_queue"),
"Number of jobs in a failed queue.",
[]string{"queue"}, nil,
)
jobsInQueueDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "jobs_in_queue"),
"Number of jobs in a queue.",
[]string{"queue"}, nil,
)
scrapeDurationDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "scrape_duration_seconds"),
"Time this scrape of resque metrics took.",
nil, nil,
)
upDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Whether this scrape of resque metrics was successful.",
nil, nil,
)
workersDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "workers"),
"Number of workers.",
nil, nil,
)
workingWorkersDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "working_workers"),
"Number of working workers.",
nil, nil,
)
)
// Exporter collects Resque metrics. It implements prometheus.Collector.
type Exporter struct {
redisClient *redis.Client
redisNamespace string
failedScrapes prometheus.Counter
scrapes prometheus.Counter
}
// NewExporter returns a new Resque exporter.
func NewExporter(redisURL, redisNamespace string) (*Exporter, error) {
redisClient, err := newRedisClient(redisURL)
if err != nil {
return nil, err
}
return &Exporter{
redisClient: redisClient,
redisNamespace: redisNamespace,
failedScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "failed_scrapes_total",
Help: "Total number of failed scrapes.",
}),
scrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "scrapes_total",
Help: "Total number of scrapes.",
}),
}, nil
}
func newRedisClient(redisURL string) (*redis.Client, error) {
var options redis.Options
u, err := url.Parse(redisURL)
if err != nil {
return nil, err
}
if u.Scheme == "redis" || u.Scheme == "tcp" {
options.Network = "tcp"
options.Addr = net.JoinHostPort(u.Hostname(), u.Port())
if len(u.Path) > 1 {
if db, err := strconv.Atoi(u.Path[1:]); err == nil {
options.DB = db
}
}
} else if u.Scheme == "unix" {
options.Network = "unix"
options.Addr = u.Path
} else {
return nil, fmt.Errorf("unknown URL scheme: %s", u.Scheme)
}
if password, ok := u.User.Password(); ok {
options.Password = password
}
return redis.NewClient(&options), nil
}
// Describe implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- failedJobExecutionsDesc
ch <- jobExecutionsDesc
ch <- jobsInFailedQueueDesc
ch <- jobsInQueueDesc
ch <- scrapeDurationDesc
ch <- upDesc
ch <- workersDesc
ch <- workingWorkersDesc
ch <- e.failedScrapes.Desc()
ch <- e.scrapes.Desc()
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
if err := e.scrape(ch); err != nil {
e.failedScrapes.Inc()
log.Error(err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0)
} else {
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1)
}
ch <- e.failedScrapes
ch <- e.scrapes
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) error {
e.scrapes.Inc()
defer func(start time.Time) {
ch <- prometheus.MustNewConstMetric(
scrapeDurationDesc,
prometheus.GaugeValue,
float64(time.Since(start).Seconds()))
}(time.Now())
executions, err := e.redisClient.Get(e.redisKey("stat:processed")).Float64()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(jobExecutionsDesc, prometheus.CounterValue, executions)
failedExecutions, err := e.redisClient.Get(e.redisKey("stat:failed")).Float64()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(failedJobExecutionsDesc, prometheus.CounterValue, failedExecutions)
queues, err := e.redisClient.SMembers(e.redisKey("queues")).Result()
if err != nil {
return err
}
for _, queue := range queues {
jobs, err := e.redisClient.LLen(e.redisKey("queue", queue)).Result()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(jobsInQueueDesc, prometheus.GaugeValue, float64(jobs), queue)
}
failedQueues, err := e.redisClient.SMembers(e.redisKey("failed_queues")).Result()
if err != nil {
return err
}
if len(failedQueues) == 0 {
exists, err := e.redisClient.Exists(e.redisKey("failed")).Result()
if err != nil {
return err
}
if exists == 1 {
failedQueues = []string{"failed"}
}
}
for _, queue := range failedQueues {
jobs, err := e.redisClient.LLen(e.redisKey(queue)).Result()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(jobsInFailedQueueDesc, prometheus.GaugeValue, float64(jobs), queue)
}
workers, err := e.redisClient.SMembers(e.redisKey("workers")).Result()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(workersDesc, prometheus.GaugeValue, float64(len(workers)))
var workingWorkers int
for _, worker := range workers {
exists, err := e.redisClient.Exists(e.redisKey("worker", worker)).Result()
if err != nil {
return err
}
if exists == 1 {
workingWorkers++
}
}
ch <- prometheus.MustNewConstMetric(workingWorkersDesc, prometheus.GaugeValue, float64(workingWorkers))
return nil
}
func (e *Exporter) redisKey(a ...string) string {
return e.redisNamespace + ":" + strings.Join(a, ":")
}
func init() {
prometheus.MustRegister(version.NewCollector("resque_exporter"))
}
func main() {
flag.Parse()
if *printVersion {
fmt.Println(version.Print("resque_exporter"))
return
}
log.Infoln("Starting resque_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
if u := os.Getenv("REDIS_URL"); len(u) > 0 {
*redisURL = u
}
exporter, err := NewExporter(*redisURL, *redisNamespace)
if err != nil {
log.Fatal(err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Resque Exporter</title></head>
<body>
<h1>Resque Exporter</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}