From 219da01e081ae4040f301a45666c9560cf0e6ef2 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 14 Oct 2016 17:42:47 +0100 Subject: [PATCH 1/7] Add extremely noddy counter metrics for !commands Missing the command type currently. Also, not sure how "nice" this is: we could probably blob the 3 functions into 1 and use an enum but, meh? --- src/github.com/matrix-org/go-neb/goneb.go | 1 + .../matrix-org/go-neb/metrics/metrics.go | 41 +++++++++++++++++++ .../matrix-org/go-neb/plugin/plugin.go | 5 +++ 3 files changed, 47 insertions(+) create mode 100644 src/github.com/matrix-org/go-neb/metrics/metrics.go diff --git a/src/github.com/matrix-org/go-neb/goneb.go b/src/github.com/matrix-org/go-neb/goneb.go index 1ca087f..d2c6f11 100644 --- a/src/github.com/matrix-org/go-neb/goneb.go +++ b/src/github.com/matrix-org/go-neb/goneb.go @@ -5,6 +5,7 @@ import ( "github.com/matrix-org/dugong" "github.com/matrix-org/go-neb/clients" "github.com/matrix-org/go-neb/database" + _ "github.com/matrix-org/go-neb/metrics" "github.com/matrix-org/go-neb/polling" _ "github.com/matrix-org/go-neb/realms/github" _ "github.com/matrix-org/go-neb/realms/jira" diff --git a/src/github.com/matrix-org/go-neb/metrics/metrics.go b/src/github.com/matrix-org/go-neb/metrics/metrics.go new file mode 100644 index 0000000..d7e30c0 --- /dev/null +++ b/src/github.com/matrix-org/go-neb/metrics/metrics.go @@ -0,0 +1,41 @@ +package metrics + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +var ( + numIncomingCmds = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "num_incoming_commands_total", + Help: "The number of incoming commands from matrix clients", + }) + numSuccessCmds = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "num_success_commands_total", + Help: "The number of incoming commands from matrix clients which were successful", + }) + numErrorCmds = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "num_error_commands_total", + Help: "The number of incoming commands from matrix clients which failed", + }) +) + +// IncIncomingCommand increments the incoming command counter (TODO: cmd type) +func IncIncomingCommand() { + numIncomingCmds.Inc() +} + +// IncSuccessCommand increments the success command counter (TODO: cmd type) +func IncSuccessCommand() { + numSuccessCmds.Inc() +} + +// IncErrorCommand increments the error command counter (TODO: cmd type) +func IncErrorCommand() { + numErrorCmds.Inc() +} + +func init() { + prometheus.MustRegister(numIncomingCmds) + prometheus.MustRegister(numSuccessCmds) + prometheus.MustRegister(numErrorCmds) +} diff --git a/src/github.com/matrix-org/go-neb/plugin/plugin.go b/src/github.com/matrix-org/go-neb/plugin/plugin.go index 05ee1c1..02b0467 100644 --- a/src/github.com/matrix-org/go-neb/plugin/plugin.go +++ b/src/github.com/matrix-org/go-neb/plugin/plugin.go @@ -3,6 +3,7 @@ package plugin import ( log "github.com/Sirupsen/logrus" "github.com/matrix-org/go-neb/matrix" + "github.com/matrix-org/go-neb/metrics" "github.com/mattn/go-shellwords" "regexp" "strings" @@ -71,6 +72,7 @@ func runCommandForPlugin(plugin Plugin, event *matrix.Event, arguments []string) "user_id": event.Sender, "command": bestMatch.Path, }).Info("Executing command") + metrics.IncIncomingCommand() content, err := bestMatch.Command(event.RoomID, event.Sender, cmdArgs) if err != nil { if content != nil { @@ -82,7 +84,10 @@ func runCommandForPlugin(plugin Plugin, event *matrix.Event, arguments []string) "args": cmdArgs, }).Warn("Command returned both error and content.") } + metrics.IncErrorCommand() content = matrix.TextMessage{"m.notice", err.Error()} + } else { + metrics.IncSuccessCommand() } return content From 19eeb96610832c8f18eb6c69d8255684b628c4c2 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2016 10:40:16 +0100 Subject: [PATCH 2/7] Use enums instead --- .../matrix-org/go-neb/metrics/metrics.go | 31 +++++++++++-------- .../matrix-org/go-neb/plugin/plugin.go | 6 ++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/metrics/metrics.go b/src/github.com/matrix-org/go-neb/metrics/metrics.go index d7e30c0..178a238 100644 --- a/src/github.com/matrix-org/go-neb/metrics/metrics.go +++ b/src/github.com/matrix-org/go-neb/metrics/metrics.go @@ -4,6 +4,14 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +type CommandStatus int + +const ( + Pending CommandStatus = iota + Success + Failure +) + var ( numIncomingCmds = prometheus.NewCounter(prometheus.CounterOpts{ Name: "num_incoming_commands_total", @@ -19,19 +27,16 @@ var ( }) ) -// IncIncomingCommand increments the incoming command counter (TODO: cmd type) -func IncIncomingCommand() { - numIncomingCmds.Inc() -} - -// IncSuccessCommand increments the success command counter (TODO: cmd type) -func IncSuccessCommand() { - numSuccessCmds.Inc() -} - -// IncErrorCommand increments the error command counter (TODO: cmd type) -func IncErrorCommand() { - numErrorCmds.Inc() +// IncrementCommand increments the incoming command counter (TODO: cmd type) +func IncrementCommand(st CommandStatus) { + switch st { + case Pending: + numIncomingCmds.Inc() + case Success: + numSuccessCmds.Inc() + case Failure: + numErrorCmds.Inc() + } } func init() { diff --git a/src/github.com/matrix-org/go-neb/plugin/plugin.go b/src/github.com/matrix-org/go-neb/plugin/plugin.go index 02b0467..a55afaf 100644 --- a/src/github.com/matrix-org/go-neb/plugin/plugin.go +++ b/src/github.com/matrix-org/go-neb/plugin/plugin.go @@ -72,7 +72,7 @@ func runCommandForPlugin(plugin Plugin, event *matrix.Event, arguments []string) "user_id": event.Sender, "command": bestMatch.Path, }).Info("Executing command") - metrics.IncIncomingCommand() + metrics.IncrementCommand(metrics.Pending) content, err := bestMatch.Command(event.RoomID, event.Sender, cmdArgs) if err != nil { if content != nil { @@ -84,10 +84,10 @@ func runCommandForPlugin(plugin Plugin, event *matrix.Event, arguments []string) "args": cmdArgs, }).Warn("Command returned both error and content.") } - metrics.IncErrorCommand() + metrics.IncrementCommand(metrics.Failure) content = matrix.TextMessage{"m.notice", err.Error()} } else { - metrics.IncSuccessCommand() + metrics.IncrementCommand(metrics.Success) } return content From 32e8a5b6ccce8bfff995171eb2e8ffbac862fb66 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2016 10:43:13 +0100 Subject: [PATCH 3/7] Docs --- src/github.com/matrix-org/go-neb/metrics/metrics.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/github.com/matrix-org/go-neb/metrics/metrics.go b/src/github.com/matrix-org/go-neb/metrics/metrics.go index 178a238..0711635 100644 --- a/src/github.com/matrix-org/go-neb/metrics/metrics.go +++ b/src/github.com/matrix-org/go-neb/metrics/metrics.go @@ -4,8 +4,10 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +// CommandStatus is the status of a incoming command type CommandStatus int +// The command status values const ( Pending CommandStatus = iota Success From cdb49fa60b12a07c6e68f7d9f2fb0ad8f3230dc2 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2016 11:11:43 +0100 Subject: [PATCH 4/7] Add command type as a label --- .../matrix-org/go-neb/metrics/metrics.go | 34 +++++++++---------- .../matrix-org/go-neb/plugin/plugin.go | 6 ++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/metrics/metrics.go b/src/github.com/matrix-org/go-neb/metrics/metrics.go index 0711635..f9f80c4 100644 --- a/src/github.com/matrix-org/go-neb/metrics/metrics.go +++ b/src/github.com/matrix-org/go-neb/metrics/metrics.go @@ -9,35 +9,35 @@ type CommandStatus int // The command status values const ( - Pending CommandStatus = iota - Success - Failure + StatusPending CommandStatus = iota + StatusSuccess + StatusFailure ) var ( - numIncomingCmds = prometheus.NewCounter(prometheus.CounterOpts{ + numIncomingCmds = prometheus.NewCounterVec(prometheus.CounterOpts{ Name: "num_incoming_commands_total", Help: "The number of incoming commands from matrix clients", - }) - numSuccessCmds = prometheus.NewCounter(prometheus.CounterOpts{ + }, []string{"cmd"}) + numSuccessCmds = prometheus.NewCounterVec(prometheus.CounterOpts{ Name: "num_success_commands_total", Help: "The number of incoming commands from matrix clients which were successful", - }) - numErrorCmds = prometheus.NewCounter(prometheus.CounterOpts{ + }, []string{"cmd"}) + numErrorCmds = prometheus.NewCounterVec(prometheus.CounterOpts{ Name: "num_error_commands_total", Help: "The number of incoming commands from matrix clients which failed", - }) + }, []string{"cmd"}) ) -// IncrementCommand increments the incoming command counter (TODO: cmd type) -func IncrementCommand(st CommandStatus) { +// IncrementCommand increments the incoming command counter +func IncrementCommand(cmdName string, st CommandStatus) { switch st { - case Pending: - numIncomingCmds.Inc() - case Success: - numSuccessCmds.Inc() - case Failure: - numErrorCmds.Inc() + case StatusPending: + numIncomingCmds.With(prometheus.Labels{"cmd": cmdName}).Inc() + case StatusSuccess: + numSuccessCmds.With(prometheus.Labels{"cmd": cmdName}).Inc() + case StatusFailure: + numErrorCmds.With(prometheus.Labels{"cmd": cmdName}).Inc() } } diff --git a/src/github.com/matrix-org/go-neb/plugin/plugin.go b/src/github.com/matrix-org/go-neb/plugin/plugin.go index a55afaf..0f170c0 100644 --- a/src/github.com/matrix-org/go-neb/plugin/plugin.go +++ b/src/github.com/matrix-org/go-neb/plugin/plugin.go @@ -72,7 +72,7 @@ func runCommandForPlugin(plugin Plugin, event *matrix.Event, arguments []string) "user_id": event.Sender, "command": bestMatch.Path, }).Info("Executing command") - metrics.IncrementCommand(metrics.Pending) + metrics.IncrementCommand(bestMatch.Path[0], metrics.StatusPending) content, err := bestMatch.Command(event.RoomID, event.Sender, cmdArgs) if err != nil { if content != nil { @@ -84,10 +84,10 @@ func runCommandForPlugin(plugin Plugin, event *matrix.Event, arguments []string) "args": cmdArgs, }).Warn("Command returned both error and content.") } - metrics.IncrementCommand(metrics.Failure) + metrics.IncrementCommand(bestMatch.Path[0], metrics.StatusFailure) content = matrix.TextMessage{"m.notice", err.Error()} } else { - metrics.IncrementCommand(metrics.Success) + metrics.IncrementCommand(bestMatch.Path[0], metrics.StatusSuccess) } return content From b4b0a661c4955a0152b333b8ad162bb82a359788 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2016 14:18:21 +0100 Subject: [PATCH 5/7] Add more metrics --- src/github.com/matrix-org/go-neb/api.go | 9 ++- .../matrix-org/go-neb/metrics/metrics.go | 62 ++++++++++--------- .../matrix-org/go-neb/plugin/plugin.go | 1 - .../matrix-org/go-neb/services/guggy/guggy.go | 19 +++++- .../go-neb/services/rssbot/rssbot.go | 16 +++++ 5 files changed, 70 insertions(+), 37 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index 4a7881d..09ec2c2 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -8,6 +8,7 @@ import ( "github.com/matrix-org/go-neb/clients" "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/errors" + "github.com/matrix-org/go-neb/metrics" "github.com/matrix-org/go-neb/polling" "github.com/matrix-org/go-neb/types" "net/http" @@ -180,7 +181,6 @@ func (wh *webhookHandler) handle(w http.ResponseWriter, req *http.Request) { // but we've base64d it. base64srvID := segments[len(segments)-1] bytesSrvID, err := base64.RawURLEncoding.DecodeString(base64srvID) - srvID := string(bytesSrvID) if err != nil { log.WithError(err).WithField("base64_service_id", base64srvID).Print( "Not a b64 encoded string", @@ -188,6 +188,7 @@ func (wh *webhookHandler) handle(w http.ResponseWriter, req *http.Request) { w.WriteHeader(400) return } + srvID := string(bytesSrvID) service, err := wh.db.LoadService(srvID) if err != nil { @@ -203,9 +204,10 @@ func (wh *webhookHandler) handle(w http.ResponseWriter, req *http.Request) { return } log.WithFields(log.Fields{ - "service_id": service.ServiceID(), - "service_typ": service.ServiceType(), + "service_id": service.ServiceID(), + "service_type": service.ServiceType(), }).Print("Incoming webhook for service") + metrics.IncrementWebhook(service.ServiceType()) service.OnReceiveWebhook(w, req, cli) } @@ -319,6 +321,7 @@ func (s *configureServiceHandler) OnIncomingRequest(req *http.Request) (interfac } service.PostRegister(old) + metrics.IncrementConfigureService(service.ServiceType()) return &struct { ID string diff --git a/src/github.com/matrix-org/go-neb/metrics/metrics.go b/src/github.com/matrix-org/go-neb/metrics/metrics.go index f9f80c4..3023210 100644 --- a/src/github.com/matrix-org/go-neb/metrics/metrics.go +++ b/src/github.com/matrix-org/go-neb/metrics/metrics.go @@ -4,45 +4,47 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -// CommandStatus is the status of a incoming command -type CommandStatus int +// Status is the status of a measurable metric (incoming commands, outgoing polls, etc) +type Status string -// The command status values +// Common status values const ( - StatusPending CommandStatus = iota - StatusSuccess - StatusFailure + StatusSuccess = "success" + StatusFailure = "failure" ) var ( - numIncomingCmds = prometheus.NewCounterVec(prometheus.CounterOpts{ - Name: "num_incoming_commands_total", + cmdCounter = prometheus.NewCounterVec(prometheus.CounterOpts{ + Name: "goneb_pling_cmd_total", Help: "The number of incoming commands from matrix clients", - }, []string{"cmd"}) - numSuccessCmds = prometheus.NewCounterVec(prometheus.CounterOpts{ - Name: "num_success_commands_total", - Help: "The number of incoming commands from matrix clients which were successful", - }, []string{"cmd"}) - numErrorCmds = prometheus.NewCounterVec(prometheus.CounterOpts{ - Name: "num_error_commands_total", - Help: "The number of incoming commands from matrix clients which failed", - }, []string{"cmd"}) + }, []string{"cmd", "status"}) + configureServicesCounter = prometheus.NewCounterVec(prometheus.CounterOpts{ + Name: "goneb_configure_services_total", + Help: "The total number of configured services requests", + }, []string{"service_type"}) + webhookCounter = prometheus.NewCounterVec(prometheus.CounterOpts{ + Name: "goneb_webhook_total", + Help: "The total number of recognised incoming webhook requests", + }, []string{"service_type"}) ) -// IncrementCommand increments the incoming command counter -func IncrementCommand(cmdName string, st CommandStatus) { - switch st { - case StatusPending: - numIncomingCmds.With(prometheus.Labels{"cmd": cmdName}).Inc() - case StatusSuccess: - numSuccessCmds.With(prometheus.Labels{"cmd": cmdName}).Inc() - case StatusFailure: - numErrorCmds.With(prometheus.Labels{"cmd": cmdName}).Inc() - } +// IncrementCommand increments the pling command counter +func IncrementCommand(cmdName string, st Status) { + cmdCounter.With(prometheus.Labels{"cmd": cmdName, "status": string(st)}).Inc() +} + +// IncrementConfigureService increments the /configureService counter +func IncrementConfigureService(serviceType string) { + configureServicesCounter.With(prometheus.Labels{"service_type": serviceType}).Inc() +} + +// IncrementWebhook increments the incoming webhook request counter +func IncrementWebhook(serviceType string) { + webhookCounter.With(prometheus.Labels{"service_type": serviceType}).Inc() } func init() { - prometheus.MustRegister(numIncomingCmds) - prometheus.MustRegister(numSuccessCmds) - prometheus.MustRegister(numErrorCmds) + prometheus.MustRegister(cmdCounter) + prometheus.MustRegister(configureServicesCounter) + prometheus.MustRegister(webhookCounter) } diff --git a/src/github.com/matrix-org/go-neb/plugin/plugin.go b/src/github.com/matrix-org/go-neb/plugin/plugin.go index 0f170c0..0007bec 100644 --- a/src/github.com/matrix-org/go-neb/plugin/plugin.go +++ b/src/github.com/matrix-org/go-neb/plugin/plugin.go @@ -72,7 +72,6 @@ func runCommandForPlugin(plugin Plugin, event *matrix.Event, arguments []string) "user_id": event.Sender, "command": bestMatch.Path, }).Info("Executing command") - metrics.IncrementCommand(bestMatch.Path[0], metrics.StatusPending) content, err := bestMatch.Command(event.RoomID, event.Sender, cmdArgs) if err != nil { if content != nil { diff --git a/src/github.com/matrix-org/go-neb/services/guggy/guggy.go b/src/github.com/matrix-org/go-neb/services/guggy/guggy.go index 5045694..4d6a2d3 100644 --- a/src/github.com/matrix-org/go-neb/services/guggy/guggy.go +++ b/src/github.com/matrix-org/go-neb/services/guggy/guggy.go @@ -3,10 +3,12 @@ package services import ( "bytes" "encoding/json" + "fmt" log "github.com/Sirupsen/logrus" "github.com/matrix-org/go-neb/matrix" "github.com/matrix-org/go-neb/plugin" "github.com/matrix-org/go-neb/types" + "io/ioutil" "math" "net/http" "strings" @@ -54,7 +56,7 @@ func (s *guggyService) cmdGuggy(client *matrix.Client, roomID, userID string, ar querySentence := strings.Join(args, " ") gifResult, err := s.text2gifGuggy(querySentence) if err != nil { - return nil, err + return nil, fmt.Errorf("Failed to query Guggy: %s", err.Error()) } if gifResult.GIF == "" { @@ -66,7 +68,7 @@ func (s *guggyService) cmdGuggy(client *matrix.Client, roomID, userID string, ar mxc, err := client.UploadLink(gifResult.GIF) if err != nil { - return nil, err + return nil, fmt.Errorf("Failed to upload Guggy image to matrix: %s", err.Error()) } return matrix.ImageMessage{ @@ -114,9 +116,20 @@ func (s *guggyService) text2gifGuggy(querySentence string) (*guggyGifResult, err log.Error(err) return nil, err } + if res.StatusCode < 200 || res.StatusCode >= 300 { + resBytes, err := ioutil.ReadAll(res.Body) + if err != nil { + log.WithError(err).Error("Failed to decode Guggy response body") + } + log.WithFields(log.Fields{ + "code": res.StatusCode, + "body": string(resBytes), + }).Error("Failed to query Guggy") + return nil, fmt.Errorf("Failed to decode response (HTTP %d)", res.StatusCode) + } var result guggyGifResult if err := json.NewDecoder(res.Body).Decode(&result); err != nil { - return nil, err + return nil, fmt.Errorf("Failed to decode response (HTTP %d): %s", res.StatusCode, err.Error()) } return &result, nil 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 226d83a..3fd8069 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 @@ -11,6 +11,7 @@ import ( "github.com/matrix-org/go-neb/polling" "github.com/matrix-org/go-neb/types" "github.com/mmcdole/gofeed" + "github.com/prometheus/client_golang/prometheus" "html" "net/http" "time" @@ -18,6 +19,13 @@ import ( var cachingClient *http.Client +var ( + pollCounter = prometheus.NewCounterVec(prometheus.CounterOpts{ + Name: "goneb_rss_polls_total", + Help: "The number of feed polls from RSS services", + }, []string{"url", "http_status"}) +) + const minPollingIntervalSeconds = 60 * 5 // 5 min (News feeds can be genuinely spammy) type rssBotService struct { @@ -127,8 +135,15 @@ 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() continue } + pollCounter.With(prometheus.Labels{"url": u, "http_status": "200"}).Inc() // technically 2xx but gofeed doesn't tell us which // 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] @@ -285,4 +300,5 @@ func init() { } return r }) + prometheus.MustRegister(pollCounter) } From 2e5c8e4d79b240d232f8639fc40b4e7f8be1d0ff Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2016 14:38:24 +0100 Subject: [PATCH 6/7] 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 From f493a1933ef000423f5c576bcf045a176faaeca3 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2016 15:00:06 +0100 Subject: [PATCH 7/7] s/sendMetric/incrementMetrics/ --- src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go | 6 +++--- 1 file changed, 3 insertions(+), 3 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 cdcedb7..58b3ca4 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 @@ -136,10 +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") - sendMetric(u, err) + incrementMetrics(u, err) continue } - sendMetric(u, nil) + incrementMetrics(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] @@ -161,7 +161,7 @@ func (s *rssBotService) OnPoll(cli *matrix.Client) time.Time { return s.nextTimestamp() } -func sendMetric(urlStr string, err error) { +func incrementMetrics(urlStr string, err error) { // extract domain part of RSS feed URL to get coarser (more useful) statistics domain := urlStr u, urlErr := url.Parse(urlStr)