-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
59 lines (47 loc) · 1.25 KB
/
log.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
package types
import (
"encoding/json"
"errors"
)
// FluentBitLog is the Go representation of a fluent-bit log record.
// Normally, the format is an array of mixed types.
// [timestamp, attrs]
// Where timestamp is a float with seconds and nanoseconds as fraction.
// And attrs being an object with string key and values.
type FluentBitLog struct {
Timestamp FluentBitTime
Attrs FluentBitLogAttrs
}
// FluentBitTime wrapper.
type FluentBitTime float64
// FluentBitLogAttrs wrapper.
type FluentBitLogAttrs map[string]any
func (l FluentBitLog) AsSlice() []any {
return []any{float64(l.Timestamp), map[string]any(l.Attrs)}
}
// UnmarshalJSON parses fluent-big json representation of
// [timestamp, attrs].
func (l *FluentBitLog) UnmarshalJSON(b []byte) error {
var ss [2]json.RawMessage
err := json.Unmarshal(b, &ss)
if err != nil {
return err
}
if len(ss) != 2 {
return errors.New("unexpected log record parts length")
}
err = json.Unmarshal(ss[0], &l.Timestamp)
if err != nil {
return err
}
err = json.Unmarshal(ss[1], &l.Attrs)
if err != nil {
return err
}
return nil
}
// MarshalJSON into fluent-bit json representation of
// [timestamp, attrs].
func (l FluentBitLog) MarshalJSON() ([]byte, error) {
return json.Marshal(l.AsSlice())
}