-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathconversion.go
428 lines (365 loc) · 9.38 KB
/
conversion.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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fluentforwardreceiver
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"time"
"github.com/tinylib/msgp/msgp"
"go.opentelemetry.io/collector/model/pdata"
)
const tagAttributeKey = "fluent.tag"
// Most of this logic is derived directly from
// https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1,
// which describes the fields in much greater detail.
type Event interface {
DecodeMsg(dc *msgp.Reader) error
LogRecords() pdata.LogSlice
Chunk() string
Compressed() string
}
type OptionsMap map[string]interface{}
// Chunk returns the `chunk` option or blank string if it was not set.
func (om OptionsMap) Chunk() string {
c, _ := om["chunk"].(string)
return c
}
func (om OptionsMap) Compressed() string {
compressed, _ := om["compressed"].(string)
return compressed
}
type EventMode int
type Peeker interface {
Peek(n int) ([]byte, error)
}
// Values for enum EventMode.
const (
UnknownMode EventMode = iota
MessageMode
ForwardMode
PackedForwardMode
)
func (em EventMode) String() string {
switch em {
case UnknownMode:
return "unknown"
case MessageMode:
return "message"
case ForwardMode:
return "forward"
case PackedForwardMode:
return "packedforward"
default:
panic("programmer bug")
}
}
func insertToAttributeMap(key string, val interface{}, dest *pdata.AttributeMap) {
// See https://github.com/tinylib/msgp/wiki/Type-Mapping-Rules
switch r := val.(type) {
case bool:
dest.InsertBool(key, r)
case string:
dest.InsertString(key, r)
case uint64:
dest.InsertInt(key, int64(r))
case int64:
dest.InsertInt(key, r)
case []byte:
dest.InsertString(key, string(r))
case map[string]interface{}, []interface{}:
encoded, err := json.Marshal(r)
if err != nil {
dest.InsertString(key, err.Error())
}
dest.InsertString(key, string(encoded))
case float32:
dest.InsertDouble(key, float64(r))
case float64:
dest.InsertDouble(key, r)
default:
dest.InsertString(key, fmt.Sprintf("%v", r))
}
}
func timeFromTimestamp(ts interface{}) (time.Time, error) {
switch v := ts.(type) {
case int64:
return time.Unix(v, 0), nil
case *eventTimeExt:
return time.Time(*v), nil
default:
return time.Time{}, fmt.Errorf("unknown type of value: %v", ts)
}
}
func decodeTimestampToLogRecord(dc *msgp.Reader, lr pdata.LogRecord) error {
tsIntf, err := dc.ReadIntf()
if err != nil {
return msgp.WrapError(err, "Time")
}
ts, err := timeFromTimestamp(tsIntf)
if err != nil {
return msgp.WrapError(err, "Time")
}
lr.SetTimestamp(pdata.NewTimestampFromTime(ts))
return nil
}
func parseRecordToLogRecord(dc *msgp.Reader, lr pdata.LogRecord) error {
attrs := lr.Attributes()
recordLen, err := dc.ReadMapHeader()
if err != nil {
return msgp.WrapError(err, "Record")
}
for recordLen > 0 {
recordLen--
key, err := dc.ReadString()
if err != nil {
// The protocol doesn't specify this but apparently some map keys
// can be binary type instead of string
keyBytes, keyBytesErr := dc.ReadBytes(nil)
if keyBytesErr != nil {
return msgp.WrapError(keyBytesErr, "Record")
}
key = string(keyBytes)
}
val, err := dc.ReadIntf()
if err != nil {
return msgp.WrapError(err, "Record", key)
}
// fluentd uses message, fluentbit log.
if key == "message" || key == "log" {
switch v := val.(type) {
case string:
lr.Body().SetStringVal(v)
case []uint8:
// Sometimes strings come in as uint8's.
lr.Body().SetStringVal(string(v))
default:
return fmt.Errorf("cannot convert message type %T to string", val)
}
} else {
insertToAttributeMap(key, val, &attrs)
}
}
return nil
}
type MessageEventLogRecord struct {
pdata.LogSlice
OptionsMap
}
func (melr *MessageEventLogRecord) LogRecords() pdata.LogSlice {
return melr.LogSlice
}
func (melr *MessageEventLogRecord) DecodeMsg(dc *msgp.Reader) error {
melr.LogSlice = pdata.NewLogSlice()
log := melr.LogSlice.AppendEmpty()
var arrLen uint32
var err error
arrLen, err = dc.ReadArrayHeader()
if err != nil {
return msgp.WrapError(err)
}
if arrLen > 4 || arrLen < 3 {
return msgp.ArrayError{Wanted: 3, Got: arrLen}
}
tag, err := dc.ReadString()
if err != nil {
return msgp.WrapError(err, "Tag")
}
attrs := log.Attributes()
attrs.InsertString(tagAttributeKey, tag)
err = decodeTimestampToLogRecord(dc, log)
if err != nil {
return msgp.WrapError(err, "Time")
}
err = parseRecordToLogRecord(dc, log)
if err != nil {
return err
}
if arrLen == 4 {
melr.OptionsMap, err = parseOptions(dc)
if err != nil {
return err
}
}
return nil
}
func parseOptions(dc *msgp.Reader) (OptionsMap, error) {
var optionLen uint32
optionLen, err := dc.ReadMapHeader()
if err != nil {
return nil, msgp.WrapError(err, "Option")
}
out := make(OptionsMap, optionLen)
for optionLen > 0 {
optionLen--
key, err := dc.ReadString()
if err != nil {
return nil, msgp.WrapError(err, "Option")
}
val, err := dc.ReadIntf()
if err != nil {
return nil, msgp.WrapError(err, "Option", key)
}
out[key] = val
}
return out, nil
}
type ForwardEventLogRecords struct {
pdata.LogSlice
OptionsMap
}
func (fe *ForwardEventLogRecords) LogRecords() pdata.LogSlice {
return fe.LogSlice
}
func (fe *ForwardEventLogRecords) DecodeMsg(dc *msgp.Reader) (err error) {
fe.LogSlice = pdata.NewLogSlice()
var arrLen uint32
arrLen, err = dc.ReadArrayHeader()
if err != nil {
err = msgp.WrapError(err)
return
}
if arrLen < 2 || arrLen > 3 {
err = msgp.ArrayError{Wanted: 2, Got: arrLen}
return
}
tag, err := dc.ReadString()
if err != nil {
return msgp.WrapError(err, "Tag")
}
entryLen, err := dc.ReadArrayHeader()
if err != nil {
err = msgp.WrapError(err, "Record")
return
}
fe.LogSlice.EnsureCapacity(int(entryLen))
for i := 0; i < int(entryLen); i++ {
lr := fe.LogSlice.AppendEmpty()
err = parseEntryToLogRecord(dc, lr)
if err != nil {
return msgp.WrapError(err, "Entries", i)
}
fe.LogSlice.At(i).Attributes().InsertString(tagAttributeKey, tag)
}
if arrLen == 3 {
fe.OptionsMap, err = parseOptions(dc)
if err != nil {
return err
}
}
return
}
func parseEntryToLogRecord(dc *msgp.Reader, lr pdata.LogRecord) error {
arrLen, err := dc.ReadArrayHeader()
if err != nil {
return msgp.WrapError(err)
}
if arrLen != 2 {
return msgp.ArrayError{Wanted: 2, Got: arrLen}
}
err = decodeTimestampToLogRecord(dc, lr)
if err != nil {
return msgp.WrapError(err, "Time")
}
return parseRecordToLogRecord(dc, lr)
}
type PackedForwardEventLogRecords struct {
pdata.LogSlice
OptionsMap
}
func (pfe *PackedForwardEventLogRecords) LogRecords() pdata.LogSlice {
return pfe.LogSlice
}
// DecodeMsg implements msgp.Decodable. This was originally code generated but
// then manually copied here in order to handle the optional Options field.
func (pfe *PackedForwardEventLogRecords) DecodeMsg(dc *msgp.Reader) error {
pfe.LogSlice = pdata.NewLogSlice()
arrLen, err := dc.ReadArrayHeader()
if err != nil {
return msgp.WrapError(err)
}
if arrLen < 2 || arrLen > 3 {
return msgp.ArrayError{Wanted: 2, Got: arrLen}
}
tag, err := dc.ReadString()
if err != nil {
return msgp.WrapError(err, "Tag")
}
entriesFirstByte, err := dc.R.Peek(1)
if err != nil {
return msgp.WrapError(err, "EntriesRaw")
}
entriesType := msgp.NextType(entriesFirstByte)
// We have to read out the entries raw all the way first because we don't
// know whether it is compressed or not until we read the options map which
// comes after. I guess we could use some kind of detection logic to
// determine if it is gzipped by peeking and just ignoring options, but
// this seems simpler for now.
var entriesRaw []byte
switch entriesType {
case msgp.StrType:
var entriesStr string
entriesStr, err = dc.ReadString()
if err != nil {
return msgp.WrapError(err, "EntriesRaw")
}
entriesRaw = []byte(entriesStr)
case msgp.BinType:
entriesRaw, err = dc.ReadBytes(nil)
if err != nil {
return msgp.WrapError(err, "EntriesRaw")
}
default:
return msgp.WrapError(fmt.Errorf("invalid type %d", entriesType), "EntriesRaw")
}
if arrLen == 3 {
pfe.OptionsMap, err = parseOptions(dc)
if err != nil {
return err
}
}
err = pfe.parseEntries(entriesRaw, pfe.Compressed() == "gzip", tag)
if err != nil {
return err
}
return nil
}
func (pfe *PackedForwardEventLogRecords) parseEntries(entriesRaw []byte, isGzipped bool, tag string) error {
var reader io.Reader
reader = bytes.NewReader(entriesRaw)
if isGzipped {
var err error
reader, err = gzip.NewReader(reader)
if err != nil {
return err
}
defer reader.(*gzip.Reader).Close()
}
msgpReader := msgp.NewReader(reader)
for {
lr := pdata.NewLogRecord()
err := parseEntryToLogRecord(msgpReader, lr)
if err != nil {
if msgp.Cause(err) == io.EOF {
return nil
}
return err
}
lr.Attributes().InsertString(tagAttributeKey, tag)
tgt := pfe.LogSlice.AppendEmpty()
lr.CopyTo(tgt)
}
}