forked from percona/mysqld_exporter
/
dba_open_tables.go
70 lines (58 loc) · 1.59 KB
/
dba_open_tables.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
package dbacollector
import (
"context"
"database/sql"
cl "github.com/a-korotich/mysqld_exporter/collector"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
const (
OpenTablesQuery = `
SHOW OPEN TABLES WHERE In_use > 0
`
)
var (
globalOpenTablesDesc = prometheus.NewDesc(
prometheus.BuildFQName(cl.Namespace, dba, "open_tables"),
"lists tables that are currently open in the table cache",
[]string{"schema", "table", "name_blocked"}, nil,
)
)
type ScrapeOpenTables struct{}
// Name of the Scraper. Should be unique.
func (ScrapeOpenTables) Name() string {
return "open_tables"
}
// Help describes the role of the Scraper.
func (ScrapeOpenTables) Help() string {
return "Collect tables that are currently open"
}
// Version of MySQL from which scraper is available.
func (ScrapeOpenTables) Version() float64 {
return 5.1
}
// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeOpenTables) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
openTablesRows, err := db.QueryContext(ctx, OpenTablesQuery)
if err != nil {
return err
}
defer openTablesRows.Close()
var (
schema, table, name_blocked string
value float64
)
for openTablesRows.Next() {
if err := openTablesRows.Scan(
&schema, &table, &value, &name_blocked,
); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
globalOpenTablesDesc, prometheus.GaugeValue, value,
schema, table, name_blocked,
)
}
return nil
}
var _ cl.Scraper = ScrapeOpenTables{}