forked from prometheus/mysqld_exporter
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
bc2aac4
commit 6aa5465
Showing
4 changed files
with
126 additions
and
2 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
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{} |
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,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) | ||
} | ||
} |
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