Skip to content

Commit 163327f

Browse files
committed
PMM-2221: Fix last_scrape_error.
1 parent 0a335cc commit 163327f

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

collector/exporter.go

+10-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,7 +87,7 @@ 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
}
@@ -107,7 +100,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
107100
if err = e.db.Ping(); err != nil {
108101
log.Errorln("Error pinging mysqld:", err)
109102
e.mysqldUp.Set(0)
110-
e.error.Set(1)
103+
e.stats.Error.Set(1)
111104
return
112105
}
113106
e.mysqldUp.Set(1)
@@ -128,7 +121,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
128121
if err := scraper.Scrape(e.db, ch); err != nil {
129122
log.Errorln("Error scraping for "+label+":", err)
130123
e.stats.ScrapeErrors.WithLabelValues(label).Inc()
131-
e.error.Set(1)
124+
e.stats.Error.Set(1)
132125
}
133126
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds(), label)
134127
}(scraper)
@@ -155,6 +148,7 @@ func getMySQLVersion(db *sql.DB) float64 {
155148
type Stats struct {
156149
TotalScrapes prometheus.Counter
157150
ScrapeErrors *prometheus.CounterVec
151+
Error prometheus.Gauge
158152
}
159153

160154
func NewStats(resolution string) *Stats {
@@ -175,5 +169,11 @@ func NewStats(resolution string) *Stats {
175169
Name: "scrape_errors_total",
176170
Help: "Total number of times an error occurred scraping a MySQL.",
177171
}, []string{"collector"}),
172+
Error: prometheus.NewGauge(prometheus.GaugeOpts{
173+
Namespace: namespace,
174+
Subsystem: subsystem,
175+
Name: "last_scrape_error",
176+
Help: "Whether the last scrape of metrics from MySQL resulted in an error (1 for error, 0 for success).",
177+
}),
178178
}
179179
}

0 commit comments

Comments
 (0)