@@ -36,75 +36,50 @@ var (
36
36
type Exporter struct {
37
37
db * sql.DB
38
38
scrapers []Scraper
39
- stats * Stats
40
- mysqldUp prometheus.Gauge
39
+ metrics Metrics
41
40
}
42
41
43
42
// New returns a new MySQL exporter for the provided DSN.
44
- func New (db * sql.DB , scrapers []Scraper , stats * Stats ) * Exporter {
43
+ func New (db * sql.DB , metrics Metrics , scrapers []Scraper ) * Exporter {
45
44
return & Exporter {
46
45
db : db ,
47
46
scrapers : scrapers ,
48
- stats : stats ,
49
- mysqldUp : prometheus .NewGauge (prometheus.GaugeOpts {
50
- Namespace : namespace ,
51
- Name : "up" ,
52
- Help : "Whether the MySQL server is up." ,
53
- }),
47
+ metrics : metrics ,
54
48
}
55
49
}
56
50
57
51
// Describe implements prometheus.Collector.
58
52
func (e * Exporter ) Describe (ch chan <- * prometheus.Desc ) {
59
- // We cannot know in advance what metrics the exporter will generate
60
- // from MySQL. So we use the poor man's describe method: Run a collect
61
- // and send the descriptors of all the collected metrics. The problem
62
- // here is that we need to connect to the MySQL DB. If it is currently
63
- // unavailable, the descriptors will be incomplete. Since this is a
64
- // stand-alone exporter and not used as a library within other code
65
- // implementing additional metrics, the worst that can happen is that we
66
- // don't detect inconsistent metrics created by this exporter
67
- // itself. Also, a change in the monitored MySQL instance may change the
68
- // exported metrics during the runtime of the exporter.
69
-
70
- metricCh := make (chan prometheus.Metric )
71
- doneCh := make (chan struct {})
72
-
73
- go func () {
74
- for m := range metricCh {
75
- ch <- m .Desc ()
76
- }
77
- close (doneCh )
78
- }()
79
-
80
- e .Collect (metricCh )
81
- close (metricCh )
82
- <- doneCh
53
+ ch <- e .metrics .TotalScrapes .Desc ()
54
+ ch <- e .metrics .Error .Desc ()
55
+ e .metrics .ScrapeErrors .Describe (ch )
56
+ ch <- e .metrics .MySQLUp .Desc ()
83
57
}
84
58
85
59
// Collect implements prometheus.Collector.
86
60
func (e * Exporter ) Collect (ch chan <- prometheus.Metric ) {
87
61
e .scrape (ch )
88
62
89
- ch <- e .stats .TotalScrapes
90
- ch <- e .stats .Error
91
- e .stats .ScrapeErrors .Collect (ch )
92
- ch <- e .mysqldUp
63
+ ch <- e .metrics .TotalScrapes
64
+ ch <- e .metrics .Error
65
+ e .metrics .ScrapeErrors .Collect (ch )
66
+ ch <- e .metrics . MySQLUp
93
67
}
94
68
95
69
func (e * Exporter ) scrape (ch chan <- prometheus.Metric ) {
96
- e .stats .Error .Set (0 )
97
- e .stats .TotalScrapes .Inc ()
70
+ e .metrics .Error .Set (0 )
71
+ e .metrics .TotalScrapes .Inc ()
98
72
var err error
99
73
100
74
scrapeTime := time .Now ()
101
75
if err = e .db .Ping (); err != nil {
102
76
log .Errorln ("Error pinging mysqld:" , err )
103
- e .mysqldUp .Set (0 )
104
- e .stats .Error .Set (1 )
77
+ e .metrics . MySQLUp .Set (0 )
78
+ e .metrics .Error .Set (1 )
105
79
return
106
80
}
107
- e .mysqldUp .Set (1 )
81
+ e .metrics .MySQLUp .Set (1 )
82
+
108
83
ch <- prometheus .MustNewConstMetric (scrapeDurationDesc , prometheus .GaugeValue , time .Since (scrapeTime ).Seconds (), "connection" )
109
84
110
85
versionNum := getMySQLVersion (e .db )
@@ -121,8 +96,8 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
121
96
scrapeTime := time .Now ()
122
97
if err := scraper .Scrape (e .db , ch ); err != nil {
123
98
log .Errorln ("Error scraping for " + label + ":" , err )
124
- e .stats .ScrapeErrors .WithLabelValues (label ).Inc ()
125
- e .stats .Error .Set (1 )
99
+ e .metrics .ScrapeErrors .WithLabelValues (label ).Inc ()
100
+ e .metrics .Error .Set (1 )
126
101
}
127
102
ch <- prometheus .MustNewConstMetric (scrapeDurationDesc , prometheus .GaugeValue , time .Since (scrapeTime ).Seconds (), label )
128
103
}(scraper )
@@ -146,18 +121,21 @@ func getMySQLVersion(db *sql.DB) float64 {
146
121
return versionNum
147
122
}
148
123
149
- type Stats struct {
124
+ // Metrics represents exporter metrics which values can be carried between http requests.
125
+ type Metrics struct {
150
126
TotalScrapes prometheus.Counter
151
127
ScrapeErrors * prometheus.CounterVec
152
128
Error prometheus.Gauge
129
+ MySQLUp prometheus.Gauge
153
130
}
154
131
155
- func NewStats (resolution string ) * Stats {
132
+ // NewMetrics creates new Metrics instance.
133
+ func NewMetrics (resolution string ) Metrics {
156
134
subsystem := exporter
157
135
if resolution != "" {
158
136
subsystem = exporter + "_" + resolution
159
137
}
160
- return & Stats {
138
+ return Metrics {
161
139
TotalScrapes : prometheus .NewCounter (prometheus.CounterOpts {
162
140
Namespace : namespace ,
163
141
Subsystem : subsystem ,
@@ -176,5 +154,10 @@ func NewStats(resolution string) *Stats {
176
154
Name : "last_scrape_error" ,
177
155
Help : "Whether the last scrape of metrics from MySQL resulted in an error (1 for error, 0 for success)." ,
178
156
}),
157
+ MySQLUp : prometheus .NewGauge (prometheus.GaugeOpts {
158
+ Namespace : namespace ,
159
+ Name : "up" ,
160
+ Help : "Whether the MySQL server is up." ,
161
+ }),
179
162
}
180
163
}
0 commit comments