apcupsd_exporter: report error when apcupsd cannot be reached

Signed-off-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
Matt Layher 2020-06-08 09:55:28 -04:00
parent a4a2b2f150
commit 43387c6d02
No known key found for this signature in database
GPG Key ID: 77BFE531397EDE94

View File

@ -4,6 +4,7 @@ package apcupsdexporter
import ( import (
"context" "context"
"fmt"
"log" "log"
"time" "time"
@ -11,6 +12,8 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
// TODO(mdlayher): rework this with metricslite.
const ( const (
// namespace is the top-level namespace for this apcupsd exporter. // namespace is the top-level namespace for this apcupsd exporter.
namespace = "apcupsd" namespace = "apcupsd"
@ -44,7 +47,8 @@ func New(fn ClientFunc) *Exporter {
// Describe sends all the descriptors of the collectors included to // Describe sends all the descriptors of the collectors included to
// the provided channel. // the provided channel.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.withCollectors(func(cs []prometheus.Collector) { // Don't care, we'll get this error on Collect.
_ = e.withCollectors(func(cs []prometheus.Collector) {
for _, c := range cs { for _, c := range cs {
c.Describe(ch) c.Describe(ch)
} }
@ -54,24 +58,30 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// Collect sends the collected metrics from each of the collectors to // Collect sends the collected metrics from each of the collectors to
// prometheus. // prometheus.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) { func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.withCollectors(func(cs []prometheus.Collector) { err := e.withCollectors(func(cs []prometheus.Collector) {
for _, c := range cs { for _, c := range cs {
c.Collect(ch) c.Collect(ch)
} }
}) })
// This is a hack but it allows us to report failure to dial without
// reworking significant portions of the code.
if err != nil {
log.Println(err)
ch <- prometheus.NewInvalidMetric(NewUPSCollector(nil).Info, err)
return
}
} }
// withCollectors sets up an apcupsd client and creates a set of prometheus // withCollectors sets up an apcupsd client and creates a set of prometheus
// collectors. It invokes the input closure and then cleans up after the // collectors. It invokes the input closure and then cleans up after the
// closure returns. // closure returns.
func (e *Exporter) withCollectors(fn func(cs []prometheus.Collector)) { func (e *Exporter) withCollectors(fn func(cs []prometheus.Collector)) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
c, err := e.clientFn(ctx) c, err := e.clientFn(ctx)
if err != nil { if err != nil {
log.Printf("error creating apcupsd client: %v", err) return fmt.Errorf("error creating apcupsd client: %v", err)
return
} }
defer c.Close() defer c.Close()
@ -80,4 +90,6 @@ func (e *Exporter) withCollectors(fn func(cs []prometheus.Collector)) {
} }
fn(cs) fn(cs)
return nil
} }