From 19eeb96610832c8f18eb6c69d8255684b628c4c2 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 17 Oct 2016 10:40:16 +0100 Subject: [PATCH] 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