forked from gjbae1212/fluent-bit-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
output_pubsub.go
217 lines (193 loc) · 5.59 KB
/
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
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
package main
import (
"C"
"fmt"
"strconv"
"time"
"unsafe"
"cloud.google.com/go/pubsub"
"context"
"github.com/fluent/fluent-bit-go/output"
)
import "os"
var (
plugin Keeper
hostname string
wrapper = OutputWrapper(&Output{})
timeout = pubsub.DefaultPublishSettings.Timeout
delayThreshold = pubsub.DefaultPublishSettings.DelayThreshold
countThreshold = pubsub.DefaultPublishSettings.CountThreshold
byteThreshold = pubsub.DefaultPublishSettings.ByteThreshold
debug = false
)
type Output struct{}
type OutputWrapper interface {
Register(ctx unsafe.Pointer, name string, desc string) int
GetConfigKey(ctx unsafe.Pointer, key string) string
NewDecoder(data unsafe.Pointer, length int) *output.FLBDecoder
GetRecord(dec *output.FLBDecoder) (ret int, ts interface{}, rec map[interface{}]interface{})
}
func (o *Output) Register(ctx unsafe.Pointer, name string, desc string) int {
return output.FLBPluginRegister(ctx, name, desc)
}
func (o *Output) GetConfigKey(ctx unsafe.Pointer, key string) string {
return output.FLBPluginConfigKey(ctx, key)
}
func (o *Output) NewDecoder(data unsafe.Pointer, length int) *output.FLBDecoder {
return output.NewDecoder(data, length)
}
func (o *Output) GetRecord(dec *output.FLBDecoder) (ret int, ts interface{}, rec map[interface{}]interface{}) {
return output.GetRecord(dec)
}
//export FLBPluginRegister
func FLBPluginRegister(ctx unsafe.Pointer) int {
return wrapper.Register(ctx, "pubsub", "output pubsub")
}
//export FLBPluginInit
func FLBPluginInit(ctx unsafe.Pointer) int {
var err error
project := wrapper.GetConfigKey(ctx, "Project")
topic := wrapper.GetConfigKey(ctx, "Topic")
jwtPath := wrapper.GetConfigKey(ctx, "JwtPath")
dg := wrapper.GetConfigKey(ctx, "Debug")
to := wrapper.GetConfigKey(ctx, "Timeout")
bt := wrapper.GetConfigKey(ctx, "ByteThreshold")
ct := wrapper.GetConfigKey(ctx, "CountThreshold")
dt := wrapper.GetConfigKey(ctx, "DelayThreshold")
fmt.Printf("[pubsub-go] plugin parameter project = '%s'\n", project)
fmt.Printf("[pubsub-go] plugin parameter topic = '%s'\n", topic)
fmt.Printf("[pubsub-go] plugin parameter jwtPath = '%s'\n", jwtPath)
fmt.Printf("[pubsub-go] plugin parameter debug = '%s'\n", dg)
fmt.Printf("[pubsub-go] plugin parameter timeout = '%s'\n", to)
fmt.Printf("[pubsub-go] plugin parameter byte threshold = '%s'\n", bt)
fmt.Printf("[pubsub-go] plugin parameter count threshold = '%s'\n", ct)
fmt.Printf("[pubsub-go] plugin parameter delay threshold = '%s'\n", dt)
hostname, err = os.Hostname()
if err != nil {
fmt.Printf("[err][init] %+v\n", err)
return output.FLB_ERROR
}
fmt.Printf("[pubsub-go] plugin hostname = '%s'\n", hostname)
if dg != "" {
debug, err = strconv.ParseBool(dg)
if err != nil {
fmt.Printf("[err][init] %+v\n", err)
return output.FLB_ERROR
}
}
if to != "" {
v, err := strconv.Atoi(to)
if err != nil {
fmt.Printf("[err][init] %+v\n", err)
return output.FLB_ERROR
}
timeout = time.Duration(v) * time.Millisecond
}
if bt != "" {
v, err := strconv.Atoi(bt)
if err != nil {
fmt.Printf("[err][init] %+v\n", err)
return output.FLB_ERROR
}
byteThreshold = v
}
if ct != "" {
v, err := strconv.Atoi(ct)
if err != nil {
fmt.Printf("[err][init] %+v\n", err)
return output.FLB_ERROR
}
countThreshold = v
}
if dt != "" {
v, err := strconv.Atoi(dt)
if err != nil {
fmt.Printf("[err][init] %+v\n", err)
return output.FLB_ERROR
}
delayThreshold = time.Duration(v) * time.Millisecond
}
publishSetting := pubsub.PublishSettings{
ByteThreshold: byteThreshold,
CountThreshold: countThreshold,
DelayThreshold: delayThreshold,
Timeout: timeout,
}
keeper, err := NewKeeper(project, topic, jwtPath, &publishSetting)
if err != nil {
fmt.Printf("[err][init] %+v\n", err)
return output.FLB_ERROR
}
plugin = keeper
return output.FLB_OK
}
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
ctx := context.Background()
tagname := ""
if tag == nil {
tagname = C.GoString(tag)
}
// Create Fluent Bit decoder
dec := wrapper.NewDecoder(data, int(length))
var results []*pubsub.PublishResult
// Iterate Records
for {
// Extract Record
ret, ts, record := wrapper.GetRecord(dec)
if ret != 0 { // don't rest
break
}
var timestamp time.Time
switch t := ts.(type) {
case output.FLBTime:
timestamp = ts.(output.FLBTime).Time
case uint64:
timestamp = time.Unix(int64(t), 0)
default:
fmt.Println("time provided invalid, defaulting to now.")
timestamp = time.Now()
}
for k, v := range record {
//fmt.Printf("[%s] %s %s %v \n", tagname, timestamp.String(), k, v)
_, _, _ = k, timestamp, tagname
results = append(results, plugin.Send(ctx, interfaceToBytes(v)))
}
}
for _, result := range results {
if _, err := result.Get(ctx); err != nil {
// if timeout is raised.
if err == context.DeadlineExceeded || err == context.Canceled {
fmt.Printf("[err][publish][retry] %+v \n", err)
return output.FLB_RETRY
}
// else error is next
fmt.Printf("[err][publish][don't retry] %+v \n", err)
}
}
return output.FLB_OK
}
//export FLBPluginExit
func FLBPluginExit() int {
plugin.Stop()
return output.FLB_OK
}
func interfaceToBytes(v interface{}) []byte {
switch d := v.(type) {
case []byte:
return d
case string:
return []byte(d)
case int, int32, int64, uint, uint32, uint64:
return []byte(fmt.Sprintf("%d", d))
case float32, float64:
return []byte(fmt.Sprintf("%f", d))
case bool:
return []byte(strconv.FormatBool(d))
case time.Time:
return []byte(d.Format(time.RFC3339))
default:
return []byte(fmt.Sprintf("%v", d))
}
}
func main() {}