forked from grafana/grafana
/
msg.go
202 lines (186 loc) · 5.12 KB
/
msg.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
package msg
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/codeskyblue/go-uuid"
"github.com/raintank/raintank-metric/schema"
)
type MetricData struct {
Id int64
Metrics []*schema.MetricData
Produced time.Time
Format Format
Msg []byte
}
type ProbeEvent struct {
Id int64
Produced time.Time
Event *schema.ProbeEvent
Format Format
Msg []byte
}
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"`
}
// parses format and id (cheap), but doesn't decode metrics (expensive) just yet.
func MetricDataFromMsg(msg []byte) (MetricData, error) {
m := MetricData{
Metrics: make([]*schema.MetricData, 0),
Msg: msg,
}
if len(msg) < 9 {
return m, errors.New("msg too small")
}
buf := bytes.NewReader(msg[1:9])
binary.Read(buf, binary.BigEndian, &m.Id)
m.Produced = time.Unix(0, m.Id)
format := Format(msg[0])
if format != FormatMetricDataArrayJson && format != FormatMetricDataArrayMsgp {
return m, errors.New("unknown format")
}
m.Format = format
return m, nil
}
func (m *MetricData) DecodeMetricData() error {
var err error
switch m.Format {
case FormatMetricDataArrayJson:
err = json.Unmarshal(m.Msg[9:], &m.Metrics)
case FormatMetricDataArrayMsgp:
var out schema.MetricDataArray
_, err = out.UnmarshalMsg(m.Msg[9:])
m.Metrics = []*schema.MetricData(out)
default:
return fmt.Errorf("unrecognized format %d", m.Msg[0])
}
if err != nil {
return fmt.Errorf("ERROR: failure to unmarshal message body via format %q: %s", m.Format, err)
}
return nil
}
func CreateMsg(metrics []*schema.MetricData, 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("binary.Write failed: %s", err.Error())
}
err = binary.Write(buf, binary.BigEndian, id)
if err != nil {
return nil, fmt.Errorf("binary.Write failed: %s", err.Error())
}
var msg []byte
switch version {
case FormatMetricDataArrayJson:
msg, err = json.Marshal(metrics)
case FormatMetricDataArrayMsgp:
m := schema.MetricDataArray(metrics)
msg, err = m.MarshalMsg(nil)
default:
return nil, errors.New("unsupported 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("buf.Write failed: %s", err.Error())
}
return buf.Bytes(), nil
}
func ProbeEventFromMsg(msg []byte) (*ProbeEvent, error) {
e := &ProbeEvent{
Event: &schema.ProbeEvent{},
Msg: msg,
}
if len(msg) < 9 {
return e, errors.New("msg too small")
}
buf := bytes.NewReader(msg[1:9])
binary.Read(buf, binary.BigEndian, &e.Id)
e.Produced = time.Unix(0, e.Id)
format := Format(msg[0])
if format != FormatProbeEventJson && format != FormatProbeEventMsgp {
return e, errors.New("unknown format")
}
e.Format = format
return e, nil
}
func (e *ProbeEvent) DecodeProbeEvent() error {
var err error
switch e.Format {
case FormatProbeEventJson:
oldFormat := &ProbeEventJson{}
err = json.Unmarshal(e.Msg[9:], oldFormat)
//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]
}
e.Event = &schema.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:
_, err = e.Event.UnmarshalMsg(e.Msg[9:])
default:
return fmt.Errorf("unrecognized format %d", e.Msg[0])
}
if err != nil {
return fmt.Errorf("ERROR: failure to unmarshal message body via format %q: %s", e.Format, err)
}
return nil
}
func CreateProbeEventMsg(event *schema.ProbeEvent, id int64, version Format) ([]byte, error) {
if event.Id == "" {
// per http://blog.mikemccandless.com/2014/05/choosing-fast-unique-identifier-uuid.html,
// using V1 UUIDs is much faster than v4 like we were using
u := uuid.NewUUID()
event.Id = u.String()
}
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, uint8(version))
if err != nil {
return nil, fmt.Errorf("binary.Write failed: %s", err.Error())
}
err = binary.Write(buf, binary.BigEndian, id)
if err != nil {
return nil, fmt.Errorf("binary.Write failed: %s", err.Error())
}
var msg []byte
switch version {
case FormatProbeEventJson:
msg, err = json.Marshal(event)
case FormatProbeEventMsgp:
msg, err = event.MarshalMsg(nil)
default:
return nil, errors.New("unsupported 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("buf.Write failed: %s", err.Error())
}
return buf.Bytes(), nil
}