diff --git a/core/poller/poller/influx.go b/core/poller/poller/influx.go index 1d0af593..d3173c4c 100644 --- a/core/poller/poller/influx.go +++ b/core/poller/poller/influx.go @@ -8,6 +8,22 @@ import ( client "github.com/influxdata/influxdb1-client/v2" ) +// CollectAndProcess collects measurements and then reports them to InfluxDB +// Can be called once or in a ticker loop. This function and all the ones below +// handle their own logging. An error is returned so the calling function may +// determine if there was a read or write error and act on it. This is currently +// called in two places in this library. One returns an error, one does not. +func (u *UnifiPoller) CollectAndProcess() error { + metrics, err := u.CollectMetrics() + if err != nil { + return err + } + u.AugmentMetrics(metrics) + err = u.ReportMetrics(metrics) + u.LogError(err, "processing metrics") + return err +} + // ReportMetrics batches all the metrics and writes them to InfluxDB. // This creates an InfluxDB writer, and returns an error if the write fails. func (u *UnifiPoller) ReportMetrics(metrics *metrics.Metrics) error { diff --git a/core/poller/poller/start.go b/core/poller/poller/start.go index 4ad0a380..13f02621 100644 --- a/core/poller/poller/start.go +++ b/core/poller/poller/start.go @@ -95,7 +95,7 @@ func (u *UnifiPoller) Run() (err error) { u.Logf("Logging Measurements to InfluxDB at %s as user %s one time (lambda mode)", u.Config.InfluxURL, u.Config.InfluxUser) u.LastCheck = time.Now() - return u.CollectAndProcess(u.ReportMetrics) + return u.CollectAndProcess() case "prometheus", "exporter": u.Logf("Exporting Measurements at https://%s/metrics for Prometheus", u.Config.HTTPListen) @@ -115,7 +115,7 @@ func (u *UnifiPoller) Run() (err error) { u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.Config.InfluxURL, u.Config.InfluxUser) u.Config.Mode = "influx poller" - return u.PollController(u.ReportMetrics) + return u.PollController() } } diff --git a/core/poller/poller/unifi.go b/core/poller/poller/unifi.go index dd768022..26dc4bf2 100644 --- a/core/poller/poller/unifi.go +++ b/core/poller/poller/unifi.go @@ -13,7 +13,7 @@ import ( // PollController runs forever, polling UniFi // and pushing to influx OR exporting for prometheus. // This is started by Run() after everything checks out. -func (u *UnifiPoller) PollController(process func(*metrics.Metrics) error) error { +func (u *UnifiPoller) PollController() error { interval := u.Config.Interval.Round(time.Second) log.Printf("[INFO] Everything checks out! Poller started in %v mode, interval: %v", u.Config.Mode, interval) ticker := time.NewTicker(interval) @@ -28,7 +28,7 @@ func (u *UnifiPoller) PollController(process func(*metrics.Metrics) error) error } if err == nil { // Only run this if the authentication procedure didn't return error. - _ = u.CollectAndProcess(process) + _ = u.CollectAndProcess() } if u.errorCount > 0 { return fmt.Errorf("too many errors, stopping poller") @@ -70,23 +70,6 @@ FIRST: return nil } -// CollectAndProcess collects measurements and then passese them into the -// provided method. The method is either an http exporter or an influxdb update. -// Can be called once or in a ticker loop. This function and all the ones below -// handle their own logging. An error is returned so the calling function may -// determine if there was a read or write error and act on it. This is currently -// called in two places in this library. One returns an error, one does not. -func (u *UnifiPoller) CollectAndProcess(process func(*metrics.Metrics) error) error { - metrics, err := u.CollectMetrics() - if err != nil { - return err - } - u.AugmentMetrics(metrics) - err = process(metrics) - u.LogError(err, "processing metrics") - return err -} - // CollectMetrics grabs all the measurements from a UniFi controller and returns them. func (u *UnifiPoller) CollectMetrics() (*metrics.Metrics, error) { m := &metrics.Metrics{TS: u.LastCheck} // At this point, it's the Current Check.