forked from ncabatoff/dbms_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbms_exporter.go
523 lines (464 loc) · 14.7 KB
/
dbms_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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package main
import (
"flag"
"fmt"
"math"
"net/http"
"os"
"strings"
"time"
_ "net/http/pprof"
"github.com/ncabatoff/dbms_exporter/common"
"github.com/ncabatoff/dbms_exporter/config"
"github.com/ncabatoff/dbms_exporter/db"
"github.com/ncabatoff/dbms_exporter/recipes"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
// Version is set at build time use ldflags.
var Version string
var (
version = flag.Bool("version", false, "print version and exit")
listenAddress = flag.String(
"web.listen-address", ":9113",
"Address to listen on for web interface and telemetry.",
)
metricPath = flag.String(
"web.telemetry-path", "/metrics",
"Path under which to expose metrics.",
)
queriesPath = flag.String(
"queryfile", "",
"File with queries to run.",
)
onlyDumpMaps = flag.Bool(
"dumpmaps", false,
"Do not run, simply dump the maps.",
)
driver = flag.String(
"driver", "postgres",
"DB driver to user, one of ("+strings.Join(db.Drivers(), ",")+
"); sybase is the same as freetds except for the prefix of generated metrics)",
)
persistentConnection = flag.Bool(
"persistent.connection", false,
"keep a DB connection open rather than opening a new one for each scrape",
)
queryFatalTimeout = flag.Duration(
"scrape.fatal-timeout", 0,
"exit if a scrape takes this long to execute",
)
)
// Metric name parts.
const (
// Subsystems.
exporter = "exporter"
)
// landingPageFmt contains the HTML served at '/'.
// TODO: Make this nicer and more informative.
var landingPageFmt = `<html>
<head><title>%s exporter</title></head>
<body>
<h1>%s exporter</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`
// Stores the prometheus metric description which a given column will be mapped
// to by the collector
type MetricMap struct {
discard bool // Should metric be discarded during mapping?
vtype prometheus.ValueType // Prometheus valuetype
desc *prometheus.Desc // Prometheus descriptor
conversion func(interface{}) (float64, bool) // Conversion function to turn DB result into float64
fixedval string
}
// Groups metric maps under a shared set of labels
type MetricMapNamespace struct {
labels []string // Label names for this namespace
columnMappings map[string]MetricMap // Column mappings in this namespace
}
func makeDescMap(metricName string, resultMap recipes.ResultMap, recipe recipes.MetricQueryRecipe) MetricMapNamespace {
thisMap := make(map[string]MetricMap)
// Get the constant labels
var variableLabels []string
var constLabels = make(prometheus.Labels)
for columnName, columnMapping := range resultMap {
if columnMapping.Usage == common.LABEL {
variableLabels = append(variableLabels, columnName)
} else if columnMapping.Usage == common.FIXED {
constLabels[columnName] = columnMapping.Fixedval
}
}
newDesc := func(colName, desc string) *prometheus.Desc {
return prometheus.NewDesc(fmt.Sprintf("%s_%s", metricName, colName), desc, variableLabels, constLabels)
}
for columnName, columnMapping := range resultMap {
switch columnMapping.Usage {
case common.DISCARD, common.LABEL:
thisMap[columnName] = MetricMap{
discard: true,
}
case common.COUNTER:
regexp := columnMapping.Regexp
thisMap[columnName] = MetricMap{
vtype: prometheus.CounterValue,
desc: newDesc(columnName, columnMapping.Description),
conversion: func(in interface{}) (float64, bool) {
return db.ToUnsignedFloat64(in, regexp)
},
}
case common.GAUGE:
regexp := columnMapping.Regexp
thisMap[columnName] = MetricMap{
vtype: prometheus.GaugeValue,
desc: newDesc(columnName, columnMapping.Description),
conversion: func(in interface{}) (float64, bool) {
return db.ToFloat64(in, regexp)
},
}
case common.MAPPEDMETRIC:
thisMap[columnName] = MetricMap{
vtype: prometheus.GaugeValue,
desc: newDesc(columnName, columnMapping.Description),
conversion: func(in interface{}) (float64, bool) {
text, ok := in.(string)
if !ok {
return math.NaN(), false
}
val, ok := columnMapping.Mapping[text]
if !ok {
return math.NaN(), false
}
return val, true
},
}
case common.DURATION:
fullName := fmt.Sprintf("%s_milliseconds", columnName)
thisMap[columnName] = MetricMap{
vtype: prometheus.GaugeValue,
desc: newDesc(fullName, columnMapping.Description),
conversion: convertDuration,
}
}
}
return MetricMapNamespace{variableLabels, thisMap}
}
func convertDuration(in interface{}) (float64, bool) {
var durationString string
switch t := in.(type) {
case []byte:
durationString = string(t)
case string:
durationString = t
default:
log.Errorln("DURATION conversion metric was not a string")
return math.NaN(), false
}
if durationString == "-1" {
return math.NaN(), false
}
d, err := time.ParseDuration(durationString)
if err != nil {
return math.NaN(), false
}
return float64(d / time.Millisecond), true
}
// Turn the MetricMap column mapping into a prometheus descriptor mapping.
func makeDescMaps(recipes []recipes.MetricQueryRecipe) map[string]MetricMapNamespace {
var metricMap = make(map[string]MetricMapNamespace)
for _, recipe := range recipes {
namespace := recipe.GetNamespace()
for _, rm := range recipe.GetResultMaps() {
if rm.Name == "discard" {
continue
}
metricName := namespace
if rm.Name != "metrics" {
metricName = metricName + "_" + rm.Name
}
metricMap[metricName] = makeDescMap(metricName, rm.ResultMap, recipe)
}
}
return metricMap
}
type scrapeRequest struct {
results chan<- prometheus.Metric
done chan struct{}
}
// Exporter collects DB metrics. It implements prometheus.Collector.
type Exporter struct {
dsn string
driver string
persistentConnection bool
conn db.Conn
scrapeChan chan scrapeRequest
duration prometheus.Gauge
totalScrapes prometheus.Counter
errors_total prometheus.Counter
open_seconds_total prometheus.Counter
query_seconds_total *prometheus.CounterVec
metricMap map[string]MetricMapNamespace
recipes []recipes.MetricQueryRecipe
scrapeTimeoutFatal time.Duration
}
// NewExporter returns a new exporter for the provided DSN.
func NewExporter(driver, dsn string, recipes []recipes.MetricQueryRecipe, persistentConn bool, fatalTimeout time.Duration) *Exporter {
return &Exporter{
driver: driver,
dsn: dsn,
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: driver,
Subsystem: exporter,
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape of metrics from DB",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: driver,
Subsystem: exporter,
Name: "scrapes_total",
Help: "Total number of times the DB was scraped for metrics.",
}),
errors_total: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: driver,
Subsystem: exporter,
Name: "scrape_errors_total",
Help: "How many scrapes failed due to an error",
}),
open_seconds_total: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: driver,
Subsystem: exporter,
Name: "open_seconds_total",
Help: "How much time was consumed opening DB connections",
}),
query_seconds_total: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: driver,
Subsystem: exporter,
Name: "query_seconds_total",
Help: "How much time was consumed opening DB connections",
}, []string{"namespace"}),
metricMap: makeDescMaps(recipes),
recipes: recipes,
persistentConnection: persistentConn,
scrapeChan: make(chan scrapeRequest),
scrapeTimeoutFatal: fatalTimeout,
}
}
// Describe implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// We cannot know in advance what metrics the exporter will generate
// from the DB. So we use the poor man's describe method: Run a collect
// and send the descriptors of all the collected metrics. The problem
// here is that we need to connect to the DB. If it is currently
// unavailable, the descriptors will be incomplete. Since this is a
// stand-alone exporter and not used as a library within other code
// implementing additional metrics, the worst that can happen is that we
// don't detect inconsistent metrics created by this exporter
// itself. Also, a change in the monitored DB instance may change the
// exported metrics during the runtime of the exporter.
metricCh := make(chan prometheus.Metric)
doneCh := make(chan struct{})
go func() {
for m := range metricCh {
ch <- m.Desc()
}
close(doneCh)
}()
e.Collect(metricCh)
close(metricCh)
<-doneCh
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
req := scrapeRequest{results: ch, done: make(chan struct{})}
e.scrapeChan <- req
var timeoutChan <-chan time.Time
if e.scrapeTimeoutFatal > 0 {
t := time.NewTimer(e.scrapeTimeoutFatal)
timeoutChan = t.C
}
select {
case <-req.done:
return
case <-timeoutChan:
log.Fatalf("timed out waiting %s for scrape to complete, exiting", e.scrapeTimeoutFatal)
}
}
func (e *Exporter) Start() {
go func() {
for req := range e.scrapeChan {
ch := req.results
e.scrape(ch)
ch <- e.duration
ch <- e.totalScrapes
ch <- e.errors_total
ch <- e.open_seconds_total
e.query_seconds_total.Collect(ch)
req.done <- struct{}{}
}
}()
}
func (e *Exporter) scrapeRecipe(ch chan<- prometheus.Metric, conn db.Conn, recipe recipes.MetricQueryRecipe) error {
namespace := recipe.GetNamespace()
log.Debugln("Querying namespace: ", namespace)
qstart := time.Now()
srss, err := recipe.Run(conn)
e.query_seconds_total.WithLabelValues(namespace).Add(time.Since(qstart).Seconds())
if err != nil {
return err
}
rms := recipe.GetResultMaps()
for i, srs := range srss {
log.Debugf("handling resultset %d with %d rows", i, len(srs.Rows))
rm := rms[i]
// handle the 'discard' scenario by skipping this resultset
if rm.ShouldSkip() {
continue
}
ns := namespace
if rm.Name != "metrics" {
ns = ns + "_" + rm.Name
}
e.scrapeResultSet(ch, ns, srs, rm.ResultMap)
}
return nil
}
func (e *Exporter) scrapeResultSet(ch chan<- prometheus.Metric, namespace string, srs db.ScannedResultSet, rm recipes.ResultMap) {
// Make a lookup map for the column indices
var columnIdx = make(map[string]int, len(srs.Colnames))
for i, n := range srs.Colnames {
columnIdx[n] = i
}
for _, row := range srs.Rows {
// Get the label values for this row
mapping := e.metricMap[namespace]
var labels = make([]string, len(mapping.labels))
for idx, columnName := range mapping.labels {
labels[idx], _ = db.ToString(row[columnIdx[columnName]])
}
// Loop over column names, and match to scan data. Unknown columns
// will be filled with an untyped metric number *if* they can be
// converted to float64s. NULLs are allowed and treated as NaN.
for idx, columnName := range srs.Colnames {
columnName = strings.Replace(columnName, " ", "_", -1)
if metricMapping, ok := mapping.columnMappings[columnName]; ok {
// Is this a metricy metric?
if metricMapping.discard {
continue
}
value, ok := metricMapping.conversion(row[idx])
if !ok {
e.errors_total.Inc()
log.Errorln("Unexpected error parsing column: ", namespace, columnName, row[idx])
continue
}
// Generate the metric
ch <- prometheus.MustNewConstMetric(metricMapping.desc, metricMapping.vtype, value, labels...)
} else {
log.Debugf("unknown metric %q in namespace %q, labels: %v", columnName, namespace, labels)
// Unknown metric. Report as untyped if scan to float64 works, else note an error too.
desc := prometheus.NewDesc(fmt.Sprintf("%s_%s_%s", e.driver, namespace, columnName), fmt.Sprintf("Unknown metric from %s", namespace), nil, nil)
// Its not an error to fail here, since the values are
// unexpected anyway.
value, ok := db.ToFloat64(row[idx], nil)
if !ok {
log.Warnln("Unparseable column type - discarding: ", namespace, columnName)
continue
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.UntypedValue, value)
}
}
}
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
defer func(begun time.Time) {
e.duration.Set(time.Since(begun).Seconds())
}(time.Now())
e.totalScrapes.Inc()
conn := e.conn
if conn == db.Conn(nil) {
start := time.Now()
var err error
conn, err = db.Open(e.driver, e.dsn)
if err != nil {
log.Infof("Error opening connection to %s database: %v", e.driver, err)
e.errors_total.Inc()
return
}
if e.persistentConnection {
e.conn = conn
} else {
defer conn.Close()
}
e.open_seconds_total.Add(time.Since(start).Seconds())
}
for _, recipe := range e.recipes {
err := e.scrapeRecipe(ch, conn, recipe)
if err != nil {
log.Errorf("Error running query for %q: %v", recipe.GetNamespace(), err)
e.errors_total.Inc()
if e.conn != nil {
e.conn.Close()
}
e.conn = nil
return
}
}
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, usage)
}
flag.Parse()
if *version {
fmt.Printf("dbms-exporter version %s\n", Version)
os.Exit(0)
}
if *queriesPath == "" {
log.Fatalf("-queryfile is a required argument")
}
rcps, err := config.ReadRecipesFile(*queriesPath, *driver)
if err != nil {
log.Fatalf("error parsing file %q: %v", *queriesPath, err)
}
if *driver == "sybase" {
*driver = "freetds"
}
found := false
for _, d := range db.Drivers() {
if d == *driver {
found = true
break
}
}
if !found {
log.Fatalf("driver %q not supported in this build", *driver)
}
if *onlyDumpMaps {
recipes.DumpMaps(rcps)
return
}
dsn := os.Getenv("DATA_SOURCE_NAME")
if len(dsn) == 0 {
log.Fatal("couldn't find environment variable DATA_SOURCE_NAME")
}
exporter := NewExporter(*driver, dsn, rcps, *persistentConnection, *queryFatalTimeout)
exporter.Start()
prometheus.MustRegister(exporter)
http.Handle(*metricPath, prometheus.Handler())
landingPage := []byte(fmt.Sprintf(landingPageFmt, *driver, *driver))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(landingPage)
})
log.Infof("Starting Server: %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
var usage = `
The DATA_SOURCE_NAME enviroment variable specifies connection details. Examples:
Sybase FreeTDS example (driver=freetds):
compatibility_mode=sybase;user=myuser;pwd=mypassword;server=myhostname
PostgreSQL example (driver=postgres):
postgres://myuser@myhost:5432/?sslmode=disable&dbname=postgres&client_encoding=UTF8
`