-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
80 lines (66 loc) · 1.7 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
package hec
import (
"fmt"
"time"
)
type Event struct {
Host *string `json:"host,omitempty"`
Index *string `json:"index,omitempty"`
Source *string `json:"source,omitempty"`
SourceType *string `json:"sourcetype,omitempty"`
Time *string `json:"time,omitempty"`
Fields map[string]interface{} `json:"fields,omitempty"`
Event interface{} `json:"event"`
}
func NewEvent(data interface{}) *Event {
// Empty event is not allowed, but let HEC complain the error
switch data.(type) {
case *string:
return &Event{Event: *data.(*string)}
case string:
return &Event{Event: data.(string)}
default:
return &Event{Event: data}
}
}
func (e *Event) SetHost(host string) {
e.Host = &host
}
func (e *Event) SetIndex(index string) {
e.Index = &index
}
func (e *Event) SetSourceType(sourcetype string) {
e.SourceType = &sourcetype
}
func (e *Event) SetSource(source string) {
e.Source = &source
}
func (e *Event) SetTime(time time.Time) {
e.Time = String(epochTime(&time))
}
func (e *Event) SetFields(fields map[string]interface{}) {
e.Fields = fields
}
func (e *Event) SetField(fieldName string, val interface{}) {
if e.Fields == nil {
e.Fields = make(map[string]interface{})
}
e.Fields[fieldName] = val
}
func (e *Event) empty() bool {
switch e.Event.(type) {
case *string:
return e.Event.(*string) == nil || *e.Event.(*string) == ""
case string:
return e.Event.(string) == ""
default:
return e.Event == nil
}
}
func epochTime(t *time.Time) string {
millis := t.UnixNano() / 1000000
return fmt.Sprintf("%d.%03d", millis/1000, millis%1000)
}
func String(str string) *string {
return &str
}