forked from hartza-capital/fluent-bit-go-gcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
out_gcs.go
154 lines (132 loc) · 3.73 KB
/
out_gcs.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
package main
import (
"C"
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"time"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
"github.com/google/uuid"
jsoniter "github.com/json-iterator/go"
)
import "strings"
var (
gcsClient Client
err error
)
//export FLBPluginRegister
func FLBPluginRegister(def unsafe.Pointer) int {
return output.FLBPluginRegister(def, "gcs", "GCS Output plugin written in GO!")
}
//export FLBPluginInit
func FLBPluginInit(plugin unsafe.Pointer) int {
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", output.FLBPluginConfigKey(plugin, "Credential"))
gcsClient, err = NewClient()
if err != nil {
output.FLBPluginUnregister(plugin)
log.Fatal(err)
return output.FLB_ERROR
}
// Set the context
output.FLBPluginSetContext(plugin, map[string]string{
"region": output.FLBPluginConfigKey(plugin, "Region"),
"bucket": output.FLBPluginConfigKey(plugin, "Bucket"),
"prefix": output.FLBPluginConfigKey(plugin, "Prefix"),
})
return output.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
// Type assert context back into the original type for the Go variable
values := output.FLBPluginGetContext(ctx).(map[string]string)
log.Printf("[event] Flush called, context %s, %s, %v\n", values["region"], values["bucket"], C.GoString(tag))
dec := output.NewDecoder(data, int(length))
var rs []map[interface{}]interface{}
for {
ret, ts, record := output.GetRecord(dec)
if ret != 0 {
break
}
// Get timestamp
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:
log.Println("[warn] timestamp isn't known format. Use current time.")
timestamp = time.Now()
}
record["ts"] = timestamp
rs = append(rs, record)
}
if err := SaveRecords(values["bucket"], values["prefix"], C.GoString(tag), rs); err != nil {
log.Printf("[warn] error sending message in GCS: %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
}
func SaveRecords(bucket, prefix, tag string, records []map[interface{}]interface{}) error {
t := time.Now()
j, err := createJSONLines(records)
if err != nil {
return err
}
objectKey := GenerateObjectKey(prefix, tag, t)
if err := gcsClient.Write(bucket, objectKey, bytes.NewReader(j)); err != nil {
return err
}
return nil
}
// GenerateObjectKey : gen format object name PREFIX/date/hour/tag/timestamp_uuid.log
func GenerateObjectKey(prefix, tag string, t time.Time) string {
fileName := fmt.Sprintf("%s.log", uuid.Must(uuid.NewRandom()).String())
return filepath.Join(prefix, tag, t.Format("20060102/15"), fileName)
}
func createJSONLines(records []map[interface{}]interface{}) ([]byte, error) {
rs := make([]string, len(records))
for i, r := range records {
j, err := createJSON(r)
if err != nil {
return nil, err
}
rs[i] = j
}
return []byte(strings.Join(rs, "\n")), nil
}
func createJSON(record map[interface{}]interface{}) (string, error) {
js, err := jsoniter.Marshal(parseMap(record))
if err != nil {
return "{}", err
}
return string(js), nil
}
func parseMap(mapInterface map[interface{}]interface{}) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range mapInterface {
switch t := v.(type) {
case []byte:
// prevent encoding to base64
m[k.(string)] = string(t)
case map[interface{}]interface{}:
m[k.(string)] = parseMap(t)
default:
m[k.(string)] = v
}
}
return m
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}
func main() {}