Browse Source

Use enums instead

pull/91/head
Kegan Dougal 8 years ago
parent
commit
19eeb96610
  1. 25
      src/github.com/matrix-org/go-neb/metrics/metrics.go
  2. 6
      src/github.com/matrix-org/go-neb/plugin/plugin.go

25
src/github.com/matrix-org/go-neb/metrics/metrics.go

@ -4,6 +4,14 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
type CommandStatus int
const (
Pending CommandStatus = iota
Success
Failure
)
var ( var (
numIncomingCmds = prometheus.NewCounter(prometheus.CounterOpts{ numIncomingCmds = prometheus.NewCounter(prometheus.CounterOpts{
Name: "num_incoming_commands_total", Name: "num_incoming_commands_total",
@ -19,20 +27,17 @@ var (
}) })
) )
// IncIncomingCommand increments the incoming command counter (TODO: cmd type)
func IncIncomingCommand() {
// IncrementCommand increments the incoming command counter (TODO: cmd type)
func IncrementCommand(st CommandStatus) {
switch st {
case Pending:
numIncomingCmds.Inc() numIncomingCmds.Inc()
}
// IncSuccessCommand increments the success command counter (TODO: cmd type)
func IncSuccessCommand() {
case Success:
numSuccessCmds.Inc() numSuccessCmds.Inc()
}
// IncErrorCommand increments the error command counter (TODO: cmd type)
func IncErrorCommand() {
case Failure:
numErrorCmds.Inc() numErrorCmds.Inc()
} }
}
func init() { func init() {
prometheus.MustRegister(numIncomingCmds) prometheus.MustRegister(numIncomingCmds)

6
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, "user_id": event.Sender,
"command": bestMatch.Path, "command": bestMatch.Path,
}).Info("Executing command") }).Info("Executing command")
metrics.IncIncomingCommand()
metrics.IncrementCommand(metrics.Pending)
content, err := bestMatch.Command(event.RoomID, event.Sender, cmdArgs) content, err := bestMatch.Command(event.RoomID, event.Sender, cmdArgs)
if err != nil { if err != nil {
if content != nil { if content != nil {
@ -84,10 +84,10 @@ func runCommandForPlugin(plugin Plugin, event *matrix.Event, arguments []string)
"args": cmdArgs, "args": cmdArgs,
}).Warn("Command returned both error and content.") }).Warn("Command returned both error and content.")
} }
metrics.IncErrorCommand()
metrics.IncrementCommand(metrics.Failure)
content = matrix.TextMessage{"m.notice", err.Error()} content = matrix.TextMessage{"m.notice", err.Error()}
} else { } else {
metrics.IncSuccessCommand()
metrics.IncrementCommand(metrics.Success)
} }
return content return content

Loading…
Cancel
Save