forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 12
/
event.go
206 lines (173 loc) · 4.71 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
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
package wavefront
import (
"encoding/json"
"fmt"
"time"
)
// Event represents a single Wavefront Event
type Event struct {
// Name is the name given to the Event
Name string `json:"name"`
// ID is the Wavefront-assigned ID of an existing Event
ID *string `json:"id,omitempty"`
// StartTime is the start time, in epoch milliseconds, of the Event.
// If zero, it will be set to current time
StartTime int64 `json:"startTime"`
// EndTime is the end time, in epoch milliseconds, of the Event
EndTime int64 `json:"endTime,omitempty"`
// Tags are the tags associated with the Event
Tags []string `json:"tags"`
// Severity is the severity category of the Event, can be INFO, WARN,
// SEVERE or UNCLASSIFIED
Severity string
// Type is the type of the Event, e.g. "Alert", "Deploy" etc.
Type string
// Details is a description of the Event
Details string
// Instantaneous, if true, creates a point-in-time Event (i.e. with no duration)
Instantaneous bool `json:"isEphemeral"`
// Annotations on the event
Annotations map[string]string `json:"annotations"`
}
// Events is used to perform event-related operations against the Wavefront API
type Events struct {
// client is the Wavefront client used to perform event-related operations
client Wavefronter
}
const baseEventPath = "/api/v2/event"
// UnmarshalJSON is a custom JSON unmarshaller for an Event, used to explode
// the annotations.
func (e *Event) UnmarshalJSON(b []byte) error {
type event Event
temp := struct {
Annotations map[string]string `json:"annotations"`
*event
}{
event: (*event)(e),
}
if err := json.Unmarshal(b, &temp); err != nil {
return err
}
e.Severity = temp.Annotations["severity"]
e.Type = temp.Annotations["type"]
e.Details = temp.Annotations["details"]
e.Annotations = temp.Annotations
return nil
}
func (e *Event) MarshalJSON() ([]byte, error) {
type event Event
return json.Marshal(&struct {
Annotations map[string]string `json:"annotations"`
*event
}{
Annotations: map[string]string{
"severity": e.Severity,
"details": e.Details,
"type": e.Type,
},
event: (*event)(e),
})
}
// Events is used to return a client for event-related operations
func (c *Client) Events() *Events {
return &Events{client: c}
}
// Find returns all events filtered by the given search conditions.
// If filter is nil then all Events are returned. The result set is limited to
// the first 100 entries. If more results are required the Search type can
// be used directly.
func (e Events) Find(filter []*SearchCondition, timeRange *TimeRange) ([]*Event, error) {
search := &Search{
client: e.client,
Type: "event",
Params: &SearchParams{
Conditions: filter,
TimeRange: timeRange,
},
}
var results []*Event
resp, err := search.Execute()
if err != nil {
return nil, err
}
err = json.Unmarshal(resp.Response.Items, &results)
if err != nil {
return nil, err
}
return results, nil
}
// FindByID returns the Event with the Wavefront-assigned ID.
// If not found an error is returned
func (e Events) FindByID(id string) (*Event, error) {
res, err := e.Find([]*SearchCondition{{
Key: "id",
Value: id,
MatchingMethod: "EXACT",
},
}, nil)
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, fmt.Errorf("no event found with ID %s", id)
}
return res[0], nil
}
// Create is used to create an Event in Wavefront.
// If successful, the ID field of the event will be populated.
func (e Events) Create(event *Event) error {
if event.StartTime == 0 {
event.StartTime = time.Now().Unix() * 1000
}
if event.Instantaneous {
event.EndTime = event.StartTime + 1
}
return doRest(
"POST",
baseEventPath,
e.client,
doPayload(event),
doResponse(event))
}
// Update is used to update an existing Event.
// The ID field of the Event must be populated
func (e Events) Update(event *Event) error {
if event.ID == nil {
return fmt.Errorf("event id field not set")
}
return doRest(
"PUT",
fmt.Sprintf("%s/%s", baseEventPath, *event.ID),
e.client,
doPayload(event),
doResponse(event))
}
// Close is used to close an existing Event
func (e Events) Close(event *Event) error {
if event.ID == nil {
return fmt.Errorf("event id field not set")
}
return doRest(
"POST",
fmt.Sprintf("%s/%s/close", baseEventPath, *event.ID),
e.client,
doResponse(event),
)
}
// Delete is used to delete an existing Event.
// The ID field of the Event must be populated
func (e Events) Delete(event *Event) error {
if event.ID == nil {
return fmt.Errorf("event id field not set")
}
err := doRest(
"DELETE",
fmt.Sprintf("%s/%s", baseEventPath, *event.ID),
e.client)
if err != nil {
return err
}
//reset the ID field so deletion is not attempted again
event.ID = nil
return nil
}