This repository has been archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluent-bit-output-pubsub.go
86 lines (68 loc) · 1.73 KB
/
fluent-bit-output-pubsub.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
package main
import (
"C"
"context"
"encoding/json"
"fmt"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
"cloud.google.com/go/pubsub"
)
var topic *pubsub.Topic
//export FLBPluginRegister
func FLBPluginRegister(ctx unsafe.Pointer) int {
return output.FLBPluginRegister(ctx, "fluent-bit-output-pubsub", "FluentBit Output Plugin Cloud PubSub")
}
//export FLBPluginInit
func FLBPluginInit(ctx unsafe.Pointer) int {
projectID := output.FLBPluginConfigKey(ctx, "project_id")
topicName := output.FLBPluginConfigKey(ctx, "topic")
ctxBg := context.Background()
client, err := pubsub.NewClient(ctxBg, projectID)
if err != nil {
return output.FLB_ERROR
}
topic = client.Topic(topicName)
ok, err := topic.Exists(ctxBg)
if err != nil || !ok {
fmt.Printf("topic %s was not found in project %s. err: %s", topicName, projectID, err)
return output.FLB_ERROR
}
return output.FLB_OK
}
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
var ret int
var ts interface{}
var record map[interface{}]interface{}
// Create Fluent Bit decoder
dec := output.NewDecoder(data, int(length))
// Iterate Records
for {
// Extract Record
ret, ts, record = output.GetRecord(dec)
if ret != 0 {
break
}
timestamp := ts.(output.FLBTime)
fmt.Printf("Publishing Record %s: %v", timestamp.String(), record)
dataBuf, err := json.Marshal(record)
if err != nil {
return output.FLB_ERROR
}
ctxBg := context.Background()
result := topic.Publish(ctxBg, &pubsub.Message{Data: dataBuf})
_, err = result.Get(ctxBg)
if err != nil {
return output.FLB_RETRY
}
}
return output.FLB_OK
}
//export FLBPluginExit
func FLBPluginExit() int {
topic.Stop()
return output.FLB_OK
}
func main() {
}