forked from gjbae1212/fluent-bit-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_pubsub.go
241 lines (214 loc) · 6.19 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"C"
"fmt"
"strconv"
"time"
"unsafe"
"cloud.google.com/go/pubsub"
"context"
"github.com/fluent/fluent-bit-go/output"
)
import (
"encoding/json"
"os"
)
var (
plugin Keeper
hostname string
format string
attributes map[string]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")
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")
ft := wrapper.GetConfigKey(ctx, "Format")
ab := wrapper.GetConfigKey(ctx, "Attributes")
// 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 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)
// fmt.Printf("[pubsub-go] plugin parameter format = '%s'\n", ft)
// fmt.Printf("[pubsub-go] plugin parameter attributes = '%s'\n", ab)
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
}
if ab != "" {
err = json.Unmarshal([]byte(ab), &attributes)
if err != nil {
fmt.Printf("[err][init] %+v\n", err)
return output.FLB_ERROR
}
}
if _, ok := supportFormats[ft]; ok {
format = ft
} else {
fmt.Printf("[err][init] unsupported format '%s'\n", ft)
return output.FLB_ERROR
}
publishSetting := pubsub.PublishSettings{
ByteThreshold: byteThreshold,
CountThreshold: countThreshold,
DelayThreshold: delayThreshold,
Timeout: timeout,
}
keeper, err := NewKeeper(project, topic, &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
}
timestamp := ts.(output.FLBTime)
if formatter, ok := supportFormats[format]; ok {
// fmt.Printf("[pubsub-go] format = '%s'\n", format)
msg, err := formatter.Encode(record)
if err != nil {
fmt.Printf("[err][encode] %+v \n", err)
return output.FLB_ERROR
}
results = append(results, plugin.Send(ctx, msg, attributes))
} else {
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), attributes))
}
}
}
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() {}