Skip to content

Commit

Permalink
Add tests for the collector
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed Nov 8, 2023
1 parent 4bea166 commit 1ea40e3
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 13 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ go 1.21.4

require (
github.com/go-openapi/runtime v0.26.0
github.com/golang/mock v1.6.0
github.com/prometheus/alertmanager v0.26.0
github.com/prometheus/client_golang v1.17.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
)

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
Expand All @@ -31,6 +34,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe
github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ=
github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0=
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down Expand Up @@ -169,6 +171,7 @@ github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23n
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.mongodb.org/mongo-driver v1.7.3/go.mod h1:NqaYOwnXWr5Pm7AOpO5QFxKJ503nbMse/R79oO62zWg=
go.mongodb.org/mongo-driver v1.7.5/go.mod h1:VXEWRZ6URJIkUq2SCAyapmhH0ZLRBP+FT4xhp5Zvxng=
go.mongodb.org/mongo-driver v1.10.0/go.mod h1:wsihk0Kdgv8Kqu1Anit4sfK+22vSFbUrAVEYRhCXrA8=
Expand All @@ -183,12 +186,16 @@ go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+go
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
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=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -205,13 +212,16 @@ golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand All @@ -220,7 +230,12 @@ golang.org/x/tools v0.0.0-20190329151228-23e29df326fe/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190416151739-9c9e1878f421/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190420181800-aa740d480789/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
Expand Down
25 changes: 13 additions & 12 deletions internal/alerts_collector/alerts_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"strings"

"github.com/prometheus/alertmanager/api/v2/client"
"github.com/prometheus/alertmanager/api/v2/client/alert"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/exp/slices"
Expand All @@ -14,14 +13,14 @@ import (
func newDesc(labels []string) *prometheus.Desc {
return prometheus.NewDesc(
"alerts_exporter_alerts",
"Alertmanager alerts",
"Alerts queried from the Alertmanager API. Alert state can be found in the '_alerts_exporter_alert_state' label.",
labels,
nil,
)
}

type AlertsCollector struct {
API *client.AlertmanagerAPI
AlertService alert.ClientService

WithInhibited, WithSilenced, WithUnprocessed, WithActive *bool

Expand All @@ -42,7 +41,7 @@ func (o *AlertsCollector) Collect(ch chan<- prometheus.Metric) {
WithInhibited(o.WithInhibited).
WithFilter(o.Filters)

as, err := o.API.Alert.GetAlerts(p)
as, err := o.AlertService.GetAlerts(p)

if err != nil {
ch <- prometheus.NewInvalidMetric(newDesc([]string{}), err)
Expand All @@ -51,14 +50,16 @@ func (o *AlertsCollector) Collect(ch chan<- prometheus.Metric) {
}

for _, a := range as.Payload {
if a.Status.State != nil {
a.Labels["_alerts_exporter_alert_status"] = *a.Status.State
}
if len(a.Status.InhibitedBy) > 0 {
a.Labels["_alerts_exporter_alert_inhibited_by"] = strings.Join(a.Status.InhibitedBy, ",")
}
if len(a.Status.SilencedBy) > 0 {
a.Labels["_alerts_exporter_alert_silenced_by"] = strings.Join(a.Status.SilencedBy, ",")
if a.Status != nil {
if a.Status.State != nil {
a.Labels["_alerts_exporter_alert_state"] = *a.Status.State
}
if len(a.Status.InhibitedBy) > 0 {
a.Labels["_alerts_exporter_alert_inhibited_by"] = strings.Join(a.Status.InhibitedBy, ",")
}
if len(a.Status.SilencedBy) > 0 {
a.Labels["_alerts_exporter_alert_silenced_by"] = strings.Join(a.Status.SilencedBy, ",")
}
}

k, v := pairs(a.Labels)
Expand Down
93 changes: 93 additions & 0 deletions internal/alerts_collector/alerts_collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package alertscollector_test

import (
"context"
"errors"
"strings"
"testing"

"github.com/golang/mock/gomock"
"github.com/prometheus/alertmanager/api/v2/client/alert"
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"

alertscollector "github.com/appuio/alerts_exporter/internal/alerts_collector"
"github.com/appuio/alerts_exporter/internal/alerts_collector/mock"
)

//go:generate go run github.com/golang/mock/mockgen -destination=./mock/alert_service.go -package mock github.com/prometheus/alertmanager/api/v2/client/alert ClientService

func TestAlertsCollector(t *testing.T) {
ctrl := gomock.NewController(t)

mockAlertService := mock.NewMockClientService(ctrl)

mockAlertService.
EXPECT().
GetAlerts(
gomock.Eq(alert.NewGetAlertsParamsWithContext(context.Background()).WithActive(ptr(true))),
gomock.Any(),
).
Return(&alert.GetAlertsOK{
Payload: []*models.GettableAlert{
{
Alert: models.Alert{
Labels: map[string]string{
"alertname": "ImportantAlert",
"severity": "critical",
},
},
Status: &models.AlertStatus{
State: ptr("active"),
InhibitedBy: []string{"22a8bdd0-9b1e-4855-b8fb-8c1e18fa434f"},
SilencedBy: []string{"d505b8d4-c5ce-466f-abd7-c704864299f5"},
},
},
{
Alert: models.Alert{
Labels: map[string]string{
"alertname": "WhateverHappensHappens",
"severity": "low",
},
},
},
},
}, nil)

subject := &alertscollector.AlertsCollector{
AlertService: mockAlertService,

WithActive: ptr(true),
}

require.NoError(t,
testutil.CollectAndCompare(subject, strings.NewReader(`
# HELP alerts_exporter_alerts Alerts queried from the Alertmanager API. Alert state can be found in the '_alerts_exporter_alert_state' label.
# TYPE alerts_exporter_alerts gauge
alerts_exporter_alerts{alertname="WhateverHappensHappens",severity="low"} 1
alerts_exporter_alerts{_alerts_exporter_alert_inhibited_by="22a8bdd0-9b1e-4855-b8fb-8c1e18fa434f",_alerts_exporter_alert_silenced_by="d505b8d4-c5ce-466f-abd7-c704864299f5",_alerts_exporter_alert_state="active",alertname="ImportantAlert",severity="critical"} 1
`),
),
)
}

func TestAlertsCollector_Err(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockAlertService := mock.NewMockClientService(ctrl)

mockAlertService.EXPECT().GetAlerts(gomock.Any(), gomock.Any()).Return(nil, errors.New("API error"))

subject := &alertscollector.AlertsCollector{
AlertService: mockAlertService,
}

require.ErrorContains(t,
testutil.CollectAndCompare(subject, nil),
"API error",
)
}

func ptr[T any](t T) *T { return &t }
88 changes: 88 additions & 0 deletions internal/alerts_collector/mock/alert_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func main() {
reg := prometheus.NewRegistry()

reg.MustRegister(&alertscollector.AlertsCollector{
API: ac,
AlertService: ac.Alert,

WithActive: &withActive,
WithSilenced: &withSilenced,
Expand Down

0 comments on commit 1ea40e3

Please sign in to comment.