forked from prometheus/mysqld_exporter
/
perf_schema_file_instances.go
109 lines (93 loc) · 3.38 KB
/
perf_schema_file_instances.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
// Scrape `performance_schema.file_summary_by_instance`.
package collector
import (
"context"
"database/sql"
"strings"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/alecthomas/kingpin.v2"
)
const perfFileInstancesQuery = `
SELECT
FILE_NAME, EVENT_NAME,
COUNT_READ, COUNT_WRITE,
SUM_NUMBER_OF_BYTES_READ, SUM_NUMBER_OF_BYTES_WRITE
FROM performance_schema.file_summary_by_instance
where FILE_NAME REGEXP ?
`
// Metric descriptors.
var (
performanceSchemaFileInstancesFilter = kingpin.Flag(
"collect.perf_schema.file_instances.filter",
"RegEx file_name filter for performance_schema.file_summary_by_instance",
).Default(".*").String()
performanceSchemaFileInstancesRemovePrefix = kingpin.Flag(
"collect.perf_schema.file_instances.remove_prefix",
"Remove path prefix in performance_schema.file_summary_by_instance",
).Default("/var/lib/mysql/").String()
performanceSchemaFileInstancesBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "file_instances_bytes"),
"The number of bytes processed by file read/write operations.",
[]string{"file_name", "event_name", "mode"}, nil,
)
performanceSchemaFileInstancesCountDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "file_instances_total"),
"The total number of file read/write operations.",
[]string{"file_name", "event_name", "mode"}, nil,
)
)
// ScrapePerfFileInstances collects from `performance_schema.file_summary_by_instance`.
type ScrapePerfFileInstances struct{}
// Name of the Scraper.
func (ScrapePerfFileInstances) Name() string {
return "perf_schema.file_instances"
}
// Help returns additional information about Scraper.
func (ScrapePerfFileInstances) Help() string {
return "Collect metrics from performance_schema.file_summary_by_instance"
}
// Version of MySQL from which scraper is available.
func (ScrapePerfFileInstances) Version() float64 {
return 5.5
}
// Scrape collects data.
func (ScrapePerfFileInstances) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
// Timers here are returned in picoseconds.
perfSchemaFileInstancesRows, err := db.QueryContext(ctx, perfFileInstancesQuery, *performanceSchemaFileInstancesFilter)
if err != nil {
return err
}
defer perfSchemaFileInstancesRows.Close()
var (
fileName, eventName string
countRead, countWrite uint64
sumBytesRead, sumBytesWritten uint64
)
for perfSchemaFileInstancesRows.Next() {
if err := perfSchemaFileInstancesRows.Scan(
&fileName, &eventName,
&countRead, &countWrite,
&sumBytesRead, &sumBytesWritten,
); err != nil {
return err
}
fileName = strings.TrimPrefix(fileName, *performanceSchemaFileInstancesRemovePrefix)
ch <- prometheus.MustNewConstMetric(
performanceSchemaFileInstancesCountDesc, prometheus.CounterValue, float64(countRead),
fileName, eventName, "read",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaFileInstancesCountDesc, prometheus.CounterValue, float64(countWrite),
fileName, eventName, "write",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaFileInstancesBytesDesc, prometheus.CounterValue, float64(sumBytesRead),
fileName, eventName, "read",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaFileInstancesBytesDesc, prometheus.CounterValue, float64(sumBytesWritten),
fileName, eventName, "write",
)
}
return nil
}