forked from cosmo0920/fluent-bit-go-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
out_s3.go
254 lines (213 loc) · 6.48 KB
/
out_s3.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
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import "github.com/fluent/fluent-bit-go/output"
import "github.com/json-iterator/go"
import "github.com/aws/aws-sdk-go/aws"
import "github.com/aws/aws-sdk-go/aws/session"
import "github.com/aws/aws-sdk-go/service/s3/s3manager"
import "github.com/prometheus/common/version"
import (
"C"
"bytes"
"compress/gzip"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"unsafe"
)
var plugin GoOutputPlugin = &fluentPlugin{}
type s3 struct {
bucket string
prefix string
uploader *s3manager.Uploader
compressFormat format
}
var s3operator s3
type GoOutputPlugin interface {
PluginConfigKey(ctx unsafe.Pointer, key string) string
Unregister(ctx unsafe.Pointer)
GetRecord(dec *output.FLBDecoder) (ret int, ts interface{}, rec map[interface{}]interface{})
NewDecoder(data unsafe.Pointer, length int) *output.FLBDecoder
Put(objectKey string, timestamp time.Time, line string) error
Exit(code int)
}
type fluentPlugin struct{}
func (p *fluentPlugin) PluginConfigKey(ctx unsafe.Pointer, key string) string {
return output.FLBPluginConfigKey(ctx, key)
}
func (p *fluentPlugin) Unregister(ctx unsafe.Pointer) {
output.FLBPluginUnregister(ctx)
}
func (p *fluentPlugin) GetRecord(dec *output.FLBDecoder) (int, interface{}, map[interface{}]interface{}) {
return output.GetRecord(dec)
}
func (p *fluentPlugin) NewDecoder(data unsafe.Pointer, length int) *output.FLBDecoder {
return output.NewDecoder(data, int(length))
}
func (p *fluentPlugin) Exit(code int) {
os.Exit(code)
}
func (p *fluentPlugin) Put(objectKey string, timestamp time.Time, line string) error {
switch s3operator.compressFormat {
case plainTextFormat:
_, err := s3operator.uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s3operator.bucket),
Key: aws.String(objectKey),
Body: strings.NewReader(line),
})
return err
case gzipFormat:
compressed, err := makeGzip([]byte(line))
if err != nil {
return err
}
_, err = s3operator.uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s3operator.bucket),
Key: aws.String(objectKey),
Body: bytes.NewReader(compressed),
})
return err
}
return nil
}
// based on https://text.baldanders.info/golang/gzip-operation/
func makeGzip(body []byte) ([]byte, error) {
var b bytes.Buffer
err := func() error {
gw := gzip.NewWriter(&b)
gw.Name = "fluent-bit-go-s3"
gw.ModTime = time.Now()
defer gw.Close()
if _, err := gw.Write(body); err != nil {
return err
}
return nil
}()
return b.Bytes(), err
}
//export FLBPluginRegister
func FLBPluginRegister(ctx unsafe.Pointer) int {
return output.FLBPluginRegister(ctx, "s3", "S3 Output plugin written in GO!")
}
//export FLBPluginInit
// (fluentbit will call this)
// ctx (context) pointer to fluentbit context (state/ c code)
func FLBPluginInit(ctx unsafe.Pointer) int {
// Example to retrieve an optional configuration parameter
credential := plugin.PluginConfigKey(ctx, "Credential")
accessKeyID := plugin.PluginConfigKey(ctx, "AccessKeyID")
secretAccessKey := plugin.PluginConfigKey(ctx, "SecretAccessKey")
bucket := plugin.PluginConfigKey(ctx, "Bucket")
s3prefix := plugin.PluginConfigKey(ctx, "S3Prefix")
region := plugin.PluginConfigKey(ctx, "Region")
compress := plugin.PluginConfigKey(ctx, "Compress")
config, err := getS3Config(accessKeyID, secretAccessKey, credential, s3prefix, bucket, region, compress)
if err != nil {
plugin.Unregister(ctx)
plugin.Exit(1)
return output.FLB_ERROR
}
fmt.Printf("[flb-go] Starting fluent-bit-go-s3: %s\n", version.Info())
fmt.Printf("[flb-go] plugin credential parameter = '%s'\n", credential)
fmt.Printf("[flb-go] plugin accessKeyID parameter = '%s'\n", accessKeyID)
fmt.Printf("[flb-go] plugin secretAccessKey parameter = '%s'\n", secretAccessKey)
fmt.Printf("[flb-go] plugin bucket parameter = '%s'\n", bucket)
fmt.Printf("[flb-go] plugin s3prefix parameter = '%s'\n", s3prefix)
fmt.Printf("[flb-go] plugin region parameter = '%s'\n", region)
fmt.Printf("[flb-go] plugin compress parameter = '%s'\n", compress)
sess := session.New(&aws.Config{
Credentials: config.credentials,
Region: config.region,
})
uploader := s3manager.NewUploader(sess, func(u *s3manager.Uploader) {
u.PartSize = 5 * 1024 * 1024
u.LeavePartsOnError = true
})
s3operator = s3{
bucket: *config.bucket,
prefix: *config.s3prefix,
uploader: uploader,
compressFormat: config.compress,
}
return output.FLB_OK
}
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
var ret int
var record map[interface{}]interface{}
dec := plugin.NewDecoder(data, int(length))
var lines string
for {
ret, _, record = plugin.GetRecord(dec)
if ret != 0 {
break
}
line, err := createJSON(record)
if err != nil {
fmt.Printf("error creating message for S3: %v\n", err)
continue
}
lines += line + "\n"
}
objectKey := GenerateObjectKey(s3operator.bucket, time.Now())
err := plugin.Put(objectKey, time.Now(), lines)
if err != nil {
fmt.Printf("error sending message for S3: %v\n", err)
return output.FLB_RETRY
}
// Return options:
//
// output.FLB_OK = data have been processed.
// output.FLB_ERROR = unrecoverable error, do not try this again.
// output.FLB_RETRY = retry to flush later.
return output.FLB_OK
}
// format is S3_PREFIX/S3_TRAILING_PREFIX/date/hour/timestamp_uuid.log
func GenerateObjectKey(S3Prefix string, t time.Time) string {
var fileext string
switch s3operator.compressFormat {
case plainTextFormat:
fileext = ".log"
case gzipFormat:
fileext = ".log.gz"
}
timestamp := t.Format("20060102150405")
date := t.Format("20060102")
hour := strconv.Itoa(t.Hour())
fileName := strings.Join([]string{timestamp, fileext}, "")
objectKey := filepath.Join(s3operator.prefix, date, hour, fileName)
return objectKey
}
func encodeJSON(record map[interface{}]interface{}) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range record {
switch t := v.(type) {
case []byte:
// prevent encoding to base64
m[k.(string)] = string(t)
case map[interface{}]interface{}:
if nextValue, ok := record[k].(map[interface{}]interface{}); ok {
m[k.(string)] = encodeJSON(nextValue)
}
default:
m[k.(string)] = v
}
}
return m
}
func createJSON(record map[interface{}]interface{}) (string, error) {
m := encodeJSON(record)
js, err := jsoniter.Marshal(m)
if err != nil {
return "{}", err
}
return string(js), nil
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}
func main() {
}