-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
event.go
93 lines (82 loc) · 2.94 KB
/
event.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//nolint:revive // TODO(AML) Fix revive linter
package event
import (
"encoding/json"
"fmt"
)
// EventPriority represents the priority of an event
//
//nolint:revive // TODO(AML) Fix revive linter
type EventPriority string
// Enumeration of the existing event priorities, and their values
const (
EventPriorityNormal EventPriority = "normal"
EventPriorityLow EventPriority = "low"
)
// GetEventPriorityFromString returns the EventPriority from its string representation
func GetEventPriorityFromString(val string) (EventPriority, error) {
switch val {
case string(EventPriorityNormal):
return EventPriorityNormal, nil
case string(EventPriorityLow):
return EventPriorityLow, nil
default:
return "", fmt.Errorf("Invalid event priority: '%s'", val)
}
}
// EventAlertType represents the alert type of an event
//
//nolint:revive // TODO(AML) Fix revive linter
type EventAlertType string
// Enumeration of the existing event alert types, and their values
const (
EventAlertTypeError EventAlertType = "error"
EventAlertTypeWarning EventAlertType = "warning"
EventAlertTypeInfo EventAlertType = "info"
EventAlertTypeSuccess EventAlertType = "success"
)
// GetAlertTypeFromString returns the EventAlertType from its string representation
func GetAlertTypeFromString(val string) (EventAlertType, error) {
switch val {
case string(EventAlertTypeError):
return EventAlertTypeError, nil
case string(EventAlertTypeWarning):
return EventAlertTypeWarning, nil
case string(EventAlertTypeInfo):
return EventAlertTypeInfo, nil
case string(EventAlertTypeSuccess):
return EventAlertTypeSuccess, nil
default:
return EventAlertTypeInfo, fmt.Errorf("Invalid alert type: '%s'", val)
}
}
// Event holds an event (w/ serialization to DD agent 5 intake format)
type Event struct {
Title string `json:"msg_title"`
Text string `json:"msg_text"`
Ts int64 `json:"timestamp"`
Priority EventPriority `json:"priority,omitempty"`
Host string `json:"host"`
Tags []string `json:"tags,omitempty"`
AlertType EventAlertType `json:"alert_type,omitempty"`
AggregationKey string `json:"aggregation_key,omitempty"`
SourceTypeName string `json:"source_type_name,omitempty"`
EventType string `json:"event_type,omitempty"`
OriginFromUDS string `json:"-"`
OriginFromClient string `json:"-"`
Cardinality string `json:"-"`
}
// Return a JSON string or "" in case of error during the Marshaling
func (e *Event) String() string {
s, err := json.Marshal(e)
if err != nil {
return ""
}
return string(s)
}
// Events is a collection of Event.
type Events []*Event