-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (133 loc) · 3.5 KB
/
main.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
package main
import (
"C"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
)
import (
"encoding/json"
"log"
"time"
"github.com/rudrasecure/fluentbit-go-rethinkdb/db"
)
var pluginName = "fluentbit-go-rethinkdb"
//export FLBPluginRegister
func FLBPluginRegister(plugin unsafe.Pointer) int {
return output.FLBPluginRegister(plugin, pluginName, "A Fluent Bit Go plugin for RethinkDB.")
}
// (fluentbit will call this)
// plugin (context) pointer to fluentbit context (state/ c code)
//
//export FLBPluginInit
func FLBPluginInit(plugin unsafe.Pointer) int {
log.Printf("[%s] Init called", pluginName)
connectionUri := output.FLBPluginConfigKey(plugin, "ConnectionUri")
if connectionUri == "" {
log.Printf("[%s] ConnectionUri is required", pluginName)
return output.FLB_ERROR
}
database := output.FLBPluginConfigKey(plugin, "Database")
tableName := output.FLBPluginConfigKey(plugin, "TableName")
primaryKey := output.FLBPluginConfigKey(plugin, "PrimaryKey")
if primaryKey == "" {
primaryKey = "id"
}
if database == "" {
database = "test"
}
if tableName == "" {
tableName = "logs"
}
r := &db.RethinkDB{}
err := r.Connect(connectionUri, database, tableName, primaryKey)
if err != nil {
log.Printf("[%s] Error connecting to RethinkDB: %s", pluginName, err)
return output.FLB_ERROR
}
output.FLBPluginSetContext(plugin, r)
return output.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
decoder := output.NewDecoder(data, int(length))
var logRecords []map[string]any
r := output.FLBPluginGetContext(ctx).(*db.RethinkDB)
for {
ret, ts, record := output.GetRecord(decoder)
if ret != 0 {
break
}
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.Print("given time is not in a known format, defaulting to now.\n")
timeStamp = time.Now()
}
record["fluentbit_timestamp"] = timeStamp.UTC().Format(time.RFC3339)
logLine := createJSON(record)
logRecords = append(logRecords, logLine)
}
err := r.Insert(logRecords)
if err != nil {
log.Printf("[%s] Error inserting data to RethinkDB: %s", pluginName, err)
return output.FLB_RETRY
}
return output.FLB_OK
}
//export FLBPluginExitCtx
func FLBPluginExitCtx(ctx unsafe.Pointer) int {
log.Printf("[%v] Exit called", ctx)
r, ok := output.FLBPluginGetContext(ctx).(*db.RethinkDB)
if ok {
err := r.Close()
if err != nil {
log.Printf("[%s] Error closing connection to RethinkDB: %s", pluginName, err)
return output.FLB_ERROR
}
}
return output.FLB_OK
}
func parseValue(v interface{}) any {
switch t := v.(type) {
case []byte:
var data map[string]interface{}
err := json.Unmarshal(t, &data)
if err != nil {
return string(t)
}
return data
case map[interface{}]interface{}:
dataRemapped := make(map[string]interface{})
for k, v := range t {
dataRemapped[k.(string)] = parseValue(v)
}
return dataRemapped
case []interface{}:
dataRemapped := make([]interface{}, len(t))
for i, v := range t {
dataRemapped[i] = parseValue(v)
}
return dataRemapped
default:
return t
}
}
func createJSON(record map[interface{}]interface{}) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range record {
m[k.(string)] = parseValue(v)
}
return m
}
func handlePanic() {
if r := recover(); r != nil {
log.Printf("[%s] Recovered in f %v", pluginName, r)
}
}
func main() {
defer handlePanic()
}