Skip to content

Commit

Permalink
PMM-11267: Add collector to gather plugins installed on mysql (#113)
Browse files Browse the repository at this point in the history
* PMM-11267: Add collector to gather plugins installed on mysql

* PMM-11267 trigger actions

---------

Co-authored-by: Alex Tymchuk <alexander.tymchuk@percona.com>
Co-authored-by: Alexey Mukas <91831719+ritbl@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 7, 2023
1 parent bc2aac4 commit 6aa5465
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ collect.engine_innodb_status | 5.1 | C
collect.engine_tokudb_status | 5.6 | Collect from SHOW ENGINE TOKUDB STATUS.
collect.global_status | 5.1 | Collect from SHOW GLOBAL STATUS (Enabled by default)
collect.global_variables | 5.1 | Collect from SHOW GLOBAL VARIABLES (Enabled by default)
collect.plugins | 5.1 | Collect from SHOW PLUGINS
collect.info_schema.clientstats | 5.5 | If running with userstat=1, set to true to collect client statistics.
collect.info_schema.innodb_metrics | 5.6 | Collect metrics from information_schema.innodb_metrics.
collect.info_schema.innodb_tablespaces | 5.7 | Collect metrics from information_schema.innodb_sys_tablespaces.
Expand Down
70 changes: 70 additions & 0 deletions collector/plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package collector

import (
"context"
"database/sql"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

const (
pluginsQuery = `SHOW PLUGINS`
)

var (
pluginDescription = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "plugin"),
"MySQL plugin",
[]string{"name", "status", "type", "library", "license"}, nil,
)
)

type plugin struct {
Name sql.NullString
Status sql.NullString
Type sql.NullString
Library sql.NullString
License sql.NullString
}

// ScrapePlugins collects from `SHOW PLUGINS`.
type ScrapePlugins struct{}

// Name of the Scraper. Should be unique.
func (ScrapePlugins) Name() string {
return "plugins"
}

// Help describes the role of the Scraper.
func (ScrapePlugins) Help() string {
return "Collect from SHOW PLUGINS"
}

// Version of MySQL from which scraper is available.
func (ScrapePlugins) Version() float64 {
return 5.1
}

func (ScrapePlugins) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
showPluginsRows, err := db.QueryContext(ctx, pluginsQuery)
if err != nil {
return err
}
defer showPluginsRows.Close()
for showPluginsRows.Next() {
var pluginVal plugin
if err := showPluginsRows.Scan(&pluginVal.Name, &pluginVal.Status, &pluginVal.Type, &pluginVal.Library, &pluginVal.License); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
pluginDescription, prometheus.GaugeValue, 1,
pluginVal.Name.String, pluginVal.Status.String, pluginVal.Type.String, pluginVal.Library.String, pluginVal.License.String,
)
}

return nil
}

// check interface
var _ Scraper = ScrapePlugins{}
50 changes: 50 additions & 0 deletions collector/plugins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package collector

import (
"context"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
)

func TestScrapePlugins(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("error opening a stub database connection: %s", err)
}
defer db.Close()
columns := []string{"Name", "Status", "Type", "Library", "License"}
rows := sqlmock.NewRows(columns).
AddRow("INNODB_SYS_COLUMNS", "ACTIVE", "INFORMATION SCHEMA", nil, "GPL").
AddRow("MRG_MYISAM", "ACTIVE", "STORAGE ENGINE", nil, "GPL").
AddRow(nil, nil, nil, nil, nil)
mock.ExpectQuery(sanitizeQuery(pluginsQuery)).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapePlugins{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
}()
counterExpected := []MetricResult{
{labels: labelMap{"name": "INNODB_SYS_COLUMNS", "status": "ACTIVE", "type": "INFORMATION SCHEMA", "library": "", "license": "GPL"}, value: 1, metricType: dto.MetricType_GAUGE},
{labels: labelMap{"name": "MRG_MYISAM", "status": "ACTIVE", "type": "STORAGE ENGINE", "library": "", "license": "GPL"}, value: 1, metricType: dto.MetricType_GAUGE},
{labels: labelMap{"name": "", "status": "", "type": "", "library": "", "license": ""}, value: 1, metricType: dto.MetricType_GAUGE},
}
convey.Convey("Metrics comparison", t, func() {
for _, expect := range counterExpected {
got := readMetric(<-ch)
convey.So(got, convey.ShouldResemble, expect)
}
})

// Ensure all SQL queries were executed
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled exceptions: %s", err)
}
}
7 changes: 5 additions & 2 deletions mysqld_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/go-sql-driver/mysql"
"github.com/percona/mysqld_exporter/collector"
pcl "github.com/percona/mysqld_exporter/percona/perconacollector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
Expand All @@ -42,6 +40,9 @@ import (
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/ini.v1"

"github.com/percona/mysqld_exporter/collector"
pcl "github.com/percona/mysqld_exporter/percona/perconacollector"
)

var (
Expand Down Expand Up @@ -158,6 +159,7 @@ var scrapers = map[collector.Scraper]bool{
pcl.ScrapeGlobalStatus{}: false,
collector.ScrapeGlobalStatus{}: false,
collector.ScrapeGlobalVariables{}: false,
collector.ScrapePlugins{}: false,
collector.ScrapeSlaveStatus{}: false,
pcl.ScrapeProcesslist{}: false,
collector.ScrapeProcesslist{}: false,
Expand Down Expand Up @@ -224,6 +226,7 @@ var scrapersMr = map[collector.Scraper]struct{}{
// TODO Remove
var scrapersLr = map[collector.Scraper]struct{}{
collector.ScrapeGlobalVariables{}: {},
collector.ScrapePlugins{}: {},
collector.ScrapeTableSchema{}: {},
collector.ScrapeAutoIncrementColumns{}: {},
collector.ScrapeBinlogSize{}: {},
Expand Down

0 comments on commit 6aa5465

Please sign in to comment.