forked from aws/amazon-kinesis-streams-for-fluent-bit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fluent-bit-kinesis.go
155 lines (129 loc) · 5.04 KB
/
fluent-bit-kinesis.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
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"fmt"
"C"
"unsafe"
"strings"
"github.com/aws/amazon-kinesis-streams-for-fluent-bit/kinesis"
"github.com/aws/amazon-kinesis-firehose-for-fluent-bit/plugins"
"github.com/fluent/fluent-bit-go/output"
"github.com/sirupsen/logrus"
)
var (
pluginInstances []*kinesis.OutputPlugin
)
func addPluginInstance(ctx unsafe.Pointer) error {
pluginID := len(pluginInstances)
output.FLBPluginSetContext(ctx, pluginID)
instance, err := newKinesisOutput(ctx, pluginID)
if err != nil {
return err
}
pluginInstances = append(pluginInstances, instance)
return nil
}
func getPluginInstance(ctx unsafe.Pointer) *kinesis.OutputPlugin {
pluginID := output.FLBPluginGetContext(ctx).(int)
return pluginInstances[pluginID]
}
func newKinesisOutput(ctx unsafe.Pointer, pluginID int) (*kinesis.OutputPlugin, error) {
stream := output.FLBPluginConfigKey(ctx, "stream")
logrus.Infof("[kinesis %d] plugin parameter stream = '%s'", pluginID, stream)
region := output.FLBPluginConfigKey(ctx, "region")
logrus.Infof("[kinesis %d] plugin parameter region = '%s'", pluginID, region)
dataKeys := output.FLBPluginConfigKey(ctx, "data_keys")
logrus.Infof("[kinesis %d] plugin parameter data_keys = '%s'", pluginID, dataKeys)
partitionKey := output.FLBPluginConfigKey(ctx, "partition_key")
logrus.Infof("[kinesis %d] plugin parameter partition_key = '%s'", pluginID, partitionKey)
roleARN := output.FLBPluginConfigKey(ctx, "role_arn")
logrus.Infof("[kinesis %d] plugin parameter role_arn = '%s'", pluginID, roleARN)
endpoint := output.FLBPluginConfigKey(ctx, "endpoint")
logrus.Infof("[kinesis %d] plugin parameter endpoint = '%s'", pluginID, endpoint)
appendNewline := output.FLBPluginConfigKey(ctx, "append_newline")
logrus.Infof("[kinesis %d] plugin parameter append_newline = %s", pluginID, appendNewline)
if stream == "" || region == "" {
return nil, fmt.Errorf("[kinesis %d] stream and region are required configuration parameters", pluginID)
}
if partitionKey == "log" {
return nil, fmt.Errorf("[kinesis %d] 'log' cannot be set as the partition key", pluginID)
}
if partitionKey == "" {
logrus.Infof("[kinesis %d] no partition key provided. A random one will be generated.", pluginID)
}
appendNL := false
if strings.ToLower(appendNewline) == "true" {
appendNL = true
}
return kinesis.NewOutputPlugin(region, stream, dataKeys, partitionKey, roleARN, endpoint, appendNL, pluginID)
}
// The "export" comments have syntactic meaning
// This is how the compiler knows a function should be callable from the C code
//export FLBPluginRegister
func FLBPluginRegister(ctx unsafe.Pointer) int {
return output.FLBPluginRegister(ctx, "kinesis", "Amazon Kinesis Data Streams Fluent Bit Plugin.")
}
//export FLBPluginInit
func FLBPluginInit(ctx unsafe.Pointer) int {
plugins.SetupLogger()
err := addPluginInstance(ctx)
if err != nil {
logrus.Errorf("[kinesis] Failed to initialize plugin: %v\n", err)
return output.FLB_ERROR
}
return output.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
var count int
var ret int
var record map[interface{}]interface{}
// Create Fluent Bit decoder
dec := output.NewDecoder(data, int(length))
kinesisOutput := getPluginInstance(ctx)
fluentTag := C.GoString(tag)
logrus.Debugf("[kinesis %d] Found logs with tag: %s\n", kinesisOutput.PluginID, fluentTag)
for {
//Extract Record
ret, _, record = output.GetRecord(dec)
if ret != 0 {
break
}
retCode := kinesisOutput.AddRecord(record)
if retCode != output.FLB_OK {
return retCode
}
count++
}
err := kinesisOutput.Flush()
if err != nil {
logrus.Errorf("[kinesis %d] %v\n", kinesisOutput.PluginID, err)
return output.FLB_ERROR
}
logrus.Debugf("[kinesis %d] Processed %d events with tag %s\n", kinesisOutput.PluginID, count, fluentTag)
return output.FLB_OK
}
//export FLBPluginExit
func FLBPluginExit() int {
// Before final exit, call Flush() for all the instances of the Output Plugin
for i := range pluginInstances {
err := pluginInstances[i].Flush()
if err != nil {
logrus.Errorf("[kinesis %d] %v\n", pluginInstances[i].PluginID, err)
}
}
return output.FLB_OK
}
func main() {
}