diff --git a/apache_exporter.go b/apache_exporter.go index f64720a79f63b7134ccca36c259607fd638eb032..4c1be4600fbe33de00addb18592a6b8027bcd1dd 100644 --- a/apache_exporter.go +++ b/apache_exporter.go @@ -30,11 +30,11 @@ type Exporter struct { mutex sync.Mutex client *http.Client - up prometheus.Gauge + up *prometheus.Desc scrapeFailures prometheus.Counter - accessesTotal prometheus.Counter - kBytesTotal prometheus.Counter - uptime prometheus.Counter + accessesTotal *prometheus.Desc + kBytesTotal *prometheus.Desc + uptime *prometheus.Desc workers *prometheus.GaugeVec scoreboard *prometheus.GaugeVec connections *prometheus.GaugeVec @@ -43,31 +43,31 @@ type Exporter struct { func NewExporter(uri string) *Exporter { return &Exporter{ URI: uri, - up: prometheus.NewGauge(prometheus.GaugeOpts{ - Namespace: namespace, - Name: "up", - Help: "Could the apache server be reached.", - }), - scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Name: "exporter_scrape_failures_total", - Help: "Number of errors while scraping apache.", - }), - accessesTotal: prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Name: "accesses_total", - Help: "Current total apache accesses", - }), - kBytesTotal: prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Name: "sent_kilobytes_total", - Help: "Current total kbytes sent", - }), - uptime: prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Name: "uptime_seconds_total", - Help: "Current uptime in seconds", - }), + up: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "up"), + "Could the apache server be reached", + nil, + nil), + scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: namespace, + Name: "exporter_scrape_failures_total", + Help: "Number of errors while scraping apache.", + }), + accessesTotal: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "accesses_total"), + "Current total apache accesses", + nil, + nil), + kBytesTotal: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "sent_kilobytes_total"), + "Current total kbytes sent", + nil, + nil), + uptime: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "uptime_seconds_total"), + "Current uptime in seconds", + nil, + nil), workers: prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, Name: "workers", @@ -98,14 +98,14 @@ func NewExporter(uri string) *Exporter { } func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { - e.up.Describe(ch) - e.scrapeFailures.Describe(ch) - e.accessesTotal.Describe(ch) - e.kBytesTotal.Describe(ch) - e.uptime.Describe(ch) - e.workers.Describe(ch) - e.scoreboard.Describe(ch) - e.connections.Describe(ch) + ch <- e.up + ch <- e.accessesTotal + ch <- e.kBytesTotal + ch <- e.uptime + e.scrapeFailures.Describe(ch) + e.workers.Describe(ch) + e.scoreboard.Describe(ch) + e.connections.Describe(ch) } // Split colon separated string into two fields @@ -158,12 +158,10 @@ func (e *Exporter) updateScoreboard(scoreboard string) { func (e *Exporter) collect(ch chan<- prometheus.Metric) error { resp, err := e.client.Get(e.URI) if err != nil { - e.up.Set(0) - e.up.Collect(ch) + ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 0) return fmt.Errorf("Error scraping apache: %v", err) } - e.up.Set(1) - e.up.Collect(ch) + ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 1) data, err := ioutil.ReadAll(resp.Body) resp.Body.Close() @@ -191,24 +189,21 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error { return err } - e.accessesTotal.Set(val) - e.accessesTotal.Collect(ch) + ch <- prometheus.MustNewConstMetric(e.accessesTotal, prometheus.CounterValue, val) case key == "Total kBytes": val, err := strconv.ParseFloat(v, 64) if err != nil { return err } - e.kBytesTotal.Set(val) - e.kBytesTotal.Collect(ch) + ch <- prometheus.MustNewConstMetric(e.kBytesTotal, prometheus.CounterValue, val) case key == "Uptime": val, err := strconv.ParseFloat(v, 64) if err != nil { return err } - e.uptime.Set(val) - e.uptime.Collect(ch) + ch <- prometheus.MustNewConstMetric(e.uptime, prometheus.CounterValue, val) case key == "BusyWorkers": val, err := strconv.ParseFloat(v, 64) if err != nil {