Skip to content

Commit 7324824

Browse files
authored
Merge pull request #25 from percona/PMM-2221_fix_last_scrape_error
PMM-2221: Fix last_scrape_error.
2 parents 7fdcaba + 217d470 commit 7324824

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

collector/exporter.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ type Exporter struct {
3737
db *sql.DB
3838
scrapers []Scraper
3939
stats *Stats
40-
error prometheus.Gauge
4140
mysqldUp prometheus.Gauge
4241
}
4342

@@ -47,12 +46,6 @@ func New(db *sql.DB, scrapers []Scraper, stats *Stats) *Exporter {
4746
db: db,
4847
scrapers: scrapers,
4948
stats: stats,
50-
error: prometheus.NewGauge(prometheus.GaugeOpts{
51-
Namespace: namespace,
52-
Subsystem: exporter,
53-
Name: "last_scrape_error",
54-
Help: "Whether the last scrape of metrics from MySQL resulted in an error (1 for error, 0 for success).",
55-
}),
5649
mysqldUp: prometheus.NewGauge(prometheus.GaugeOpts{
5750
Namespace: namespace,
5851
Name: "up",
@@ -94,20 +87,21 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
9487
e.scrape(ch)
9588

9689
ch <- e.stats.TotalScrapes
97-
ch <- e.error
90+
ch <- e.stats.Error
9891
e.stats.ScrapeErrors.Collect(ch)
9992
ch <- e.mysqldUp
10093
}
10194

10295
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
96+
e.stats.Error.Set(0)
10397
e.stats.TotalScrapes.Inc()
10498
var err error
10599

106100
scrapeTime := time.Now()
107101
if err = e.db.Ping(); err != nil {
108102
log.Errorln("Error pinging mysqld:", err)
109103
e.mysqldUp.Set(0)
110-
e.error.Set(1)
104+
e.stats.Error.Set(1)
111105
return
112106
}
113107
e.mysqldUp.Set(1)
@@ -128,7 +122,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
128122
if err := scraper.Scrape(e.db, ch); err != nil {
129123
log.Errorln("Error scraping for "+label+":", err)
130124
e.stats.ScrapeErrors.WithLabelValues(label).Inc()
131-
e.error.Set(1)
125+
e.stats.Error.Set(1)
132126
}
133127
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds(), label)
134128
}(scraper)
@@ -155,6 +149,7 @@ func getMySQLVersion(db *sql.DB) float64 {
155149
type Stats struct {
156150
TotalScrapes prometheus.Counter
157151
ScrapeErrors *prometheus.CounterVec
152+
Error prometheus.Gauge
158153
}
159154

160155
func NewStats(resolution string) *Stats {
@@ -175,5 +170,11 @@ func NewStats(resolution string) *Stats {
175170
Name: "scrape_errors_total",
176171
Help: "Total number of times an error occurred scraping a MySQL.",
177172
}, []string{"collector"}),
173+
Error: prometheus.NewGauge(prometheus.GaugeOpts{
174+
Namespace: namespace,
175+
Subsystem: subsystem,
176+
Name: "last_scrape_error",
177+
Help: "Whether the last scrape of metrics from MySQL resulted in an error (1 for error, 0 for success).",
178+
}),
178179
}
179180
}

0 commit comments

Comments
 (0)