Skip to content

Commit

Permalink
add metrics (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
bivas committed Nov 7, 2017
1 parent 840869e commit 4360014
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 12 deletions.
12 changes: 12 additions & 0 deletions engine/actions/action.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package actions

import (
"fmt"
"strings"

"github.com/bivas/rivi/util/log"

"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -51,3 +54,12 @@ func BuildActionsFromConfiguration(config *viper.Viper) []Action {
}
return result
}

func NewCounter(name string) prometheus.Counter {
return prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "rivi",
Subsystem: "actions",
Name: name,
Help: fmt.Sprintf("Action counter for %s", name),
})
}
5 changes: 5 additions & 0 deletions engine/actions/autoassign/autoassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/bivas/rivi/util/log"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
)

type action struct {
Expand Down Expand Up @@ -67,6 +68,7 @@ func (a *action) Apply(state multistep.StateBag) {

winners := a.randomUsers(conf, meta, lookupRoles)
if len(winners) > 0 {
counter.Inc()
meta.AddAssignees(winners...)
}
}
Expand Down Expand Up @@ -121,6 +123,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
return &action{rule: &item, logger: logger}
}

var counter = actions.NewCounter("autoassign")

func init() {
actions.RegisterAction("autoassign", &factory{})
prometheus.Register(counter)
}
9 changes: 8 additions & 1 deletion engine/actions/automerge/automerge.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package automerge

import (
"errors"
"fmt"
"strings"

"github.com/bivas/rivi/engine/actions"
"github.com/bivas/rivi/types"
"github.com/bivas/rivi/util"
"github.com/bivas/rivi/util/log"

"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
)

type action struct {
Expand All @@ -33,11 +36,12 @@ type HasReviewersAPIData interface {
}

func (a *action) merge(meta types.Data) {
counter.Inc()
if a.rule.Label == "" {
mergeable, ok := meta.(MergeableData)
if !ok {
a.logger.Warning("Event data does not support merge. Check your configurations")
a.err = fmt.Errorf("Event data does not support merge")
a.err = errors.New("event data does not support merge")
return
}
mergeable.Merge(a.rule.Strategy)
Expand Down Expand Up @@ -129,6 +133,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
return &action{rule: &item, logger: logger}
}

var counter = actions.NewCounter("automerge")

func init() {
actions.RegisterAction("automerge", &factory{})
prometheus.Register(counter)
}
5 changes: 5 additions & 0 deletions engine/actions/commenter/commenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bivas/rivi/util/log"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
)

type action struct {
Expand All @@ -19,6 +20,7 @@ func (a *action) String() string {
}

func (a *action) Apply(state multistep.StateBag) {
counter.Inc()
state.Get("data").(types.Data).AddComment(a.rule.Comment)
}

Expand All @@ -34,6 +36,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
return &action{rule: &item}
}

var counter = actions.NewCounter("commenter")

func init() {
actions.RegisterAction("commenter", &factory{})
prometheus.Register(counter)
}
6 changes: 6 additions & 0 deletions engine/actions/labeler/labeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bivas/rivi/util/log"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
)

type action struct {
Expand All @@ -29,6 +30,7 @@ func (a *action) Apply(state multistep.StateBag) {
log.MetaFields{log.F("issue", meta.GetShortName())},
"Skipping label '%s' as it already exists", apply)
} else {
counter.Inc()
meta.AddLabel(apply)
}
}
Expand All @@ -39,6 +41,7 @@ func (a *action) Apply(state multistep.StateBag) {
log.MetaFields{log.F("issue", meta.GetShortName())},
"Skipping label '%s' removal as it does not exists", remove)
} else {
counter.Inc()
meta.RemoveLabel(remove)
}
}
Expand All @@ -57,6 +60,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
return &action{rule: &item, logger: logger}
}

var counter = actions.NewCounter("labeler")

func init() {
actions.RegisterAction("labeler", &factory{})
prometheus.Register(counter)
}
11 changes: 8 additions & 3 deletions engine/actions/locker/locker.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package locker

import (
"fmt"

"github.com/bivas/rivi/engine/actions"
"github.com/bivas/rivi/types"
"github.com/bivas/rivi/util/log"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)

