-
Notifications
You must be signed in to change notification settings - Fork 16
/
out_redis.go
193 lines (162 loc) · 4.75 KB
/
out_redis.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
package main
import (
"C"
"fmt"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
jsoniter "github.com/json-iterator/go"
"os"
"time"
)
var (
rc *redisClient
json = jsoniter.ConfigCompatibleWithStandardLibrary
// both variables are set in Makefile
revision string
builddate string
plugin Plugin = &fluentPlugin{}
)
//export FLBPluginRegister
func FLBPluginRegister(ctx unsafe.Pointer) int {
return output.FLBPluginRegister(ctx, "redis", "Redis Output Plugin.")
}
type logmessage struct {
data []byte
}
type Plugin interface {
Environment(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
Send(values []*logmessage) error
Exit(code int)
}
type fluentPlugin struct{}
func (p *fluentPlugin) Environment(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) Send(values []*logmessage) error {
return rc.send(values)
}
// ctx (context) pointer to fluentbit context (state/ c code)
//
//export FLBPluginInit
func FLBPluginInit(ctx unsafe.Pointer) int {
hosts := plugin.Environment(ctx, "Hosts")
password := plugin.Environment(ctx, "Password")
key := plugin.Environment(ctx, "Key")
db := plugin.Environment(ctx, "DB")
usetls := plugin.Environment(ctx, "UseTLS")
tlsskipverify := plugin.Environment(ctx, "TLSSkipVerify")
// create a pool of redis connection pools
config, err := getRedisConfig(hosts, password, db, usetls, tlsskipverify, key)
if err != nil {
fmt.Printf("configuration errors: %v\n", err)
// FIXME use fluent-bit method to err in init
plugin.Unregister(ctx)
plugin.Exit(1)
return output.FLB_ERROR
}
rc = &redisClient{
pools: newPoolsFromConfig(config),
key: config.key,
}
fmt.Printf("[out-redis] build:%s version:%s redis connection to: %s\n", builddate, revision, config)
return output.FLB_OK
}
// FLBPluginFlush is called from fluent-bit when data need to be sent. is called from fluent-bit when data need to be sent.
//
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
var ret int
var ts interface{}
var record map[interface{}]interface{}
// Create Fluent Bit decoder
dec := plugin.NewDecoder(data, int(length))
// Iterate Records
var logs []*logmessage
for {
// Extract Record
ret, ts, record = plugin.GetRecord(dec)
if ret != 0 {
break
}
// Print record keys and values
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.Print("given time is not in a known format, defaulting to now.\n")
timeStamp = time.Now()
}
js, err := createJSON(timeStamp, C.GoString(tag), record)
if err != nil {
fmt.Printf("%v\n", err)
// DO NOT RETURN HERE becase one message has an error when json is
// generated, but a retry would fetch ALL messages again. instead an
// error should be printed to console
continue
}
logs = append(logs, js)
}
err := plugin.Send(logs)
if err != nil {
fmt.Printf("%v\n", err)
return output.FLB_RETRY
}
fmt.Printf("pushed %d logs\n", len(logs))
// 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 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
}
func createJSON(timestamp time.Time, tag string, record map[interface{}]interface{}) (*logmessage, error) {
m := parseMap(record)
// convert timestamp to RFC3339Nano which is logstash format
m["@timestamp"] = timestamp.UTC().Format(time.RFC3339Nano)
m["@tag"] = tag
js, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("error creating message for REDIS: %w", err)
}
return &logmessage{data: js}, nil
}
//export FLBPluginExit
func FLBPluginExit() int {
rc.pools.closeAll()
return output.FLB_OK
}
func main() {
}