From 2e5c8e4d79b240d232f8639fc40b4e7f8be1d0ff Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2016 14:38:24 +0100 Subject: [PATCH] Only send the domain part of the RSS feed as that's more useful when doing aggregate metrics --- .../go-neb/services/rssbot/rssbot.go | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go index 3fd8069..cdcedb7 100644 --- a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go +++ b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go @@ -14,6 +14,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "html" "net/http" + "net/url" "time" ) @@ -135,15 +136,10 @@ func (s *rssBotService) OnPoll(cli *matrix.Client) time.Time { feed, items, err := s.queryFeed(u) if err != nil { logger.WithField("feed_url", u).WithError(err).Error("Failed to query feed") - herr, ok := err.(gofeed.HTTPError) - statusCode := 0 // e.g. network timeout - if ok { - statusCode = herr.StatusCode - } - pollCounter.With(prometheus.Labels{"url": u, "http_status": string(statusCode)}).Inc() + sendMetric(u, err) continue } - pollCounter.With(prometheus.Labels{"url": u, "http_status": "200"}).Inc() // technically 2xx but gofeed doesn't tell us which + sendMetric(u, nil) // Loop backwards since [0] is the most recent and we want to send in chronological order for i := len(items) - 1; i >= 0; i-- { item := items[i] @@ -165,6 +161,25 @@ func (s *rssBotService) OnPoll(cli *matrix.Client) time.Time { return s.nextTimestamp() } +func sendMetric(urlStr string, err error) { + // extract domain part of RSS feed URL to get coarser (more useful) statistics + domain := urlStr + u, urlErr := url.Parse(urlStr) + if urlErr == nil { + domain = u.Host + } + if err != nil { + herr, ok := err.(gofeed.HTTPError) + statusCode := 0 // e.g. network timeout + if ok { + statusCode = herr.StatusCode + } + pollCounter.With(prometheus.Labels{"url": domain, "http_status": string(statusCode)}).Inc() + } else { + pollCounter.With(prometheus.Labels{"url": domain, "http_status": "200"}).Inc() // technically 2xx but gofeed doesn't tell us which + } +} + func (s *rssBotService) nextTimestamp() time.Time { // return the earliest next poll ts var earliestNextTs int64