Skip to content

Commit a237b72

Browse files
elsesiyPettitWesley
authored andcommitted
examples: Fix panic due to timestamp type assertion
Signed-off-by: Jonas-Taha El Sesiy <github@elsesiy.com>
1 parent 1b7fc15 commit a237b72

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

examples/out_gstdout/out_gstdout.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"C"
55
"fmt"
6+
"time"
67
"unsafe"
78

89
"github.com/fluent/fluent-bit-go/output"
@@ -42,8 +43,18 @@ func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
4243
break
4344
}
4445

46+
var timestamp time.Time
47+
switch t := ts.(type) {
48+
case output.FLBTime:
49+
timestamp = ts.(output.FLBTime).Time
50+
case uint64:
51+
timestamp = time.Unix(int64(t), 0)
52+
default:
53+
fmt.Println("time provided invalid, defaulting to now.")
54+
timestamp = time.Now()
55+
}
56+
4557
// Print record keys and values
46-
timestamp := ts.(output.FLBTime)
4758
fmt.Printf("[%d] %s: [%s, {", count, C.GoString(tag),
4859
timestamp.String())
4960
for k, v := range record {

examples/out_multiinstance/out.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"C"
55
"fmt"
66
"log"
7+
"time"
78
"unsafe"
89

910
"github.com/fluent/fluent-bit-go/output"
@@ -45,8 +46,18 @@ func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int
4546
break
4647
}
4748

49+
var timestamp time.Time
50+
switch t := ts.(type) {
51+
case output.FLBTime:
52+
timestamp = ts.(output.FLBTime).Time
53+
case uint64:
54+
timestamp = time.Unix(int64(t), 0)
55+
default:
56+
fmt.Println("time provided invalid, defaulting to now.")
57+
timestamp = time.Now()
58+
}
59+
4860
// Print record keys and values
49-
timestamp := ts.(output.FLBTime)
5061
fmt.Printf("[%d] %s: [%s, {", count, C.GoString(tag), timestamp.String())
5162

5263
for k, v := range record {

0 commit comments

Comments
 (0)