Skip to content

Commit

Permalink
Change to const metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpett committed Jul 5, 2017
1 parent 56a3ca2 commit 49c11ee
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,25 @@ const (
)

var (
scrapeDurations = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: collector.Namespace,
Subsystem: "exporter",
Name: "collector_duration_seconds",
Help: "wmi_exporter: Duration of a collection.",
},
scrapeDurationDesc = prometheus.NewDesc(
prometheus.BuildFQName(collector.Namespace, "exporter", "collector_duration_seconds"),
"wmi_exporter: Duration of a collection.",
[]string{"collector"},
nil,
)
scrapeSuccess = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: collector.Namespace,
Subsystem: "exporter",
Name: "collector_success",
Help: "wmi_exporter: Whether the collector was successful.",
},
scrapeSuccessDesc = prometheus.NewDesc(
prometheus.BuildFQName(collector.Namespace, "exporter", "collector_success"),
"wmi_exporter: Whether the collector was successful.",
[]string{"collector"},
nil,
)
)

// Describe sends all the descriptors of the collectors included to
// the provided channel.
func (coll WmiCollector) Describe(ch chan<- *prometheus.Desc) {
scrapeDurations.Describe(ch)
scrapeSuccess.Describe(ch)
ch <- scrapeDurationDesc
ch <- scrapeSuccessDesc
}

// Collect sends the collected metrics from each of the collectors to
Expand All @@ -73,8 +67,6 @@ func (coll WmiCollector) Collect(ch chan<- prometheus.Metric) {
}(name, c)
}
wg.Wait()
scrapeDurations.Collect(ch)
scrapeSuccess.Collect(ch)
}

func filterAvailableCollectors(collectors string) string {
Expand All @@ -92,15 +84,27 @@ func execute(name string, c collector.Collector, ch chan<- prometheus.Metric) {
begin := time.Now()
err := c.Collect(ch)
duration := time.Since(begin)
var success int

if err != nil {
log.Errorf("ERROR: %s collector failed after %fs: %s", name, duration.Seconds(), err)
scrapeSuccess.WithLabelValues(name).Set(0)
success = 0
} else {
log.Debugf("OK: %s collector succeeded after %fs.", name, duration.Seconds())
scrapeSuccess.WithLabelValues(name).Set(1)
success = 1
}
scrapeDurations.WithLabelValues(name).Set(duration.Seconds())
ch <- prometheus.MustNewConstMetric(
scrapeDurationDesc,
prometheus.GaugeValue,
float64(duration.Seconds()),
name,
)
ch <- prometheus.MustNewConstMetric(
scrapeSuccessDesc,
prometheus.GaugeValue,
float64(success),
name,
)
}

func expandEnabledCollectors(enabled string) []string {
Expand Down

0 comments on commit 49c11ee

Please sign in to comment.