@@ -37,7 +37,6 @@ type Exporter struct {
37
37
db * sql.DB
38
38
scrapers []Scraper
39
39
stats * Stats
40
- error prometheus.Gauge
41
40
mysqldUp prometheus.Gauge
42
41
}
43
42
@@ -47,12 +46,6 @@ func New(db *sql.DB, scrapers []Scraper, stats *Stats) *Exporter {
47
46
db : db ,
48
47
scrapers : scrapers ,
49
48
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
- }),
56
49
mysqldUp : prometheus .NewGauge (prometheus.GaugeOpts {
57
50
Namespace : namespace ,
58
51
Name : "up" ,
@@ -94,20 +87,21 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
94
87
e .scrape (ch )
95
88
96
89
ch <- e .stats .TotalScrapes
97
- ch <- e .error
90
+ ch <- e .stats . Error
98
91
e .stats .ScrapeErrors .Collect (ch )
99
92
ch <- e .mysqldUp
100
93
}
101
94
102
95
func (e * Exporter ) scrape (ch chan <- prometheus.Metric ) {
96
+ e .stats .Error .Set (0 )
103
97
e .stats .TotalScrapes .Inc ()
104
98
var err error
105
99
106
100
scrapeTime := time .Now ()
107
101
if err = e .db .Ping (); err != nil {
108
102
log .Errorln ("Error pinging mysqld:" , err )
109
103
e .mysqldUp .Set (0 )
110
- e .error .Set (1 )
104
+ e .stats . Error .Set (1 )
111
105
return
112
106
}
113
107
e .mysqldUp .Set (1 )
@@ -128,7 +122,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
128
122
if err := scraper .Scrape (e .db , ch ); err != nil {
129
123
log .Errorln ("Error scraping for " + label + ":" , err )
130
124
e .stats .ScrapeErrors .WithLabelValues (label ).Inc ()
131
- e .error .Set (1 )
125
+ e .stats . Error .Set (1 )
132
126
}
133
127
ch <- prometheus .MustNewConstMetric (scrapeDurationDesc , prometheus .GaugeValue , time .Since (scrapeTime ).Seconds (), label )
134
128
}(scraper )
@@ -155,6 +149,7 @@ func getMySQLVersion(db *sql.DB) float64 {
155
149
type Stats struct {
156
150
TotalScrapes prometheus.Counter
157
151
ScrapeErrors * prometheus.CounterVec
152
+ Error prometheus.Gauge
158
153
}
159
154
160
155
func NewStats (resolution string ) * Stats {
@@ -175,5 +170,11 @@ func NewStats(resolution string) *Stats {
175
170
Name : "scrape_errors_total" ,
176
171
Help : "Total number of times an error occurred scraping a MySQL." ,
177
172
}, []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
+ }),
178
179
}
179
180
}
0 commit comments