forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
189 lines (151 loc) · 3.84 KB
/
types.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
package alert
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/influxdata/kapacitor/models"
)
type Event struct {
Topic string
State EventState
Data EventData
NoExternal bool
previousState EventState
}
func (e Event) AlertData() Data {
return Data{
ID: e.State.ID,
Message: e.State.Message,
Details: e.State.Details,
Time: e.State.Time,
Duration: e.State.Duration,
Level: e.State.Level,
Data: e.Data.Result,
PreviousLevel: e.previousState.Level,
Recoverable: e.Data.Recoverable,
}
}
func (e Event) PreviousState() EventState {
return e.previousState
}
func (e Event) TemplateData() TemplateData {
return TemplateData{
ID: e.State.ID,
Message: e.State.Message,
Level: e.State.Level.String(),
Time: e.State.Time,
Duration: e.State.Duration,
Name: e.Data.Name,
TaskName: e.Data.TaskName,
Group: e.Data.Group,
Tags: e.Data.Tags,
Fields: e.Data.Fields,
}
}
type Handler interface {
// Handle is responsible for taking action on the event.
Handle(event Event)
}
type EventState struct {
ID string
Message string
Details string
Time time.Time
Duration time.Duration
Level Level
}
type EventData struct {
// Measurement name
Name string
// TaskName is the name of the task that generated this event.
TaskName string
// Category is the category of the alert that generated this event.
Category string
// Concatenation of all group-by tags of the form [key=value,]+.
// If not groupBy is performed equal to literal 'nil'
Group string
// Map of tags
Tags map[string]string
// Fields of alerting data point.
Fields map[string]interface{}
Recoverable bool
Result models.Result
}
// TemplateData is a structure containing all information available to use in templates for an Event.
type TemplateData struct {
// The ID of the alert.
ID string
// The Message of the Alert
Message string
// Alert Level, one of: INFO, WARNING, CRITICAL.
Level string
// Time the event occurred.
Time time.Time
// Duration of the event
Duration time.Duration
// Measurement name
Name string
// Task name
TaskName string
// Concatenation of all group-by tags of the form [key=value,]+.
// If not groupBy is performed equal to literal 'nil'
Group string
// Map of tags
Tags map[string]string
// Fields of alerting data point.
Fields map[string]interface{}
}
type Level int
const (
OK Level = iota
Info
Warning
Critical
maxLevel
)
const levelStrings = "OKINFOWARNINGCRITICAL"
var levelBytes = []byte(levelStrings)
var levelOffsets = []int{0, 2, 6, 13, 21}
func (l Level) String() string {
if l < maxLevel {
return levelStrings[levelOffsets[l]:levelOffsets[l+1]]
}
return "unknown"
}
func (l Level) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
func (l *Level) UnmarshalText(text []byte) error {
idx := bytes.Index(levelBytes, text)
if idx >= 0 {
for i := 0; i < int(maxLevel); i++ {
if idx == levelOffsets[i] {
*l = Level(i)
return nil
}
}
}
return fmt.Errorf("unknown alert level '%s'", text)
}
func ParseLevel(s string) (l Level, err error) {
err = l.UnmarshalText([]byte(strings.ToUpper(s)))
return
}
type TopicState struct {
Level Level
Collected int64
}
// Data is a structure that contains relevant data about an alert event.
// The structure is intended to be JSON encoded, providing a consistent data format.
type Data struct {
ID string `json:"id"`
Message string `json:"message"`
Details string `json:"details"`
Time time.Time `json:"time"`
Duration time.Duration `json:"duration"`
Level Level `json:"level"`
Data models.Result `json:"data"`
PreviousLevel Level `json:"previousLevel"`
Recoverable bool `json:"recoverable"`
}