Skip to content

Commit

Permalink
feat: filter not firing alerts on /firing (#15)
Browse files Browse the repository at this point in the history
* feat: filter not firing alerts on /firing

* chore: fix linting
  • Loading branch information
freak12techno authored Jan 31, 2023
1 parent 52914a4 commit f647381
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
go-version: '^1.18.0'
- run: go version
- run: go mod download
- run: go build cmd/grafana-interacter/main.go
- run: go build cmd/grafana-interacter.go
go-vet:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ config.yml
main
.idea
dist/
grafana-interacter
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ before:
hooks:
- go mod tidy
builds:
- main: ./cmd/grafana-interacter
- main: ./cmd/grafana-interacter.go
env:
- CGO_ENABLED=0
goos:
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
build:
go build cmd/grafana-interacter/main.go
go build cmd/grafana-interacter.go

install:
go install cmd/grafana-interacter/main.go
go install cmd/grafana-interacter.go

lint:
golangci-lint run --fix ./...
File renamed without changes.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/creasty/defaults v1.5.2
github.com/rs/zerolog v1.26.1
github.com/spf13/cobra v1.4.0
golang.org/x/exp v0.0.0-20230131120322-dfa7d7a641b0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/telebot.v3 v3.0.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/exp v0.0.0-20230131120322-dfa7d7a641b0 h1:Fi9VR3JnhlA3HOMXAmw2ZY4zypNQvZq01MpVbIA7hY4=
golang.org/x/exp v0.0.0-20230131120322-dfa7d7a641b0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
28 changes: 26 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"golang.org/x/exp/slices"

tele "gopkg.in/telebot.v3"
)

Expand Down Expand Up @@ -236,13 +238,35 @@ func ParseSilenceOptions(query string, c tele.Context) (*types.Silence, string)
func FilterFiringOrPendingAlertGroups(groups []types.GrafanaAlertGroup) []types.GrafanaAlertGroup {
var returnGroups []types.GrafanaAlertGroup

alertingStatuses := []string{"firing", "alerting", "pending"}

for _, group := range groups {
rules := []types.GrafanaAlertRule{}
hasAnyRules := false

for _, rule := range group.Rules {
if rule.State == "firing" || rule.State == "alerting" || rule.State == "pending" {
rules = append(rules, rule)
if !slices.Contains(alertingStatuses, strings.ToLower(rule.State)) {
continue
}

alerts := []types.GrafanaAlert{}
hasAnyAlerts := false

for _, alert := range rule.Alerts {
if !slices.Contains(alertingStatuses, strings.ToLower(alert.State)) {
continue
}

alerts = append(alerts, alert)
hasAnyAlerts = true
}

if hasAnyAlerts {
rules = append(rules, types.GrafanaAlertRule{
State: rule.State,
Name: rule.Name,
Alerts: alerts,
})
hasAnyRules = true
}
}
Expand Down

0 comments on commit f647381

Please sign in to comment.