Skip to content

Commit a7a013e

Browse files
authored
Merge pull request #68 from fluent/cosmo0920-handle-fluent-bit-v2-event-format
output: Handle Fluent Bit V2 metadata format
2 parents b93d969 + 4c65c5a commit a7a013e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

output/decoder.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,22 @@ func GetRecord(dec *FLBDecoder) (ret int, ts interface{}, rec map[interface{}]in
8282
return -2, 0, nil
8383
}
8484

85-
t := slice.Index(0).Interface()
85+
var t interface{}
86+
ts = slice.Index(0).Interface()
87+
switch ty := ts.(type) {
88+
case FLBTime:
89+
t = ty
90+
case uint64:
91+
t = ty
92+
case []interface{}: // for Fluent Bit V2 metadata type of format
93+
s := reflect.ValueOf(ty)
94+
if s.Kind() != reflect.Slice || s.Len() < 2 {
95+
return -4, 0, nil
96+
}
97+
t = s.Index(0).Interface()
98+
default:
99+
return -5, 0, nil
100+
}
86101
data := slice.Index(1)
87102

88103
map_data, ok := data.Interface().(map[interface{}]interface{})

output/decoder_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ var dummyRecord [29]byte = [29]byte{0x92, /* fix array 2 */
3333
0x01, /* fix int 1 */
3434
}
3535

36+
// dummyV2Record should be byte Array, not Slice to be able to Cast c array.
37+
var dummyV2Record [39]byte = [39]byte{0xdd, /* array 32 */ 0x00, 0x00, 0x00,
38+
0x02, /* count of array elements */
39+
0xdd, /* array 32 */ 0x00, 0x00, 0x00,
40+
0x02, /* count of array elements */
41+
0xd7, 0x00, 0x64, 0xbe, 0x0e, 0xeb, 0x16, 0x36, 0xe1, 0x28, 0x80, /* 2023/07/24 14:40:59 */
42+
0x82, /* fix map 2 */
43+
0xa7, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63, 0x74, /* fix str 7 "compact" */
44+
0xc3, /* true */
45+
0xa6, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, /* fix str 6 "schema" */
46+
0x01, /* fix int 1 */
47+
}
48+
3649
func TestGetRecord(t *testing.T) {
3750
dec := NewDecoder(unsafe.Pointer(&dummyRecord), len(dummyRecord))
3851
if dec == nil {
@@ -63,3 +76,34 @@ func TestGetRecord(t *testing.T) {
6376
t.Errorf(`record["schema"] is not 1 %d`, v)
6477
}
6578
}
79+
80+
func TestGetV2Record(t *testing.T) {
81+
dec := NewDecoder(unsafe.Pointer(&dummyV2Record), len(dummyV2Record))
82+
if dec == nil {
83+
t.Fatal("dec is nil")
84+
}
85+
86+
ret, timestamp, record := GetRecord(dec)
87+
if ret < 0 {
88+
t.Fatalf("ret is negative: code %v", ret)
89+
}
90+
91+
// test timestamp
92+
ts, ok := timestamp.(FLBTime)
93+
if !ok {
94+
t.Fatalf("cast error. Type is %s", reflect.TypeOf(timestamp))
95+
}
96+
97+
if ts.Unix() != int64(0x64be0eeb) {
98+
t.Errorf("ts.Unix() error. given %d", ts.Unix())
99+
}
100+
101+
// test record
102+
v, ok := record["schema"].(int64)
103+
if !ok {
104+
t.Fatalf("cast error. Type is %s", reflect.TypeOf(record["schema"]))
105+
}
106+
if v != 1 {
107+
t.Errorf(`record["schema"] is not 1 %d`, v)
108+
}
109+
}

0 commit comments

Comments
 (0)