From 92cc5318dfc021dc6234f70f3f7a366f01ff339a Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 27 Jun 2024 14:54:47 +0200 Subject: [PATCH] chore: refactor metrics and fetching slightly --- collector/checks.go | 47 ++++++++++++-------------------- collector/metrics.go | 65 +++++++++++++++++++------------------------- updown/client.go | 9 +++--- updown/metrics.go | 12 ++++---- 4 files changed, 57 insertions(+), 76 deletions(-) diff --git a/collector/checks.go b/collector/checks.go index bb60248..6ed4b4d 100644 --- a/collector/checks.go +++ b/collector/checks.go @@ -1,8 +1,7 @@ package collector import ( - "strconv" - "sync" + "cmp" "github.com/go-logr/logr" "github.com/prometheus/client_golang/prometheus" @@ -26,13 +25,11 @@ func NewChecksCollector(s System, client *updown.Client, log logr.Logger) *Check Client: client, Log: log, Enabled: prometheus.NewDesc( - prometheus.BuildFQName(s.Namespace, subsystem, "enabled"), - "status of check (enabled=1)", + prometheus.BuildFQName(s.Namespace, subsystem, "up"), + "status of check", []string{ - "token", "url", - "status", - "ssl_valid", + "alias", }, nil, ), @@ -49,30 +46,22 @@ func (c *ChecksCollector) Collect(ch chan<- prometheus.Metric) { return } - var wg sync.WaitGroup for _, check := range checks { - wg.Add(1) - go func(check updown.Check) { - defer wg.Done() - ch <- prometheus.MustNewConstMetric( - c.Enabled, - prometheus.CounterValue, - func(enabled bool) (result float64) { - if enabled { - result = 1.0 - } - return result - }(check.Enabled), - []string{ - check.Token, - check.URL, - strconv.FormatUint(uint64(check.LastStatus), 10), - strconv.FormatBool(check.SSL.Valid), - }..., - ) - }(check) + ch <- prometheus.MustNewConstMetric( + c.Enabled, + prometheus.CounterValue, + boolFloat(!check.Down), + check.URL, + cmp.Or(check.Alias, check.URL), + ) } - wg.Wait() +} + +func boolFloat(enabled bool) float64 { + if enabled { + return 1.0 + } + return 0.0 } // Describe implements Prometheus' Collector interface is used to describe metrics diff --git a/collector/metrics.go b/collector/metrics.go index ffb0c7c..28c2c9f 100644 --- a/collector/metrics.go +++ b/collector/metrics.go @@ -1,7 +1,7 @@ package collector import ( - "strconv" + "cmp" "sync" "github.com/go-logr/logr" @@ -26,12 +26,11 @@ func NewMetricsCollector(s System, client *updown.Client, log logr.Logger) *Metr Client: client, Log: log, ResponseTime: prometheus.NewDesc( - prometheus.BuildFQName(s.Namespace, subsystem, "response_times"), - "check metrics response times (ms)", + prometheus.BuildFQName(s.Namespace, subsystem, "response_time_seconds"), + "check metrics response times (seconds)", []string{ - "token", "url", - "status", + "alias", }, nil, ), @@ -51,41 +50,33 @@ func (c *MetricsCollector) Collect(ch chan<- prometheus.Metric) { var wg sync.WaitGroup for _, check := range checks { - wg.Add(1) - go func(check updown.Check) { - defer wg.Done() + log := log.WithValues("URL", check.URL) - log := log.WithValues("URL", check.URL) + if check.Token == "" { + log.Info("unable to obtain token for Check") + return + } - if check.Token == "" { - log.Info("unable to obtain token for Check") - return - } + metrics, err := c.Client.GetCheckMetrics(check.Token) + if err != nil { + log.Error(err, "unable to read metrics for Check") + return + } - metrics, err := c.Client.GetCheckMetrics(check.Token) - if err != nil { - log.Info("unable to read metrics for Check") - return - } - - respTime := metrics.Requests.ByResponseTime - ch <- prometheus.MustNewConstHistogram( - c.ResponseTime, - // updown doesn't provide values for above 4s (i.e. Infinity) - // website only permits maxium value of 2s so I assume 4s is intended to represent "all else" - // Assuming that Under4000 is effectively infinity and using it as the value for count - uint64(respTime.Under4000), - // updown doesn't provide a value for the sum of values - 0.0, - // Convert the struct into a map of buckets - respTime.ToBuckets(), - []string{ - check.Token, - check.URL, - strconv.FormatUint(uint64(check.LastStatus), 10), - }..., - ) - }(check) + respTime := metrics.Requests.ByResponseTime + ch <- prometheus.MustNewConstHistogram( + c.ResponseTime, + // updown doesn't provide values for above 4s (i.e. Infinity) + // website only permits maxium value of 2s so I assume 4s is intended to represent "all else" + // Assuming that Under4000 is effectively infinity and using it as the value for count + uint64(respTime.Under4000), + // updown doesn't provide a value for the sum of values + 0.0, + // Convert the struct into a map of buckets + respTime.ToBuckets(), + check.URL, + cmp.Or(check.Alias, check.URL), + ) } wg.Wait() } diff --git a/updown/client.go b/updown/client.go index 7e4b16c..382e8b0 100644 --- a/updown/client.go +++ b/updown/client.go @@ -3,7 +3,7 @@ package updown import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "github.com/go-logr/logr" @@ -51,7 +51,7 @@ func (c *Client) GetChecks() ([]Check, error) { return []Check{}, err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Info("Unable to read response body") return []Check{}, err @@ -101,7 +101,7 @@ func (c *Client) GetCheckMetrics(token string) (Metrics, error) { return Metrics{}, err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Info("Unable to read response body") return Metrics{}, err @@ -113,7 +113,8 @@ func (c *Client) GetCheckMetrics(token string) (Metrics, error) { metrics := Metrics{} if err := json.Unmarshal(body, &metrics); err != nil { - return Metrics{}, err + fmt.Println(string(body)) + return Metrics{}, fmt.Errorf("unmarshal: %w", err) } // log.Info("Result", diff --git a/updown/metrics.go b/updown/metrics.go index 46add7c..2ed9be5 100644 --- a/updown/metrics.go +++ b/updown/metrics.go @@ -29,12 +29,12 @@ type ByResponseTime struct { // ToBuckets is a method that converts ByResponseTIme structs into Prometheus buckets func (x *ByResponseTime) ToBuckets() map[float64]uint64 { return map[float64]uint64{ - 125.0: uint64(x.Under125), - 250.0: uint64(x.Under250), - 500.0: uint64(x.Under500), - 1000.0: uint64(x.Under1000), - 2000.0: uint64(x.Under2000), - 4000.0: uint64(x.Under4000), + 0.125: uint64(x.Under125), + 0.250: uint64(x.Under250), + 0.500: uint64(x.Under500), + 1.000: uint64(x.Under1000), + 2.000: uint64(x.Under2000), + 4.000: uint64(x.Under4000), } }