@@ -34,31 +34,19 @@ var (
34
34
35
35
// Exporter collects MySQL metrics. It implements prometheus.Collector.
36
36
type Exporter struct {
37
- db * sql.DB
38
- scrapers []Scraper
39
- error prometheus.Gauge
40
- totalScrapes prometheus.Counter
41
- scrapeErrors * prometheus.CounterVec
42
- mysqldUp prometheus.Gauge
37
+ db * sql.DB
38
+ scrapers []Scraper
39
+ stats * Stats
40
+ error prometheus.Gauge
41
+ mysqldUp prometheus.Gauge
43
42
}
44
43
45
44
// New returns a new MySQL exporter for the provided DSN.
46
- func New (db * sql.DB , scrapers []Scraper ) * Exporter {
45
+ func New (db * sql.DB , scrapers []Scraper , stats * Stats ) * Exporter {
47
46
return & Exporter {
48
47
db : db ,
49
48
scrapers : scrapers ,
50
- totalScrapes : prometheus .NewCounter (prometheus.CounterOpts {
51
- Namespace : namespace ,
52
- Subsystem : exporter ,
53
- Name : "scrapes_total" ,
54
- Help : "Total number of times MySQL was scraped for metrics." ,
55
- }),
56
- scrapeErrors : prometheus .NewCounterVec (prometheus.CounterOpts {
57
- Namespace : namespace ,
58
- Subsystem : exporter ,
59
- Name : "scrape_errors_total" ,
60
- Help : "Total number of times an error occurred scraping a MySQL." ,
61
- }, []string {"collector" }),
49
+ stats : stats ,
62
50
error : prometheus .NewGauge (prometheus.GaugeOpts {
63
51
Namespace : namespace ,
64
52
Subsystem : exporter ,
@@ -105,14 +93,14 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
105
93
func (e * Exporter ) Collect (ch chan <- prometheus.Metric ) {
106
94
e .scrape (ch )
107
95
108
- ch <- e .totalScrapes
96
+ ch <- e .stats . TotalScrapes
109
97
ch <- e .error
110
- e .scrapeErrors .Collect (ch )
98
+ e .stats . ScrapeErrors .Collect (ch )
111
99
ch <- e .mysqldUp
112
100
}
113
101
114
102
func (e * Exporter ) scrape (ch chan <- prometheus.Metric ) {
115
- e .totalScrapes .Inc ()
103
+ e .stats . TotalScrapes .Inc ()
116
104
var err error
117
105
118
106
scrapeTime := time .Now ()
@@ -122,13 +110,10 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
122
110
e .error .Set (1 )
123
111
return
124
112
}
125
-
126
113
e .mysqldUp .Set (1 )
127
-
128
- versionNum := getMySQLVersion (e .db )
129
-
130
114
ch <- prometheus .MustNewConstMetric (scrapeDurationDesc , prometheus .GaugeValue , time .Since (scrapeTime ).Seconds (), "connection" )
131
115
116
+ versionNum := getMySQLVersion (e .db )
132
117
wg := & sync.WaitGroup {}
133
118
defer wg .Wait ()
134
119
for _ , scraper := range e .scrapers {
@@ -142,7 +127,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
142
127
scrapeTime := time .Now ()
143
128
if err := scraper .Scrape (e .db , ch ); err != nil {
144
129
log .Errorln ("Error scraping for " + label + ":" , err )
145
- e .scrapeErrors .WithLabelValues (label ).Inc ()
130
+ e .stats . ScrapeErrors .WithLabelValues (label ).Inc ()
146
131
e .error .Set (1 )
147
132
}
148
133
ch <- prometheus .MustNewConstMetric (scrapeDurationDesc , prometheus .GaugeValue , time .Since (scrapeTime ).Seconds (), label )
@@ -166,3 +151,29 @@ func getMySQLVersion(db *sql.DB) float64 {
166
151
}
167
152
return versionNum
168
153
}
154
+
155
+ type Stats struct {
156
+ TotalScrapes prometheus.Counter
157
+ ScrapeErrors * prometheus.CounterVec
158
+ }
159
+
160
+ func NewStats (resolution string ) * Stats {
161
+ subsystem := exporter
162
+ if resolution != "" {
163
+ subsystem = exporter + "_" + resolution
164
+ }
165
+ return & Stats {
166
+ TotalScrapes : prometheus .NewCounter (prometheus.CounterOpts {
167
+ Namespace : namespace ,
168
+ Subsystem : subsystem ,
169
+ Name : "scrapes_total" ,
170
+ Help : "Total number of times MySQL was scraped for metrics." ,
171
+ }),
172
+ ScrapeErrors : prometheus .NewCounterVec (prometheus.CounterOpts {
173
+ Namespace : namespace ,
174
+ Subsystem : subsystem ,
175
+ Name : "scrape_errors_total" ,
176
+ Help : "Total number of times an error occurred scraping a MySQL." ,
177
+ }, []string {"collector" }),
178
+ }
179
+ }
0 commit comments