From 219da01e081ae4040f301a45666c9560cf0e6ef2 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 14 Oct 2016 17:42:47 +0100 Subject: [PATCH] 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