This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathalert.go
More file actions
104 lines (92 loc) · 2.88 KB
/
Copy pathalert.go
File metadata and controls
104 lines (92 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package wasp
import (
"fmt"
"os"
"sort"
"strings"
"testing"
"time"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink-testing-framework/grafana"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// AlertChecker is checking alerts according to dashboardUUID and requirements labels
type AlertChecker struct {
RequirementLabelKey string
T *testing.T
l zerolog.Logger
grafanaClient *grafana.Client
}
func NewAlertChecker(t *testing.T) *AlertChecker {
url := os.Getenv("GRAFANA_URL")
if url == "" {
panic(fmt.Errorf("GRAFANA_URL env var must be defined"))
}
apiKey := os.Getenv("GRAFANA_TOKEN")
if apiKey == "" {
panic(fmt.Errorf("GRAFANA_TOKEN env var must be defined"))
}
grafanaClient := grafana.NewGrafanaClient(url, apiKey)
return &AlertChecker{
RequirementLabelKey: "requirement_name",
T: t,
grafanaClient: grafanaClient,
l: GetLogger(t, "AlertChecker"),
}
}
// AnyAlerts check if any alerts with dashboardUUID have been raised
func (m *AlertChecker) AnyAlerts(dashboardUUID, requirementLabelValue string) ([]grafana.AlertGroupsResponse, error) {
raised := false
defer func() {
if m.T != nil && raised {
m.T.Fail()
}
}()
alertGroups, _, err := m.grafanaClient.AlertManager.GetAlertGroups()
if err != nil {
return alertGroups, fmt.Errorf("failed to get alert groups: %s", err)
}
for _, a := range alertGroups {
for _, aa := range a.Alerts {
log.Debug().Interface("Alert", aa).Msg("Scanning alert")
if aa.Annotations.DashboardUID == dashboardUUID && aa.Labels[m.RequirementLabelKey] == requirementLabelValue {
log.Warn().
Str("Summary", aa.Annotations.Summary).
Str("Description", aa.Annotations.Description).
Str("URL", aa.GeneratorURL).
Interface("Labels", aa.Labels).
Time("StartsAt", aa.StartsAt).
Time("UpdatedAt", aa.UpdatedAt).
Str("State", aa.Status.State).
Msg("Alert fired")
raised = true
}
}
}
return alertGroups, nil
}
// CheckDashobardAlerts checks for alerts in the given dashboardUUIDs between from and to times
func CheckDashboardAlerts(grafanaClient *grafana.Client, from, to time.Time, dashboardUID string) ([]grafana.Annotation, error) {
annotationType := "alert"
alerts, _, err := grafanaClient.GetAnnotations(grafana.AnnotationsQueryParams{
DashboardUID: &dashboardUID,
From: &from,
To: &to,
Type: &annotationType,
})
if err != nil {
return alerts, fmt.Errorf("could not check for alerts: %s", err)
}
// Sort the annotations by time oldest to newest
sort.Slice(alerts, func(i, j int) bool {
return alerts[i].Time.Before(alerts[j].Time.Time)
})
// Check if any alerts are in alerting state
for _, a := range alerts {
if strings.ToLower(a.NewState) == "alerting" {
return alerts, errors.New("at least one alert was firing")
}
}
return alerts, nil
}