Skip to content

Commit 8f50ba4

Browse files
authored
Merge pull request #34 from percona/PMM-2726_abort_on_timeout
PMM-2726: Abort on timeout.
2 parents 7d978fe + 6138387 commit 8f50ba4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+177
-92
lines changed

collector/binlog.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package collector
44

55
import (
6+
"context"
67
"database/sql"
78
"strconv"
89
"strings"
@@ -56,9 +57,9 @@ func (ScrapeBinlogSize) Version() float64 {
5657
}
5758

5859
// Scrape collects data.
59-
func (ScrapeBinlogSize) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error {
60+
func (ScrapeBinlogSize) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
6061
var logBin uint8
61-
err := db.QueryRow(logbinQuery).Scan(&logBin)
62+
err := db.QueryRowContext(ctx, logbinQuery).Scan(&logBin)
6263
if err != nil {
6364
return err
6465
}
@@ -67,7 +68,7 @@ func (ScrapeBinlogSize) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error {
6768
return nil
6869
}
6970

70-
masterLogRows, err := db.Query(binlogQuery)
71+
masterLogRows, err := db.QueryContext(ctx, binlogQuery)
7172
if err != nil {
7273
return err
7374
}

collector/binlog_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package collector
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/prometheus/client_golang/prometheus"
@@ -27,7 +28,7 @@ func TestScrapeBinlogSize(t *testing.T) {
2728

2829
ch := make(chan prometheus.Metric)
2930
go func() {
30-
if err = (ScrapeBinlogSize{}).Scrape(db, ch); err != nil {
31+
if err = (ScrapeBinlogSize{}).Scrape(context.Background(), db, ch); err != nil {
3132
t.Errorf("error calling function on test: %s", err)
3233
}
3334
close(ch)

collector/engine_innodb.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package collector
44

55
import (
6+
"context"
67
"database/sql"
78
"regexp"
89
"strconv"
@@ -37,8 +38,8 @@ func (ScrapeEngineInnodbStatus) Version() float64 {
3738
}
3839

3940
// Scrape collects data.
40-
func (ScrapeEngineInnodbStatus) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error {
41-
rows, err := db.Query(engineInnodbStatusQuery)
41+
func (ScrapeEngineInnodbStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
42+
rows, err := db.QueryContext(ctx, engineInnodbStatusQuery)
4243
if err != nil {
4344
return err
4445
}

collector/engine_innodb_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package collector
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/prometheus/client_golang/prometheus"
@@ -140,7 +141,7 @@ END OF INNODB MONITOR OUTPUT
140141

141142
ch := make(chan prometheus.Metric)
142143
go func() {
143-
if err = (ScrapeEngineInnodbStatus{}).Scrape(db, ch); err != nil {
144+
if err = (ScrapeEngineInnodbStatus{}).Scrape(context.Background(), db, ch); err != nil {
144145
t.Errorf("error calling function on test: %s", err)
145146
}
146147
close(ch)

collector/engine_tokudb.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package collector
44

55
import (
6+
"context"
67
"database/sql"
78
"strings"
89

@@ -53,8 +54,8 @@ func (ScrapeEngineTokudbStatus) Version() float64 {
5354
}
5455

5556
// Scrape collects data.
56-
func (ScrapeEngineTokudbStatus) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error {
57-
tokudbRows, err := db.Query(engineTokudbStatusQuery)
57+
func (ScrapeEngineTokudbStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
58+
tokudbRows, err := db.QueryContext(ctx, engineTokudbStatusQuery)
5859
if err != nil {
5960
return err
6061
}

collector/engine_tokudb_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package collector
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/prometheus/client_golang/prometheus"
@@ -44,7 +45,7 @@ func TestScrapeEngineTokudbStatus(t *testing.T) {
4445

4546
ch := make(chan prometheus.Metric)
4647
go func() {
47-
if err = (ScrapeEngineTokudbStatus{}).Scrape(db, ch); err != nil {
48+
if err = (ScrapeEngineTokudbStatus{}).Scrape(context.Background(), db, ch); err != nil {
4849
t.Errorf("error calling function on test: %s", err)
4950
}
5051
close(ch)

collector/exporter.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package collector
22

33
import (
4+
"context"
45
"database/sql"
56
"regexp"
67
"strconv"
@@ -32,16 +33,21 @@ var (
3233
)
3334
)
3435

36+
// Verify if Exporter implements prometheus.Collector
37+
var _ prometheus.Collector = (*Exporter)(nil)
38+
3539
// Exporter collects MySQL metrics. It implements prometheus.Collector.
3640
type Exporter struct {
41+
ctx context.Context
3742
db *sql.DB
3843
scrapers []Scraper
3944
metrics Metrics
4045
}
4146

4247
// New returns a new MySQL exporter for the provided DSN.
43-
func New(db *sql.DB, metrics Metrics, scrapers []Scraper) *Exporter {
48+
func New(ctx context.Context, db *sql.DB, metrics Metrics, scrapers []Scraper) *Exporter {
4449
return &Exporter{
50+
ctx: ctx,
4551
db: db,
4652
scrapers: scrapers,
4753
metrics: metrics,
@@ -58,21 +64,21 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
5864

5965
// Collect implements prometheus.Collector.
6066
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
61-
e.scrape(ch)
67+
e.scrape(e.ctx, ch)
6268

6369
ch <- e.metrics.TotalScrapes
6470
ch <- e.metrics.Error
6571
e.metrics.ScrapeErrors.Collect(ch)
6672
ch <- e.metrics.MySQLUp
6773
}
6874

69-
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
75+
func (e *Exporter) scrape(ctx context.Context, ch chan<- prometheus.Metric) {
7076
e.metrics.Error.Set(0)
7177
e.metrics.TotalScrapes.Inc()
7278
var err error
7379

7480
scrapeTime := time.Now()
75-
if err = e.db.Ping(); err != nil {
81+
if err = e.db.PingContext(ctx); err != nil {
7682
log.Errorln("Error pinging mysqld:", err)
7783
e.metrics.MySQLUp.Set(0)
7884
e.metrics.Error.Set(1)
@@ -82,7 +88,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
8288

8389
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds(), "connection")
8490

85-
versionNum := getMySQLVersion(e.db)
91+
versionNum := getMySQLVersion(ctx, e.db)
8692
wg := &sync.WaitGroup{}
8793
defer wg.Wait()
8894
for _, scraper := range e.scrapers {
@@ -94,7 +100,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
94100
defer wg.Done()
95101
label := "collect." + scraper.Name()
96102
scrapeTime := time.Now()
97-
if err := scraper.Scrape(e.db, ch); err != nil {
103+
if err := scraper.Scrape(ctx, e.db, ch); err != nil {
98104
log.Errorln("Error scraping for "+label+":", err)
99105
e.metrics.ScrapeErrors.WithLabelValues(label).Inc()
100106
e.metrics.Error.Set(1)
@@ -104,12 +110,12 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
104110
}
105111
}
106112

107-
func getMySQLVersion(db *sql.DB) float64 {
113+
func getMySQLVersion(ctx context.Context, db *sql.DB) float64 {
108114
var (
109115
versionStr string
110116
versionNum float64
111117
)
112-
err := db.QueryRow(versionQuery).Scan(&versionStr)
118+
err := db.QueryRowContext(ctx, versionQuery).Scan(&versionStr)
113119
if err == nil {
114120
r, _ := regexp.Compile(`^\d+\.\d+`)
115121
versionNum, _ = strconv.ParseFloat(r.FindString(versionStr), 64)

collector/exporter_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package collector
22

33
import (
4+
"context"
45
"database/sql"
56
"testing"
67

@@ -24,6 +25,7 @@ func TestExporter(t *testing.T) {
2425
defer db.Close()
2526

2627
exporter := New(
28+
context.Background(),
2729
db,
2830
NewMetrics(""),
2931
[]Scraper{
@@ -65,19 +67,20 @@ func TestGetMySQLVersion(t *testing.T) {
6567
}
6668
defer db.Close()
6769

70+
ctx := context.Background()
6871
convey.Convey("MySQL version extract", t, func() {
6972
mock.ExpectQuery(versionQuery).WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(""))
70-
convey.So(getMySQLVersion(db), convey.ShouldEqual, 999)
73+
convey.So(getMySQLVersion(ctx, db), convey.ShouldEqual, 999)
7174
mock.ExpectQuery(versionQuery).WillReturnRows(sqlmock.NewRows([]string{""}).AddRow("something"))
72-
convey.So(getMySQLVersion(db), convey.ShouldEqual, 999)
75+
convey.So(getMySQLVersion(ctx, db), convey.ShouldEqual, 999)
7376
mock.ExpectQuery(versionQuery).WillReturnRows(sqlmock.NewRows([]string{""}).AddRow("10.1.17-MariaDB"))
74-
convey.So(getMySQLVersion(db), convey.ShouldEqual, 10.1)
77+
convey.So(getMySQLVersion(ctx, db), convey.ShouldEqual, 10.1)
7578
mock.ExpectQuery(versionQuery).WillReturnRows(sqlmock.NewRows([]string{""}).AddRow("5.7.13-6-log"))
76-
convey.So(getMySQLVersion(db), convey.ShouldEqual, 5.7)
79+
convey.So(getMySQLVersion(ctx, db), convey.ShouldEqual, 5.7)
7780
mock.ExpectQuery(versionQuery).WillReturnRows(sqlmock.NewRows([]string{""}).AddRow("5.6.30-76.3-56-log"))
78-
convey.So(getMySQLVersion(db), convey.ShouldEqual, 5.6)
81+
convey.So(getMySQLVersion(ctx, db), convey.ShouldEqual, 5.6)
7982
mock.ExpectQuery(versionQuery).WillReturnRows(sqlmock.NewRows([]string{""}).AddRow("5.5.51-38.1"))
80-
convey.So(getMySQLVersion(db), convey.ShouldEqual, 5.5)
83+
convey.So(getMySQLVersion(ctx, db), convey.ShouldEqual, 5.5)
8184
})
8285

8386
// Ensure all SQL queries were executed

collector/global_status.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package collector
44

55
import (
6+
"context"
67
"database/sql"
78
"regexp"
89
"strconv"
@@ -86,8 +87,8 @@ func (ScrapeGlobalStatus) Version() float64 {
8687
}
8788

8889
// Scrape collects data.
89-
func (ScrapeGlobalStatus) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error {
90-
globalStatusRows, err := db.Query(globalStatusQuery)
90+
func (ScrapeGlobalStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
91+
globalStatusRows, err := db.QueryContext(ctx, globalStatusQuery)
9192
if err != nil {
9293
return err
9394
}

collector/global_status_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package collector
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/prometheus/client_golang/prometheus"
@@ -47,7 +48,7 @@ func TestScrapeGlobalStatus(t *testing.T) {
4748

4849
ch := make(chan prometheus.Metric)
4950
go func() {
50-
if err = (ScrapeGlobalStatus{}).Scrape(db, ch); err != nil {
51+
if err = (ScrapeGlobalStatus{}).Scrape(context.Background(), db, ch); err != nil {
5152
t.Errorf("error calling function on test: %s", err)
5253
}
5354
close(ch)

collector/global_variables.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package collector
44

55
import (
6+
"context"
67
"database/sql"
78
"regexp"
89
"strconv"
@@ -123,8 +124,8 @@ func (ScrapeGlobalVariables) Version() float64 {
123124
}
124125

125126
// Scrape collects data.
126-
func (ScrapeGlobalVariables) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error {
127-
globalVariablesRows, err := db.Query(globalVariablesQuery)
127+
func (ScrapeGlobalVariables) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
128+
globalVariablesRows, err := db.QueryContext(ctx, globalVariablesQuery)
128129
if err != nil {
129130
return err
130131
}

collector/global_variables_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package collector
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/prometheus/client_golang/prometheus"
@@ -37,7 +38,7 @@ func TestScrapeGlobalVariables(t *testing.T) {
3738

3839
ch := make(chan prometheus.Metric)
3940
go func() {
40-
if err = (ScrapeGlobalVariables{}).Scrape(db, ch); err != nil {
41+
if err = (ScrapeGlobalVariables{}).Scrape(context.Background(), db, ch); err != nil {
4142
t.Errorf("error calling function on test: %s", err)
4243
}
4344
close(ch)

collector/heartbeat.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package collector
44

55
import (
6+
"context"
67
"database/sql"
78
"flag"
89
"fmt"
@@ -71,9 +72,9 @@ func (ScrapeHeartbeat) Version() float64 {
7172
}
7273

7374
// Scrape collects data.
74-
func (ScrapeHeartbeat) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error {
75+
func (ScrapeHeartbeat) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
7576
query := fmt.Sprintf(heartbeatQuery, *collectHeartbeatDatabase, *collectHeartbeatTable)
76-
heartbeatRows, err := db.Query(query)
77+
heartbeatRows, err := db.QueryContext(ctx, query)
7778
if err != nil {
7879
return err
7980
}

collector/heartbeat_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package collector
22

33
import (
4+
"context"
45
"flag"
56
"testing"
67

@@ -33,7 +34,7 @@ func TestScrapeHeartbeat(t *testing.T) {
3334

3435
ch := make(chan prometheus.Metric)
3536
go func() {
36-
if err = (ScrapeHeartbeat{}).Scrape(db, ch); err != nil {
37+
if err = (ScrapeHeartbeat{}).Scrape(context.Background(), db, ch); err != nil {
3738
t.Errorf("error calling function on test: %s", err)
3839
}
3940
close(ch)

collector/info_schema_auto_increment.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package collector
44

55
import (
6+
"context"
67
"database/sql"
78

89
"github.com/prometheus/client_golang/prometheus"
@@ -55,8 +56,8 @@ func (ScrapeAutoIncrementColumns) Version() float64 {
5556
}
5657

5758
// Scrape collects data.
58-
func (ScrapeAutoIncrementColumns) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error {
59-
autoIncrementRows, err := db.Query(infoSchemaAutoIncrementQuery)
59+
func (ScrapeAutoIncrementColumns) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
60+
autoIncrementRows, err := db.QueryContext(ctx, infoSchemaAutoIncrementQuery)
6061
if err != nil {
6162
return err
6263
}

0 commit comments

Comments
 (0)