-
Notifications
You must be signed in to change notification settings - Fork 19
/
message.go
507 lines (480 loc) · 13.9 KB
/
message.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package process
// message.go is a stripped down version of the backend message processing
// with support for the same MessageVersion and MessageEncoding but with
// only a limited set of message types.
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"strconv"
"github.com/DataDog/zstd"
"github.com/DataDog/zstd_0"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
)
// MessageEncoding represents how messages will be encoded or decoded for
// over-the-wire transfer. Protobuf should be used for server-side messages
// (e.g. from collector <-> server) and JSON should be used for client-side.
type MessageEncoding uint8
// Message encoding constants.
const (
MessageEncodingProtobuf MessageEncoding = 0
MessageEncodingJSON MessageEncoding = 1
MessageEncodingZstdPB MessageEncoding = 2
_ MessageEncoding = 3 // This is unused
MessageEncodingZstd1xPB MessageEncoding = 4
)
// MessageVersion is the version of the message. It should always be the first
// byte in the encoded version.
type MessageVersion uint8
// Message versioning constants.
const (
MessageV1 MessageVersion = 1
MessageV2 = 2
MessageV3 = 3
)
// MessageHeader is attached to all messages at the head of the message. Some
// fields are added in later versions so make sure you're only using fields that
// are available in the defined Version.
type MessageHeader struct {
Version MessageVersion
Encoding MessageEncoding
Type MessageType
SubscriptionID uint8 // Unused in Agent
OrgID int32 // Unused in Agent
Timestamp int64
}
func unmarshal(enc MessageEncoding, body []byte, m proto.Message) error {
switch enc {
case MessageEncodingProtobuf:
return proto.Unmarshal(body, m)
case MessageEncodingJSON:
return jsonpb.Unmarshal(bytes.NewReader(body), m)
case MessageEncodingZstdPB, MessageEncodingZstd1xPB:
var d []byte
var err error
if enc == MessageEncodingZstd1xPB {
d, err = zstd.Decompress(nil, body)
} else {
d, err = zstd_0.Decompress(nil, body)
}
if err != nil {
return err
}
return proto.Unmarshal(d, m)
}
return fmt.Errorf("unknown message encoding: %d", enc)
}
// MessageType is a string representing the type of a message.
type MessageType uint8
// Message type constants for MessageType.
// Note: Ordering my seem unusual, this is just to match the backend where there
// are additional types that aren't covered here.
const (
TypeCollectorProc = 12
TypeCollectorConnections = 22
TypeResCollector = 23
TypeCollectorRealTime = 27
TypeCollectorContainer = 39
TypeCollectorContainerRealTime = 40
TypeCollectorPod = 41
TypeCollectorReplicaSet = 42
TypeCollectorDeployment = 43
TypeCollectorService = 44
TypeCollectorNode = 45
TypeCollectorCluster = 46
TypeCollectorJob = 47
TypeCollectorCronJob = 48
TypeCollectorDaemonSet = 49
TypeCollectorStatefulSet = 50
TypeCollectorPersistentVolume = 51
TypeCollectorPersistentVolumeClaim = 52
TypeCollectorProcDiscovery = 53
TypeCollectorRole = 54
TypeCollectorRoleBinding = 55
TypeCollectorClusterRole = 56
TypeCollectorClusterRoleBinding = 57
TypeCollectorServiceAccount = 58
TypeCollectorIngress = 59
TypeCollectorProcEvent = 60
TypeCollectorManifest = 80
)
func (m MessageType) String() string {
switch m {
case TypeCollectorProc:
return "process"
case TypeCollectorConnections:
return "network"
case TypeCollectorRealTime:
return "process-rt"
case TypeCollectorContainer:
return "container"
case TypeCollectorContainerRealTime:
return "container-rt"
case TypeCollectorPod:
return "pod"
case TypeCollectorReplicaSet:
return "replica-set"
case TypeCollectorDeployment:
return "deployment"
case TypeCollectorService:
return "service"
case TypeCollectorNode:
return "node"
case TypeCollectorCluster:
return "cluster"
case TypeCollectorJob:
return "job"
case TypeCollectorCronJob:
return "cron-job"
case TypeCollectorDaemonSet:
return "daemon-set"
case TypeCollectorStatefulSet:
return "stateful-set"
case TypeCollectorPersistentVolume:
return "persistent-volume"
case TypeCollectorPersistentVolumeClaim:
return "persistent-volume-claim"
case TypeCollectorProcDiscovery:
return "process-discovery"
case TypeCollectorRole:
return "role"
case TypeCollectorRoleBinding:
return "role-binding"
case TypeCollectorClusterRole:
return "cluster-role"
case TypeCollectorClusterRoleBinding:
return "cluster-role-binding"
case TypeCollectorServiceAccount:
return "service-account"
case TypeCollectorIngress:
return "ingress"
case TypeCollectorProcEvent:
return "process-event"
case TypeCollectorManifest:
return "manifest"
default:
// otherwise convert the type identifier
return strconv.Itoa(int(m))
}
}
// Message is a generic type for all messages with a Header and Body.
type Message struct {
Header MessageHeader
Body MessageBody
}
// MessageBody is a common interface used by all message types.
type MessageBody interface {
ProtoMessage()
Reset()
String() string
Size() int
}
// DecodeMessage decodes raw message bytes into a specific type that satisfies
// the Message interface. If we can't decode, an error is returned.
func DecodeMessage(data []byte) (Message, error) {
header, offset, err := ReadHeader(data)
if err != nil {
return Message{}, err
}
body := data[offset:]
var m MessageBody
switch header.Type {
case TypeCollectorProc:
m = &CollectorProc{}
case TypeCollectorConnections:
m = &CollectorConnections{}
case TypeCollectorRealTime:
m = &CollectorRealTime{}
case TypeResCollector:
m = &ResCollector{}
case TypeCollectorContainer:
m = &CollectorContainer{}
case TypeCollectorContainerRealTime:
m = &CollectorContainerRealTime{}
case TypeCollectorPod:
m = &CollectorPod{}
case TypeCollectorReplicaSet:
m = &CollectorReplicaSet{}
case TypeCollectorDeployment:
m = &CollectorDeployment{}
case TypeCollectorService:
m = &CollectorService{}
case TypeCollectorNode:
m = &CollectorNode{}
case TypeCollectorCluster:
m = &CollectorCluster{}
case TypeCollectorJob:
m = &CollectorJob{}
case TypeCollectorCronJob:
m = &CollectorCronJob{}
case TypeCollectorDaemonSet:
m = &CollectorDaemonSet{}
case TypeCollectorStatefulSet:
m = &CollectorStatefulSet{}
case TypeCollectorPersistentVolume:
m = &CollectorPersistentVolume{}
case TypeCollectorPersistentVolumeClaim:
m = &CollectorPersistentVolumeClaim{}
case TypeCollectorProcDiscovery:
m = &CollectorProcDiscovery{}
case TypeCollectorRole:
m = &CollectorRole{}
case TypeCollectorRoleBinding:
m = &CollectorRoleBinding{}
case TypeCollectorClusterRole:
m = &CollectorClusterRole{}
case TypeCollectorClusterRoleBinding:
m = &CollectorClusterRoleBinding{}
case TypeCollectorServiceAccount:
m = &CollectorServiceAccount{}
case TypeCollectorIngress:
m = &CollectorIngress{}
case TypeCollectorProcEvent:
m = &CollectorProcEvent{}
case TypeCollectorManifest:
m = &CollectorManifest{}
default:
return Message{}, fmt.Errorf("unhandled message type: %d", header.Type)
}
if err = unmarshal(header.Encoding, body, m); err != nil {
return Message{}, err
}
return Message{header, m}, nil
}
// DetectMessageType returns the message type for the given MessageBody
func DetectMessageType(b MessageBody) (MessageType, error) {
var t MessageType
switch b.(type) {
case *CollectorProc:
t = TypeCollectorProc
case *CollectorConnections:
t = TypeCollectorConnections
case *CollectorRealTime:
t = TypeCollectorRealTime
case *ResCollector:
t = TypeResCollector
case *CollectorContainer:
t = TypeCollectorContainer
case *CollectorContainerRealTime:
t = TypeCollectorContainerRealTime
case *CollectorPod:
t = TypeCollectorPod
case *CollectorReplicaSet:
t = TypeCollectorReplicaSet
case *CollectorDeployment:
t = TypeCollectorDeployment
case *CollectorService:
t = TypeCollectorService
case *CollectorNode:
t = TypeCollectorNode
case *CollectorManifest:
t = TypeCollectorManifest
case *CollectorCluster:
t = TypeCollectorCluster
case *CollectorJob:
t = TypeCollectorJob
case *CollectorCronJob:
t = TypeCollectorCronJob
case *CollectorDaemonSet:
t = TypeCollectorDaemonSet
case *CollectorStatefulSet:
t = TypeCollectorStatefulSet
case *CollectorPersistentVolume:
t = TypeCollectorPersistentVolume
case *CollectorPersistentVolumeClaim:
t = TypeCollectorPersistentVolumeClaim
case *CollectorProcDiscovery:
t = TypeCollectorProcDiscovery
case *CollectorRole:
t = TypeCollectorRole
case *CollectorRoleBinding:
t = TypeCollectorRoleBinding
case *CollectorClusterRole:
t = TypeCollectorClusterRole
case *CollectorClusterRoleBinding:
t = TypeCollectorClusterRoleBinding
case *CollectorServiceAccount:
t = TypeCollectorServiceAccount
case *CollectorIngress:
t = TypeCollectorIngress
case *CollectorProcEvent:
t = TypeCollectorProcEvent
default:
return 0, fmt.Errorf("unknown message body type: %s", reflect.TypeOf(b))
}
return t, nil
}
// EncodeMessage encodes a message object into bytes with protobuf. A type
// header is added for ease of decoding.
func EncodeMessage(m Message) ([]byte, error) {
hb, err := encodeHeader(m.Header)
if err != nil {
return nil, fmt.Errorf("could not encode header: %s", err)
}
b := new(bytes.Buffer)
if _, err := b.Write(hb); err != nil {
return nil, err
}
var p []byte
switch m.Header.Encoding {
case MessageEncodingProtobuf:
p, err = proto.Marshal(m.Body)
if err != nil {
return nil, err
}
case MessageEncodingJSON:
marshaler := jsonpb.Marshaler{EmitDefaults: true}
s, err := marshaler.MarshalToString(m.Body)
if err != nil {
return nil, err
}
p = []byte(s)
case MessageEncodingZstdPB, MessageEncodingZstd1xPB:
pb, err := proto.Marshal(m.Body)
if err != nil {
return nil, err
}
if m.Header.Encoding == MessageEncodingZstd1xPB {
p, err = zstd.Compress(nil, pb)
} else {
p, err = zstd_0.Compress(nil, pb)
}
default:
return nil, fmt.Errorf("unknown message encoding: %d", m.Header.Encoding)
}
_, err = b.Write(p)
return b.Bytes(), err
}
// ReadHeader reads the header off raw message bytes.
func ReadHeader(data []byte) (MessageHeader, int, error) {
if len(data) <= 4 {
return MessageHeader{}, 0, fmt.Errorf("invalid message length: %d", len(data))
}
switch MessageVersion(uint8(data[0])) {
case MessageV1:
return readHeaderV1(data)
case MessageV2:
return readHeaderV2(data)
case MessageV3:
return readHeaderV3(data)
default:
return MessageHeader{}, 0, fmt.Errorf("invalid message version: %d", uint8(data[0]))
}
}
func readHeaderV1(data []byte) (MessageHeader, int, error) {
b := bytes.NewBuffer(data[1:])
var msgEnc uint8
if err := binary.Read(b, binary.LittleEndian, &msgEnc); err != nil {
return MessageHeader{}, 0, err
}
var msgType uint8
if err := binary.Read(b, binary.LittleEndian, &msgType); err != nil {
return MessageHeader{}, 0, err
}
var subID uint8
if err := binary.Read(b, binary.LittleEndian, &subID); err != nil {
return MessageHeader{}, 0, err
}
return MessageHeader{
Version: MessageV1,
Encoding: MessageEncoding(msgEnc),
Type: MessageType(msgType),
SubscriptionID: subID,
OrgID: 0,
}, 4, nil
}
func readHeaderV2(data []byte) (MessageHeader, int, error) {
b := bytes.NewBuffer(data[1:])
var msgEnc uint8
if err := binary.Read(b, binary.LittleEndian, &msgEnc); err != nil {
return MessageHeader{}, 0, err
}
var msgType uint8
if err := binary.Read(b, binary.LittleEndian, &msgType); err != nil {
return MessageHeader{}, 0, err
}
var subID uint8
if err := binary.Read(b, binary.LittleEndian, &subID); err != nil {
return MessageHeader{}, 0, err
}
var orgID int32
if err := binary.Read(b, binary.LittleEndian, &orgID); err != nil {
return MessageHeader{}, 0, err
}
return MessageHeader{
Version: MessageV2,
Encoding: MessageEncoding(msgEnc),
Type: MessageType(msgType),
SubscriptionID: subID,
OrgID: orgID,
}, 8, nil
}
func readHeaderV3(data []byte) (MessageHeader, int, error) {
b := bytes.NewBuffer(data[1:])
var msgEnc uint8
if err := binary.Read(b, binary.LittleEndian, &msgEnc); err != nil {
return MessageHeader{}, 0, err
}
var msgType uint8
if err := binary.Read(b, binary.LittleEndian, &msgType); err != nil {
return MessageHeader{}, 0, err
}
var subID uint8
if err := binary.Read(b, binary.LittleEndian, &subID); err != nil {
return MessageHeader{}, 0, err
}
var orgID int32
if err := binary.Read(b, binary.LittleEndian, &orgID); err != nil {
return MessageHeader{}, 0, err
}
var timestamp int64
if err := binary.Read(b, binary.LittleEndian, ×tamp); err != nil {
return MessageHeader{}, 0, err
}
return MessageHeader{
Version: MessageV3,
Encoding: MessageEncoding(msgEnc),
Type: MessageType(msgType),
SubscriptionID: subID,
OrgID: orgID,
Timestamp: timestamp,
}, 16, nil
}
func encodeHeader(h MessageHeader) ([]byte, error) {
switch h.Version {
case MessageV3:
return encodeHeaderV3(h)
default:
return nil, fmt.Errorf("invalid message version: %d", h.Version)
}
}
func encodeHeaderV3(h MessageHeader) ([]byte, error) {
b := new(bytes.Buffer)
err := binary.Write(b, binary.LittleEndian, uint8(h.Version))
if err != nil {
return nil, err
}
err = binary.Write(b, binary.LittleEndian, uint8(h.Encoding))
if err != nil {
return nil, err
}
err = binary.Write(b, binary.LittleEndian, uint8(h.Type))
if err != nil {
return nil, err
}
err = binary.Write(b, binary.LittleEndian, uint8(h.SubscriptionID))
if err != nil {
return nil, err
}
err = binary.Write(b, binary.LittleEndian, h.OrgID)
if err != nil {
return nil, err
}
err = binary.Write(b, binary.LittleEndian, h.Timestamp)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}