Skip to content

Commit 79f885a

Browse files
authored
Add tests for the collector (#6)
1 parent 4bea166 commit 79f885a

File tree

7 files changed

+225
-13
lines changed

7 files changed

+225
-13
lines changed

go.mod

+6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ go 1.21.4
44

55
require (
66
github.com/go-openapi/runtime v0.26.0
7+
github.com/golang/mock v1.6.0
78
github.com/prometheus/alertmanager v0.26.0
89
github.com/prometheus/client_golang v1.17.0
10+
github.com/stretchr/testify v1.8.4
911
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
1012
)
1113

1214
require (
1315
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
1416
github.com/beorn7/perks v1.0.1 // indirect
1517
github.com/cespare/xxhash/v2 v2.2.0 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
1619
github.com/go-logr/logr v1.2.3 // indirect
1720
github.com/go-logr/stdr v1.2.2 // indirect
1821
github.com/go-openapi/analysis v0.21.4 // indirect
@@ -31,13 +34,16 @@ require (
3134
github.com/mitchellh/mapstructure v1.5.0 // indirect
3235
github.com/oklog/ulid v1.3.1 // indirect
3336
github.com/opentracing/opentracing-go v1.2.0 // indirect
37+
github.com/pmezard/go-difflib v1.0.0 // indirect
3438
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
3539
github.com/prometheus/common v0.44.0 // indirect
3640
github.com/prometheus/procfs v0.11.1 // indirect
3741
go.mongodb.org/mongo-driver v1.11.3 // indirect
3842
go.opentelemetry.io/otel v1.14.0 // indirect
3943
go.opentelemetry.io/otel/trace v1.14.0 // indirect
44+
golang.org/x/mod v0.13.0 // indirect
4045
golang.org/x/sys v0.13.0 // indirect
46+
golang.org/x/tools v0.14.0 // indirect
4147
google.golang.org/protobuf v1.31.0 // indirect
4248
gopkg.in/yaml.v2 v2.4.0 // indirect
4349
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

+19
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe
7777
github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ=
7878
github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0=
7979
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
80+
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
81+
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
8082
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
8183
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
8284
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
@@ -169,6 +171,7 @@ github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23n
169171
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
170172
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
171173
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
174+
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
172175
go.mongodb.org/mongo-driver v1.7.3/go.mod h1:NqaYOwnXWr5Pm7AOpO5QFxKJ503nbMse/R79oO62zWg=
173176
go.mongodb.org/mongo-driver v1.7.5/go.mod h1:VXEWRZ6URJIkUq2SCAyapmhH0ZLRBP+FT4xhp5Zvxng=
174177
go.mongodb.org/mongo-driver v1.10.0/go.mod h1:wsihk0Kdgv8Kqu1Anit4sfK+22vSFbUrAVEYRhCXrA8=
@@ -183,12 +186,18 @@ go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+go
183186
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
184187
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
185188
golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
189+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
186190
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
187191
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
188192
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
189193
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
194+
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
195+
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
196+
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
190197
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
191198
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
199+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
200+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
192201
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
193202
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
194203
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -205,13 +214,16 @@ golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7w
205214
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
206215
golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
207216
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
217+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
208218
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
209219
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
220+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
210221
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
211222
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
212223
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
213224
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
214225
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
226+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
215227
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
216228
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
217229
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
@@ -220,7 +232,14 @@ golang.org/x/tools v0.0.0-20190329151228-23e29df326fe/go.mod h1:LCzVGOaR6xXOjkQ3
220232
golang.org/x/tools v0.0.0-20190416151739-9c9e1878f421/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
221233
golang.org/x/tools v0.0.0-20190420181800-aa740d480789/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
222234
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
235+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
236+
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
237+
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
238+
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
239+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
240+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
223241
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
242+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
224243
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
225244
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
226245
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=

internal/alerts_collector/alerts_collector.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"strings"
77

8-
"github.com/prometheus/alertmanager/api/v2/client"
98
"github.com/prometheus/alertmanager/api/v2/client/alert"
109
"github.com/prometheus/client_golang/prometheus"
1110
"golang.org/x/exp/slices"
@@ -14,14 +13,14 @@ import (
1413
func newDesc(labels []string) *prometheus.Desc {
1514
return prometheus.NewDesc(
1615
"alerts_exporter_alerts",
17-
"Alertmanager alerts",
16+
"Alerts queried from the Alertmanager API. Alert state can be found in the '_alerts_exporter_alert_state' label.",
1817
labels,
1918
nil,
2019
)
2120
}
2221

2322
type AlertsCollector struct {
24-
API *client.AlertmanagerAPI
23+
AlertService alert.ClientService
2524

2625
WithInhibited, WithSilenced, WithUnprocessed, WithActive *bool
2726

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

45-
as, err := o.API.Alert.GetAlerts(p)
44+
as, err := o.AlertService.GetAlerts(p)
4645

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

5352
for _, a := range as.Payload {
54-
if a.Status.State != nil {
55-
a.Labels["_alerts_exporter_alert_status"] = *a.Status.State
56-
}
57-
if len(a.Status.InhibitedBy) > 0 {
58-
a.Labels["_alerts_exporter_alert_inhibited_by"] = strings.Join(a.Status.InhibitedBy, ",")
59-
}
60-
if len(a.Status.SilencedBy) > 0 {
61-
a.Labels["_alerts_exporter_alert_silenced_by"] = strings.Join(a.Status.SilencedBy, ",")
53+
if a.Status != nil {
54+
if a.Status.State != nil {
55+
a.Labels["_alerts_exporter_alert_state"] = *a.Status.State
56+
}
57+
if len(a.Status.InhibitedBy) > 0 {
58+
a.Labels["_alerts_exporter_alert_inhibited_by"] = strings.Join(a.Status.InhibitedBy, ",")
59+
}
60+
if len(a.Status.SilencedBy) > 0 {
61+
a.Labels["_alerts_exporter_alert_silenced_by"] = strings.Join(a.Status.SilencedBy, ",")
62+
}
6263
}
6364

6465
k, v := pairs(a.Labels)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package alertscollector_test
2+
3+
import (
4+
"context"
5+
"errors"
6+
"strings"
7+
"testing"
8+
9+
"github.com/golang/mock/gomock"
10+
"github.com/prometheus/alertmanager/api/v2/client/alert"
11+
"github.com/prometheus/alertmanager/api/v2/models"
12+
"github.com/prometheus/client_golang/prometheus/testutil"
13+
"github.com/stretchr/testify/require"
14+
15+
alertscollector "github.com/appuio/alerts_exporter/internal/alerts_collector"
16+
"github.com/appuio/alerts_exporter/internal/alerts_collector/mock"
17+
)
18+
19+
//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
20+
21+
func TestAlertsCollector(t *testing.T) {
22+
ctrl := gomock.NewController(t)
23+
24+
mockAlertService := mock.NewMockClientService(ctrl)
25+
26+
mockAlertService.
27+
EXPECT().
28+
GetAlerts(
29+
gomock.Eq(alert.NewGetAlertsParamsWithContext(context.Background()).WithActive(ptr(true))),
30+
gomock.Any(),
31+
).
32+
Return(&alert.GetAlertsOK{
33+
Payload: []*models.GettableAlert{
34+
{
35+
Alert: models.Alert{
36+
Labels: map[string]string{
37+
"alertname": "ImportantAlert",
38+
"severity": "critical",
39+
},
40+
},
41+
Status: &models.AlertStatus{
42+
State: ptr("active"),
43+
InhibitedBy: []string{"22a8bdd0-9b1e-4855-b8fb-8c1e18fa434f"},
44+
SilencedBy: []string{"d505b8d4-c5ce-466f-abd7-c704864299f5"},
45+
},
46+
},
47+
{
48+
Alert: models.Alert{
49+
Labels: map[string]string{
50+
"alertname": "WhateverHappensHappens",
51+
"severity": "low",
52+
},
53+
},
54+
},
55+
},
56+
}, nil)
57+
58+
subject := &alertscollector.AlertsCollector{
59+
AlertService: mockAlertService,
60+
61+
WithActive: ptr(true),
62+
}
63+
64+
require.NoError(t,
65+
testutil.CollectAndCompare(subject, strings.NewReader(`
66+
# HELP alerts_exporter_alerts Alerts queried from the Alertmanager API. Alert state can be found in the '_alerts_exporter_alert_state' label.
67+
# TYPE alerts_exporter_alerts gauge
68+
alerts_exporter_alerts{alertname="WhateverHappensHappens",severity="low"} 1
69+
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
70+
`),
71+
),
72+
)
73+
}
74+
75+
func TestAlertsCollector_Err(t *testing.T) {
76+
ctrl := gomock.NewController(t)
77+
defer ctrl.Finish()
78+
79+
mockAlertService := mock.NewMockClientService(ctrl)
80+
81+
mockAlertService.EXPECT().GetAlerts(gomock.Any(), gomock.Any()).Return(nil, errors.New("API error"))
82+
83+
subject := &alertscollector.AlertsCollector{
84+
AlertService: mockAlertService,
85+
}
86+
87+
require.ErrorContains(t,
88+
testutil.CollectAndCompare(subject, nil),
89+
"API error",
90+
)
91+
}
92+
93+
func ptr[T any](t T) *T { return &t }

internal/alerts_collector/mock/alert_service.go

+88
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func main() {
7373
reg := prometheus.NewRegistry()
7474

7575
reg.MustRegister(&alertscollector.AlertsCollector{
76-
API: ac,
76+
AlertService: ac.Alert,
7777

7878
WithActive: &withActive,
7979
WithSilenced: &withSilenced,

tools/tools.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build tools
2+
3+
package tools
4+
5+
import _ "github.com/golang/mock/mockgen"

0 commit comments

Comments
 (0)