This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
/
events.go
176 lines (162 loc) · 4.57 KB
/
events.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
package msg
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
)
var errTooSmall = errors.New("too small")
var errFmtBinWriteFailed = "binary write failed: %q"
var errFmtUnknownFormat = "unknown format %d"
var errInvalidEvent = errors.New("invalid event definition")
var errFmtInvalidSeverity = "invalid severity level %q"
type Format uint8
const (
FormatProbeEventJson Format = iota
FormatProbeEventMsgp
FormatProbeEventsMsgp
)
//go:generate msgp
type ProbeEvent struct {
Id string `json:"id"`
EventType string `json:"event_type"`
OrgId int64 `json:"org_id"`
Severity string `json:"severity"` // enum "INFO" "WARN" "ERROR" "OK"
Source string `json:"source"`
Timestamp int64 `json:"timestamp"`
Message string `json:"message"`
Tags map[string]string `json:"tags"`
}
type ProbeEvents []*ProbeEvent
func (e *ProbeEvent) Validate() error {
if e.EventType == "" || e.OrgId == 0 || e.Source == "" || e.Timestamp == 0 || e.Message == "" {
return errInvalidEvent
}
switch strings.ToLower(e.Severity) {
case "info", "ok", "warn", "error", "warning", "critical":
// nop
default:
return fmt.Errorf(errFmtInvalidSeverity, e.Severity)
}
return nil
}
type ProbeEventJson struct {
Id string `json:"id"`
EventType string `json:"event_type"`
OrgId int64 `json:"org_id"`
Severity string `json:"severity"`
Source string `json:"source"`
Timestamp int64 `json:"timestamp"`
Message string `json:"message"`
Tags []string `json:"tags"`
}
// Decode message into probeEvent.
// The message Format is:
//
// Bytes: Description
// 0 : messgae format
// 1-9 : transmit timestamp 64bit Nanosecond Epoch TS (not used)
// 10-> : message body
func ProbeEventsFromMsg(msg []byte) ([]*ProbeEvent, error) {
if len(msg) < 9 {
return nil, errTooSmall
}
format := Format(msg[0])
if format != FormatProbeEventJson && format != FormatProbeEventMsgp && format != FormatProbeEventsMsgp {
return nil, fmt.Errorf(errFmtUnknownFormat, format)
}
events := ProbeEvents(make([]*ProbeEvent, 0))
switch format {
case FormatProbeEventJson:
oldFormat := &ProbeEventJson{}
err := json.Unmarshal(msg[9:], oldFormat)
if err != nil {
return nil, err
}
//convert our []string of key:valy pairs to
// map[string]string
tags := make(map[string]string)
for _, t := range oldFormat.Tags {
parts := strings.SplitN(t, ":", 2)
tags[parts[0]] = parts[1]
}
events = append(events, &ProbeEvent{
Id: oldFormat.Id,
EventType: oldFormat.EventType,
OrgId: oldFormat.OrgId,
Severity: oldFormat.Severity,
Source: oldFormat.Source,
Timestamp: oldFormat.Timestamp,
Message: oldFormat.Message,
Tags: tags,
})
case FormatProbeEventMsgp:
e := new(ProbeEvent)
_, err := e.UnmarshalMsg(msg[9:])
if err != nil {
return nil, err
}
events = append(events, e)
case FormatProbeEventsMsgp:
_, err := events.UnmarshalMsg(msg[9:])
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf(errFmtUnknownFormat, msg[0])
}
return events, nil
}
func CreateProbeEventMsg(event *ProbeEvent, id int64, version Format) ([]byte, error) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, uint8(version))
if err != nil {
return nil, fmt.Errorf(errFmtBinWriteFailed, err)
}
err = binary.Write(buf, binary.BigEndian, id)
if err != nil {
return nil, fmt.Errorf(errFmtBinWriteFailed, err)
}
var msg []byte
switch version {
case FormatProbeEventJson:
msg, err = json.Marshal(event)
case FormatProbeEventMsgp:
msg, err = event.MarshalMsg(nil)
default:
return nil, fmt.Errorf(errFmtUnknownFormat, version)
}
if err != nil {
return nil, fmt.Errorf("Failed to marshal metrics payload: %s", err)
}
_, err = buf.Write(msg)
if err != nil {
return nil, fmt.Errorf(errFmtBinWriteFailed, err)
}
return buf.Bytes(), nil
}
func CreateProbeEventsMsg(events []*ProbeEvent) ([]byte, error) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, uint8(FormatProbeEventsMsgp))
if err != nil {
return nil, fmt.Errorf(errFmtBinWriteFailed, err)
}
id := time.Now().UnixNano()
err = binary.Write(buf, binary.BigEndian, id)
if err != nil {
return nil, fmt.Errorf(errFmtBinWriteFailed, err)
}
var msg []byte
msg, err = ProbeEvents(events).MarshalMsg(nil)
if err != nil {
return nil, fmt.Errorf("Failed to marshal metrics payload: %s", err)
}
_, err = buf.Write(msg)
if err != nil {
return nil, fmt.Errorf(errFmtBinWriteFailed, err)
}
return buf.Bytes(), nil
}