-
Notifications
You must be signed in to change notification settings - Fork 772
/
Copy pathinfo_schema_schemastats.go
128 lines (111 loc) · 3.77 KB
/
info_schema_schemastats.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
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Scrape `information_schema.table_statistics`.
package collector
import (
"context"
"log/slog"
"github.com/prometheus/client_golang/prometheus"
)
const schemaStatQuery = `
SELECT
TABLE_SCHEMA,
SUM(ROWS_READ) AS ROWS_READ,
SUM(ROWS_CHANGED) AS ROWS_CHANGED,
SUM(ROWS_CHANGED_X_INDEXES) AS ROWS_CHANGED_X_INDEXES
FROM information_schema.TABLE_STATISTICS
GROUP BY TABLE_SCHEMA;
`
// Metric descriptors.
var (
infoSchemaStatsRowsReadDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, informationSchema, "schema_statistics_rows_read_total"),
"The number of rows read from the schema.",
[]string{"schema"}, nil,
)
infoSchemaStatsRowsChangedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, informationSchema, "schema_statistics_rows_changed_total"),
"The number of rows changed in the schema.",
[]string{"schema"}, nil,
)
infoSchemaStatsRowsChangedXIndexesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, informationSchema, "schema_statistics_rows_changed_x_indexes_total"),
"The number of rows changed in the schema, multiplied by the number of indexes changed.",
[]string{"schema"}, nil,
)
)
// ScrapeSchemaStat collects from `information_schema.table_statistics` grouped by schema.
type ScrapeSchemaStat struct{}
// Name of the Scraper. Should be unique.
func (ScrapeSchemaStat) Name() string {
return "info_schema.schemastats"
}
// Help describes the role of the Scraper.
func (ScrapeSchemaStat) Help() string {
return "If running with userstat=1, set to true to collect schema statistics"
}
// Version of MySQL from which scraper is available.
func (ScrapeSchemaStat) Version() float64 {
return 5.1
}
// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeSchemaStat) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
var varName, varVal string
db := instance.getDB()
err := db.QueryRowContext(ctx, userstatCheckQuery).Scan(&varName, &varVal)
if err != nil {
logger.Debug("Detailed schema stats are not available.")
return nil
}
if varVal == "OFF" {
logger.Debug("MySQL variable is OFF.", "var", varName)
return nil
}
informationSchemaTableStatisticsRows, err := db.QueryContext(ctx, schemaStatQuery)
if err != nil {
return err
}
defer informationSchemaTableStatisticsRows.Close()
var (
tableSchema string
rowsRead uint64
rowsChanged uint64
rowsChangedXIndexes uint64
)
for informationSchemaTableStatisticsRows.Next() {
err = informationSchemaTableStatisticsRows.Scan(
&tableSchema,
&rowsRead,
&rowsChanged,
&rowsChangedXIndexes,
)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
infoSchemaStatsRowsReadDesc, prometheus.CounterValue, float64(rowsRead),
tableSchema,
)
ch <- prometheus.MustNewConstMetric(
infoSchemaStatsRowsChangedDesc, prometheus.CounterValue, float64(rowsChanged),
tableSchema,
)
ch <- prometheus.MustNewConstMetric(
infoSchemaStatsRowsChangedXIndexesDesc, prometheus.CounterValue, float64(rowsChangedXIndexes),
tableSchema,
)
}
return nil
}
// check interface
var _ Scraper = ScrapeSchemaStat{}