-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add collector to get table stats grouped by schema (#354)
* Add collector to get table stats grouped by schema Signed-off-by: Ricardo Rodríguez <rrodriguez@tuenti.com>
- Loading branch information
1 parent
22db3fc
commit 72b576b
Showing
5 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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" | ||
"database/sql" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/log" | ||
) | ||
|
||
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, db *sql.DB, ch chan<- prometheus.Metric) error { | ||
var varName, varVal string | ||
|
||
err := db.QueryRowContext(ctx, userstatCheckQuery).Scan(&varName, &varVal) | ||
if err != nil { | ||
log.Debugln("Detailed schema stats are not available.") | ||
return nil | ||
} | ||
if varVal == "OFF" { | ||
log.Debugf("MySQL @@%s is OFF.", 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{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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. | ||
|
||
package collector | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/smartystreets/goconvey/convey" | ||
"gopkg.in/DATA-DOG/go-sqlmock.v1" | ||
) | ||
|
||
func TestScrapeSchemaStat(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("error opening a stub database connection: %s", err) | ||
} | ||
defer db.Close() | ||
|
||
mock.ExpectQuery(sanitizeQuery(userstatCheckQuery)).WillReturnRows(sqlmock.NewRows([]string{"Variable_name", "Value"}). | ||
AddRow("userstat", "ON")) | ||
|
||
columns := []string{"TABLE_SCHEMA", "ROWS_READ", "ROWS_CHANGED", "ROWS_CHANGED_X_INDEXES"} | ||
rows := sqlmock.NewRows(columns). | ||
AddRow("mysql", 238, 0, 8). | ||
AddRow("default", 99, 1, 0) | ||
mock.ExpectQuery(sanitizeQuery(schemaStatQuery)).WillReturnRows(rows) | ||
|
||
ch := make(chan prometheus.Metric) | ||
go func() { | ||
if err = (ScrapeSchemaStat{}).Scrape(context.Background(), db, ch); err != nil { | ||
t.Errorf("error calling function on test: %s", err) | ||
} | ||
close(ch) | ||
}() | ||
|
||
expected := []MetricResult{ | ||
{labels: labelMap{"schema": "mysql"}, value: 238}, | ||
{labels: labelMap{"schema": "mysql"}, value: 0}, | ||
{labels: labelMap{"schema": "mysql"}, value: 8}, | ||
{labels: labelMap{"schema": "default"}, value: 99}, | ||
{labels: labelMap{"schema": "default"}, value: 1}, | ||
{labels: labelMap{"schema": "default"}, value: 0}, | ||
} | ||
convey.Convey("Metrics comparison", t, func() { | ||
for _, expect := range expected { | ||
got := readMetric(<-ch) | ||
convey.So(expect, convey.ShouldResemble, got) | ||
} | ||
}) | ||
|
||
// Ensure all SQL queries were executed | ||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled exceptions: %s", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters