-
Notifications
You must be signed in to change notification settings - Fork 25
/
alert.go
276 lines (236 loc) · 8.82 KB
/
alert.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package alerts
import (
"strings"
"time"
"github.com/go-openapi/strfmt"
"github.com/prometheus/alertmanager/api/v2/models"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"github.com/fluxninja/aperture/v2/pkg/log"
otelconsts "github.com/fluxninja/aperture/v2/pkg/otelcollector/consts"
)
type alertSeverity string
func (a alertSeverity) String() string { return string(a) }
const (
// SeverityCrit describes an alert which requires immediate action.
SeverityCrit alertSeverity = "crit"
// SeverityWarn describes an alert which requires further observation.
SeverityWarn alertSeverity = "warn"
// SeverityInfo describes an alert which has informational purposes.
SeverityInfo alertSeverity = "info"
// SeverityUnknown describes an alert which does not have severity set.
SeverityUnknown alertSeverity = ""
)
// ParseSeverity returns alert severity parsed from string. Returns SeverityUnknown
// if parsing fails.
func ParseSeverity(rawSeverity string) alertSeverity {
s := alertSeverity(rawSeverity)
switch s {
case SeverityCrit, SeverityWarn, SeverityInfo:
return s
default:
return SeverityUnknown
}
}
// specialLabels are alert labels which are propagated in dedicated fields in OTel logs.
var specialLabels = map[string]struct{}{
otelconsts.AlertNameLabel: {},
otelconsts.AlertSeverityLabel: {},
otelconsts.AlertGeneratorURLLabel: {},
}
// AlertOption is a type for constructor options.
type AlertOption func(*Alert)
// NewAlert creates new instance of Alert with StartsAt set to now.
func NewAlert(opts ...AlertOption) *Alert {
newAlert := &Alert{
postableAlert: models.PostableAlert{
Alert: models.Alert{
Labels: models.LabelSet(map[string]string{}),
},
Annotations: models.LabelSet(map[string]string{}),
StartsAt: strfmt.DateTime(time.Now().UTC()),
},
}
for _, opt := range opts {
opt(newAlert)
}
return newAlert
}
// NewAlertFromPostableAlert creates new alert with given PostableAlert.
func NewAlertFromPostableAlert(postabelAlert models.PostableAlert) *Alert {
return &Alert{
postableAlert: postabelAlert,
}
}
// Alert is a wrapper around models.PostableAlert with handy transform methods.
type Alert struct {
postableAlert models.PostableAlert
}
// Name gets the alert name from labels. Returns empty string if label not found.
func (a *Alert) Name() string {
return a.postableAlert.Labels[otelconsts.AlertNameLabel]
}
// SetName sets the alert name in labels. Overwrites previous value if exists.
func (a *Alert) SetName(name string) {
a.postableAlert.Labels[otelconsts.AlertNameLabel] = name
}
// WithName is an option function for constructor.
func WithName(name string) AlertOption {
return func(a *Alert) {
a.SetName(name)
}
}
// Severity gets the alert severity from labels. Returns empty string if label not found.
func (a *Alert) Severity() alertSeverity {
raw, ok := a.postableAlert.Labels[otelconsts.AlertSeverityLabel]
if !ok {
return SeverityUnknown
}
return ParseSeverity(raw)
}
// SetSeverity sets the alert severity in labels. Overwrites previous value if exists.
func (a *Alert) SetSeverity(severity alertSeverity) {
a.postableAlert.Labels[otelconsts.AlertSeverityLabel] = severity.String()
}
// WithSeverity is an option function for constructor.
func WithSeverity(severity alertSeverity) AlertOption {
return func(a *Alert) {
a.SetSeverity(severity)
}
}
// AlertChannels gets the alert channels from labels. Returns empty slice if label not found.
func (a *Alert) AlertChannels() []string {
channels, ok := a.postableAlert.Labels[otelconsts.AlertChannelsLabel]
if !ok {
return []string{}
}
return strings.Split(channels, ",")
}
// SetAlertChannels sets the alert channels in labels. Overwrites previous value if exists.
func (a *Alert) SetAlertChannels(alertChannels []string) {
a.postableAlert.Labels[otelconsts.AlertChannelsLabel] = strings.Join(alertChannels, ",")
}
// WithAlertChannels is an option function for constructor.
func WithAlertChannels(alertChannels []string) AlertOption {
return func(a *Alert) {
a.SetAlertChannels(alertChannels)
}
}
// PostableAlert returns the underlying PostableAlert struct.
func (a *Alert) PostableAlert() models.PostableAlert {
return a.postableAlert
}
// SetAnnotation sets a single annotation. It overwrites the previous value if exists.
func (a *Alert) SetAnnotation(key, value string) {
a.postableAlert.Annotations[key] = value
}
// WithAnnotation is an option function for constructor.
func WithAnnotation(key, value string) AlertOption {
return func(a *Alert) {
a.SetAnnotation(key, value)
}
}
// SetLabel sets a single label. It overwrites the previous value if exists.
func (a *Alert) SetLabel(key, value string) {
a.postableAlert.Labels[key] = value
}
// WithLabel is an option function for constructor.
func WithLabel(key, value string) AlertOption {
return func(a *Alert) {
a.SetLabel(key, value)
}
}
// SetGeneratorURL sets a generator URL. It overwrites the previous value if exists.
func (a *Alert) SetGeneratorURL(value string) {
a.postableAlert.GeneratorURL = strfmt.URI(value)
}
// WithGeneratorURL is an option function for constructor.
func WithGeneratorURL(value string) AlertOption {
return func(a *Alert) {
a.SetGeneratorURL(value)
}
}
// SetResolveTimeout sets a resolve timeout which says when given alert becomes resolved.
func (a *Alert) SetResolveTimeout(t time.Duration) {
a.postableAlert.EndsAt = strfmt.DateTime(time.Time(a.postableAlert.StartsAt).Add(t))
}
// WithResolveTimeout is an option function for constructor.
func WithResolveTimeout(t time.Duration) AlertOption {
return func(a *Alert) {
a.SetResolveTimeout(t)
}
}
// AlertsFromLogs gets slice of alerts from OTel Logs.
func AlertsFromLogs(ld plog.Logs) []*Alert {
// We cannot preallocate size, as we do not know how many of those log records
// has incorrect data and will be dropped.
alerts := []*Alert{}
resourceLogsSlice := ld.ResourceLogs()
for resourceLogsIt := 0; resourceLogsIt < resourceLogsSlice.Len(); resourceLogsIt++ {
resourceLogs := resourceLogsSlice.At(resourceLogsIt)
resourceAttributes := resourceLogs.Resource().Attributes()
generatorURL, exists := resourceAttributes.Get(otelconsts.AlertGeneratorURLLabel)
if !exists {
log.Trace().
Str("key", otelconsts.AlertGeneratorURLLabel).Msg("Key not found")
return nil
}
scopeLogsSlice := resourceLogs.ScopeLogs()
for scopeLogsIt := 0; scopeLogsIt < scopeLogsSlice.Len(); scopeLogsIt++ {
scopeLogs := scopeLogsSlice.At(scopeLogsIt)
logsSlice := scopeLogs.LogRecords()
for logsIt := 0; logsIt < logsSlice.Len(); logsIt++ {
logRecord := logsSlice.At(logsIt)
a := &Alert{}
a.postableAlert.StartsAt = strfmt.DateTime(logRecord.Timestamp().AsTime())
a.postableAlert.EndsAt = strfmt.DateTime(logRecord.ObservedTimestamp().AsTime())
a.postableAlert.GeneratorURL = strfmt.URI(generatorURL.AsString())
a.postableAlert.Labels = models.LabelSet(mapFromAttributes(resourceAttributes, specialLabels))
a.SetSeverity(ParseSeverity(logRecord.SeverityText()))
a.SetName(logRecord.Body().AsString())
attributes := logRecord.Attributes()
a.postableAlert.Annotations = models.LabelSet(mapFromAttributes(attributes, map[string]struct{}{}))
alerts = append(alerts, a)
}
}
}
return alerts
}
// AsLogs returns alert as OTel Logs.
func (a *Alert) AsLogs() plog.Logs {
ld := plog.NewLogs()
resource := ld.ResourceLogs().AppendEmpty()
resourceAttributes := resource.Resource().Attributes()
// Labels in AM are used to identify identical instances of an alert. This corresponds
// with the resource notion in OTLP protocol, which describes the source of a log.
populateAttributesFromMap(resourceAttributes, a.postableAlert.Labels, specialLabels)
resourceAttributes.PutStr(otelconsts.AlertGeneratorURLLabel, string(a.postableAlert.GeneratorURL))
resourceAttributes.PutBool(otelconsts.IsAlertLabel, true)
logRecord := resource.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(time.Time(a.postableAlert.StartsAt)))
logRecord.SetObservedTimestamp(pcommon.NewTimestampFromTime(time.Time(a.postableAlert.EndsAt)))
logRecord.SetSeverityText(a.Severity().String())
pcommon.NewValueStr(a.Name()).CopyTo(logRecord.Body())
attributes := logRecord.Attributes()
populateAttributesFromMap(attributes, a.postableAlert.Annotations, map[string]struct{}{})
return ld
}
func populateAttributesFromMap(attributes pcommon.Map, values map[string]string, ignore map[string]struct{}) {
for k, v := range values {
if _, ok := ignore[k]; ok {
continue
}
attributes.PutStr(k, v)
}
}
func mapFromAttributes(attributes pcommon.Map, ignore map[string]struct{}) map[string]string {
result := map[string]string{}
attributes.Range(func(k string, v pcommon.Value) bool {
if _, exists := ignore[k]; exists {
return true
}
result[k] = v.AsString()
return true
})
return result
}