Skip to content

Commit 4360014

Browse files
authored
add metrics (#34)
1 parent 840869e commit 4360014

File tree

13 files changed

+204
-12
lines changed

13 files changed

+204
-12
lines changed

engine/actions/action.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package actions
22

33
import (
4+
"fmt"
45
"strings"
56

67
"github.com/bivas/rivi/util/log"
8+
79
"github.com/mitchellh/multistep"
10+
"github.com/prometheus/client_golang/prometheus"
811
"github.com/spf13/viper"
912
)
1013

@@ -51,3 +54,12 @@ func BuildActionsFromConfiguration(config *viper.Viper) []Action {
5154
}
5255
return result
5356
}
57+
58+
func NewCounter(name string) prometheus.Counter {
59+
return prometheus.NewCounter(prometheus.CounterOpts{
60+
Namespace: "rivi",
61+
Subsystem: "actions",
62+
Name: name,
63+
Help: fmt.Sprintf("Action counter for %s", name),
64+
})
65+
}

engine/actions/autoassign/autoassign.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/bivas/rivi/util/log"
1313
"github.com/mitchellh/mapstructure"
1414
"github.com/mitchellh/multistep"
15+
"github.com/prometheus/client_golang/prometheus"
1516
)
1617

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

6869
winners := a.randomUsers(conf, meta, lookupRoles)
6970
if len(winners) > 0 {
71+
counter.Inc()
7072
meta.AddAssignees(winners...)
7173
}
7274
}
@@ -121,6 +123,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
121123
return &action{rule: &item, logger: logger}
122124
}
123125

126+
var counter = actions.NewCounter("autoassign")
127+
124128
func init() {
125129
actions.RegisterAction("autoassign", &factory{})
130+
prometheus.Register(counter)
126131
}

engine/actions/automerge/automerge.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package automerge
22

33
import (
4+
"errors"
45
"fmt"
56
"strings"
67

78
"github.com/bivas/rivi/engine/actions"
89
"github.com/bivas/rivi/types"
910
"github.com/bivas/rivi/util"
1011
"github.com/bivas/rivi/util/log"
12+
1113
"github.com/mitchellh/mapstructure"
1214
"github.com/mitchellh/multistep"
15+
"github.com/prometheus/client_golang/prometheus"
1316
)
1417

1518
type action struct {
@@ -33,11 +36,12 @@ type HasReviewersAPIData interface {
3336
}
3437

3538
func (a *action) merge(meta types.Data) {
39+
counter.Inc()
3640
if a.rule.Label == "" {
3741
mergeable, ok := meta.(MergeableData)
3842
if !ok {
3943
a.logger.Warning("Event data does not support merge. Check your configurations")
40-
a.err = fmt.Errorf("Event data does not support merge")
44+
a.err = errors.New("event data does not support merge")
4145
return
4246
}
4347
mergeable.Merge(a.rule.Strategy)
@@ -129,6 +133,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
129133
return &action{rule: &item, logger: logger}
130134
}
131135

136+
var counter = actions.NewCounter("automerge")
137+
132138
func init() {
133139
actions.RegisterAction("automerge", &factory{})
140+
prometheus.Register(counter)
134141
}

engine/actions/commenter/commenter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/bivas/rivi/util/log"
99
"github.com/mitchellh/mapstructure"
1010
"github.com/mitchellh/multistep"
11+
"github.com/prometheus/client_golang/prometheus"
1112
)
1213

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

2122
func (a *action) Apply(state multistep.StateBag) {
23+
counter.Inc()
2224
state.Get("data").(types.Data).AddComment(a.rule.Comment)
2325
}
2426

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

39+
var counter = actions.NewCounter("commenter")
40+
3741
func init() {
3842
actions.RegisterAction("commenter", &factory{})
43+
prometheus.Register(counter)
3944
}

engine/actions/labeler/labeler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/bivas/rivi/util/log"
99
"github.com/mitchellh/mapstructure"
1010
"github.com/mitchellh/multistep"
11+
"github.com/prometheus/client_golang/prometheus"
1112
)
1213

1314
type action struct {
@@ -29,6 +30,7 @@ func (a *action) Apply(state multistep.StateBag) {
2930
log.MetaFields{log.F("issue", meta.GetShortName())},
3031
"Skipping label '%s' as it already exists", apply)
3132
} else {
33+
counter.Inc()
3234
meta.AddLabel(apply)
3335
}
3436
}
@@ -39,6 +41,7 @@ func (a *action) Apply(state multistep.StateBag) {
3941
log.MetaFields{log.F("issue", meta.GetShortName())},
4042
"Skipping label '%s' removal as it does not exists", remove)
4143
} else {
44+
counter.Inc()
4245
meta.RemoveLabel(remove)
4346
}
4447
}
@@ -57,6 +60,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
5760
return &action{rule: &item, logger: logger}
5861
}
5962

63+
var counter = actions.NewCounter("labeler")
64+
6065
func init() {
6166
actions.RegisterAction("labeler", &factory{})
67+
prometheus.Register(counter)
6268
}

engine/actions/locker/locker.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package locker
22

33
import (
4-
"fmt"
5-
64
"github.com/bivas/rivi/engine/actions"
75
"github.com/bivas/rivi/types"
86
"github.com/bivas/rivi/util/log"
97
"github.com/mitchellh/mapstructure"
108
"github.com/mitchellh/multistep"
9+
"github.com/pkg/errors"
10+
"github.com/prometheus/client_golang/prometheus"
1111
)
1212

