-
Notifications
You must be signed in to change notification settings - Fork 8
/
payload.go
79 lines (64 loc) · 1.76 KB
/
payload.go
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
package types
import (
"sort"
"time"
)
type AlertStatus string
const (
AlertStatusResolved AlertStatus = "resolved"
AlertStatusFiring AlertStatus = "firing"
)
type WebhookPayload struct {
Version string `json:"version"`
GroupKey string `json:"groupKey"`
TruncatedAlerts uint64 `json:"truncatedAlerts"`
Status AlertStatus `json:"status"`
Receiver string `json:"receiver"`
GroupLabels map[string]string `json:"groupLabels"`
CommonLabels map[string]string `json:"commonLabels"`
CommonAnnotations map[string]string `json:"commonAnnotations"`
ExternalURL string `json:"externalURL"`
Alerts []WebhookAlert `json:"alerts"`
}
type WebhookAlert struct {
Status AlertStatus `json:"status"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
}
func (p *WebhookPayload) LabelKeysExceptCommon() []string {
m := map[string]struct{}{}
for _, alert := range p.Alerts {
for k := range alert.Labels {
m[k] = struct{}{}
}
}
var keys []string
for k := range m {
if _, ok := p.CommonLabels[k]; ok {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func (p *WebhookPayload) AnnotationKeysExceptCommon() []string {
m := map[string]struct{}{}
for _, alert := range p.Alerts {
for k := range alert.Annotations {
m[k] = struct{}{}
}
}
var keys []string
for k := range m {
if _, ok := p.CommonAnnotations[k]; ok {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}