Skip to content

Commit 9ba762e

Browse files
committed
Change to const metrics
1 parent 56a3ca2 commit 9ba762e

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

exporter.go

+25-21
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,25 @@ const (
3333
)
3434

3535
var (
36-
scrapeDurations = prometheus.NewGaugeVec(
37-
prometheus.GaugeOpts{
38-
Namespace: collector.Namespace,
39-
Subsystem: "exporter",
40-
Name: "collector_duration_seconds",
41-
Help: "wmi_exporter: Duration of a collection.",
42-
},
36+
scrapeDurationDesc = prometheus.NewDesc(
37+
prometheus.BuildFQName(collector.Namespace, "exporter", "collector_duration_seconds"),
38+
"wmi_exporter: Duration of a collection.",
4339
[]string{"collector"},
40+
nil,
4441
)
45-
scrapeSuccess = prometheus.NewGaugeVec(
46-
prometheus.GaugeOpts{
47-
Namespace: collector.Namespace,
48-
Subsystem: "exporter",
49-
Name: "collector_success",
50-
Help: "wmi_exporter: Whether the collector was successful.",
51-
},
42+
scrapeSuccessDesc = prometheus.NewDesc(
43+
prometheus.BuildFQName(collector.Namespace, "exporter", "collector_success"),
44+
"wmi_exporter: Whether the collector was successful.",
5245
[]string{"collector"},
46+
nil,
5347
)
5448
)
5549

5650
// Describe sends all the descriptors of the collectors included to
5751
// the provided channel.
5852
func (coll WmiCollector) Describe(ch chan<- *prometheus.Desc) {
59-
scrapeDurations.Describe(ch)
60-
scrapeSuccess.Describe(ch)
53+
ch <- scrapeDurationDesc
54+
ch <- scrapeSuccessDesc
6155
}
6256

6357
// Collect sends the collected metrics from each of the collectors to
@@ -73,8 +67,6 @@ func (coll WmiCollector) Collect(ch chan<- prometheus.Metric) {
7367
}(name, c)
7468
}
7569
wg.Wait()
76-
scrapeDurations.Collect(ch)
77-
scrapeSuccess.Collect(ch)
7870
}
7971

8072
func filterAvailableCollectors(collectors string) string {
@@ -92,15 +84,27 @@ func execute(name string, c collector.Collector, ch chan<- prometheus.Metric) {
9284
begin := time.Now()
9385
err := c.Collect(ch)
9486
duration := time.Since(begin)
87+
var success float64
9588

9689
if err != nil {
9790
log.Errorf("ERROR: %s collector failed after %fs: %s", name, duration.Seconds(), err)
98-
scrapeSuccess.WithLabelValues(name).Set(0)
91+
success = 0
9992
} else {
10093
log.Debugf("OK: %s collector succeeded after %fs.", name, duration.Seconds())
101-
scrapeSuccess.WithLabelValues(name).Set(1)
94+
success = 1
10295
}
103-
scrapeDurations.WithLabelValues(name).Set(duration.Seconds())
96+
ch <- prometheus.MustNewConstMetric(
97+
scrapeDurationDesc,
98+
prometheus.GaugeValue,
99+
duration.Seconds(),
100+
name,
101+
)
102+
ch <- prometheus.MustNewConstMetric(
103+
scrapeSuccessDesc,
104+
prometheus.GaugeValue,
105+
success,
106+
name,
107+
)
104108
}
105109

106110
func expandEnabledCollectors(enabled string) []string {

0 commit comments

Comments
 (0)