1313
type LockableData interface {
@@ -29,13 +29,14 @@ func (a *action) Apply(state multistep.StateBag) {
2929
a.logger.WarningWith(
3030
log.MetaFields{log.F("issue", meta.GetShortName())},
3131
"Event data does not support locking. Check your configurations")
32-
a.err = fmt.Errorf("Event data does not support locking")
32+
a.err = errors.New("event data does not support locking")
3333
return
3434
}
3535
if lockable.LockState() {
3636
a.logger.Debug("Issue is locked")
3737
if a.rule.State == "unlock" || a.rule.State == "change" {
3838
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "unlocking issue")
39+
counter.Inc()
3940
lockable.Unlock()
4041
} else if a.rule.State == "lock" {
4142
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Issue is already locked - nothing changed")
@@ -44,6 +45,7 @@ func (a *action) Apply(state multistep.StateBag) {
4445
a.logger.Debug("Issue is unlocked")
4546
if a.rule.State == "lock" || a.rule.State == "change" {
4647
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Locking issue")
48+
counter.Inc()
4749
lockable.Lock()
4850
} else if a.rule.State == "lock" {
4951
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Issue is already unlocked - nothing changed")
@@ -64,6 +66,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
6466
return &action{rule: &item, logger: logger}
6567
}
6668

69+
var counter = actions.NewCounter("locker")
70+
6771
func init() {
6872
actions.RegisterAction("locker", &factory{})
73+
prometheus.Register(counter)
6974
}

engine/actions/sizing/sizing.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/bivas/rivi/util/log"
1111
"github.com/mitchellh/mapstructure"
1212
"github.com/mitchellh/multistep"
13+
"github.com/prometheus/client_golang/prometheus"
1314
)
1415

1516
type action struct {
@@ -92,6 +93,7 @@ func (a *action) Apply(state multistep.StateBag) {
9293
a.logger.DebugWith(
9394
log.MetaFields{log.F("issue", meta.GetShortName())},
9495
"Updating label from %s to %s", currentMatchedLabel, matchedLabel)
96+
counter.Inc()
9597
meta.RemoveLabel(currentMatchedLabel)
9698
meta.AddLabel(matchedLabel)
9799
if matchedRule.Comment != "" {
@@ -102,6 +104,7 @@ func (a *action) Apply(state multistep.StateBag) {
102104
log.F("issue", meta.GetShortName())},
103105
"Updating label to %s",
104106
matchedLabel)
107+
counter.Inc()
105108
meta.AddLabel(matchedLabel)
106109
if matchedRule.Comment != "" {
107110
meta.AddComment(matchedRule.Comment)
@@ -130,6 +133,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
130133
return &result
131134
}
132135

136+
var counter = actions.NewCounter("sizing")
137+
133138
func init() {
134139
actions.RegisterAction("sizing", &factory{})
140+
prometheus.Register(counter)
135141
}

engine/actions/slack/slack.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/mitchellh/mapstructure"
1414
"github.com/mitchellh/multistep"
1515
api "github.com/nlopes/slack"
16+
"github.com/prometheus/client_golang/prometheus"
1617
)
1718

1819
type action struct {
@@ -86,6 +87,7 @@ func (a *action) sendChannelMessage(config config.Configuration, meta types.Data
8687
"Unable to get channel info")
8788
return
8889
}
90+
counter.Inc()
8991
a.postMessage(channel.ID, meta.GetAssignees(), config, meta)
9092
}
9193

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

186+
var counter = actions.NewCounter("slack")
187+
183188
func init() {
184189
actions.RegisterAction("slack", &factory{})
190+
prometheus.Register(counter)
185191
}

engine/actions/trigger/trigger.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/bivas/rivi/util/log"
1313
"github.com/mitchellh/mapstructure"
1414
"github.com/mitchellh/multistep"
15+
"github.com/prometheus/client_golang/prometheus"
1516
)
1617

1718
type action struct {
@@ -35,13 +36,14 @@ func (a *action) Apply(state multistep.StateBag) {
3536
log.F("method", request.Method),
3637
log.F("content-length", request.ContentLength),
3738
}, "Prepared Request")
39+
counter.Inc()
3840
response, e := a.client.Do(request)
3941
if e != nil {
40-
a.err = fmt.Errorf("Triggering to %s, resulted in error. %s",
42+
a.err = fmt.Errorf("triggering to %s, resulted in error. %s",
4143
a.rule.Endpoint,
4244
e)
4345
} else if response.StatusCode >= 400 {
44-
a.err = fmt.Errorf("Triggering to %s, resulted in wrong status code. %d",
46+
a.err = fmt.Errorf("triggering to %s, resulted in wrong status code. %d",
4547
a.rule.Endpoint,
4648
response.StatusCode)
4749
}
@@ -100,6 +102,9 @@ func (*factory) BuildAction(config map[string]interface{}) actions.Action {
100102
return &action{rule: &item, client: http.DefaultClient, logger: logger}
101103
}
102104

105+
var counter = actions.NewCounter("trigger")
106+
103107
func init() {
104108
actions.RegisterAction("trigger", &factory{})
109+
prometheus.Register(counter)
105110
}

0 commit comments

Comments
 (0)