type LockableData interface {
Expand All @@ -29,13 +29,14 @@ func (a *action) Apply(state multistep.StateBag) {
a.logger.WarningWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"Event data does not support locking. Check your configurations")
a.err = fmt.Errorf("Event data does not support locking")
a.err = errors.New("event data does not support locking")
return
}
if lockable.LockState() {
a.logger.Debug("Issue is locked")
if a.rule.State == "unlock" || a.rule.State == "change" {
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "unlocking issue")
counter.Inc()
lockable.Unlock()
} else if a.rule.State == "lock" {
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Issue is already locked - nothing changed")
Expand All @@ -44,6 +45,7 @@ func (a *action) Apply(state multistep.StateBag) {
a.logger.Debug("Issue is unlocked")
if a.rule.State == "lock" || a.rule.State == "change" {
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Locking issue")
counter.Inc()
lockable.Lock()
} else if a.rule.State == "lock" {
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Issue is already unlocked - nothing changed")
Expand All @@ -64,6 +66,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
return &action{rule: &item, logger: logger}
}

var counter = actions.NewCounter("locker")

func init() {
actions.RegisterAction("locker", &factory{})
prometheus.Register(counter)
}
6 changes: 6 additions & 0 deletions engine/actions/sizing/sizing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bivas/rivi/util/log"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
)

type action struct {
Expand Down Expand Up @@ -92,6 +93,7 @@ func (a *action) Apply(state multistep.StateBag) {
a.logger.DebugWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"Updating label from %s to %s", currentMatchedLabel, matchedLabel)
counter.Inc()
meta.RemoveLabel(currentMatchedLabel)
meta.AddLabel(matchedLabel)
if matchedRule.Comment != "" {
Expand All @@ -102,6 +104,7 @@ func (a *action) Apply(state multistep.StateBag) {
log.F("issue", meta.GetShortName())},
"Updating label to %s",
matchedLabel)
counter.Inc()
meta.AddLabel(matchedLabel)
if matchedRule.Comment != "" {
meta.AddComment(matchedRule.Comment)
Expand Down Expand Up @@ -130,6 +133,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
return &result
}

var counter = actions.NewCounter("sizing")

func init() {
actions.RegisterAction("sizing", &factory{})
prometheus.Register(counter)
}
6 changes: 6 additions & 0 deletions engine/actions/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
api "github.com/nlopes/slack"
"github.com/prometheus/client_golang/prometheus"
)

type action struct {
Expand Down Expand Up @@ -86,6 +87,7 @@ func (a *action) sendChannelMessage(config config.Configuration, meta types.Data
"Unable to get channel info")
return
}
counter.Inc()
a.postMessage(channel.ID, meta.GetAssignees(), config, meta)
}

Expand All @@ -100,6 +102,7 @@ func (a *action) sendPrivateMessage(config config.Configuration, meta types.Data
log.F("user.id", slacker)}, "Unable to open IM channel")
continue
}
counter.Inc()
if err := a.postMessage(id, targets, config, meta); err != nil {
continue
}
Expand Down Expand Up @@ -180,6 +183,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
return &action{rule: &item, logger: logger}
}

var counter = actions.NewCounter("slack")

func init() {
actions.RegisterAction("slack", &factory{})
prometheus.Register(counter)
}
9 changes: 7 additions & 2 deletions engine/actions/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/bivas/rivi/util/log"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
)

type action struct {
Expand All @@ -35,13 +36,14 @@ func (a *action) Apply(state multistep.StateBag) {
log.F("method", request.Method),
log.F("content-length", request.ContentLength),
}, "Prepared Request")
counter.Inc()
response, e := a.client.Do(request)
if e != nil {
a.err = fmt.Errorf("Triggering to %s, resulted in error. %s",
a.err = fmt.Errorf("triggering to %s, resulted in error. %s",
a.rule.Endpoint,
e)
} else if response.StatusCode >= 400 {
a.err = fmt.Errorf("Triggering to %s, resulted in wrong status code. %d",
a.err = fmt.Errorf("triggering to %s, resulted in wrong status code. %d",
a.rule.Endpoint,
response.StatusCode)
}
Expand Down Expand Up @@ -100,6 +102,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
return &action{rule: &item, client: http.DefaultClient, logger: logger}
}

var counter = actions.NewCounter("trigger")

func init() {
actions.RegisterAction("trigger", &factory{})
prometheus.Register(counter)
}
Loading

0 comments on commit 4360014

Please sign in to comment.