-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
143 lines (122 loc) · 3.31 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
package main
import (
"fmt"
"time"
"unsafe"
"C"
outerda "github.com/erda-project/erda-for-fluent-bit/out_erda"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/fluent/fluent-bit-go/output"
)
func init() {
logrus.SetFormatter(&logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
})
}
var instanceMap = map[string]*outerda.Output{}
func defaultConfig() outerda.Config {
return outerda.Config{
RemoteConfig: outerda.RemoteConfig{
Headers: map[string]string{},
RequestTimeout: time.Second * 10,
KeepAliveIdleTimeout: time.Second * 60,
URL: "",
URLFromLogLabel: "monitor_log_collector",
GzipLevel: 3,
Format: "json",
},
}
}
//export FLBPluginRegister
func FLBPluginRegister(def unsafe.Pointer) int {
return output.FLBPluginRegister(def, "erda", "forward data to erda!")
}
//export FLBPluginInit
// (fluentbit will call this)
// plugin (context) pointer to fluentbit context (state/ c code)
func FLBPluginInit(ctx unsafe.Pointer) int {
cfg := defaultConfig()
err := outerda.LoadFromFLBPlugin(&cfg, func(key string) string {
return output.FLBPluginConfigKey(ctx, key)
})
if err != nil {
outerda.LogError("load error: %s", err)
return output.FLB_ERROR
}
obj := outerda.NewOutput(cfg)
pluginID := uuid.New().String()
instanceMap[pluginID] = obj
output.FLBPluginSetContext(ctx, pluginID)
return output.FLB_OK
}
func getPluginInstance(ctx unsafe.Pointer) (*outerda.Output, error) {
pluginID, ok := output.FLBPluginGetContext(ctx).(string)
if !ok {
return nil, fmt.Errorf("invalid pluginID %+v from ctx", output.FLBPluginGetContext(ctx))
}
outErdaInstance, ok := instanceMap[pluginID]
if !ok {
return nil, fmt.Errorf("can't find instance with pluginID<%s>", pluginID)
}
return outErdaInstance, nil
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
// var count int
var ret int
var ts interface{}
var record map[interface{}]interface{}
outErdaInstance, err := getPluginInstance(ctx)
if err != nil {
logrus.Errorf("getPluginInstance: %s", err)
return output.FLB_ERROR
}
// Create Fluent Bit decoder
dec := output.NewDecoder(data, int(length))
// Iterate Records
for {
// Extract Record
ret, ts, record = output.GetRecord(dec)
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:
timestamp = time.Now()
}
if val := outErdaInstance.AddEvent(&outerda.Event{Record: record, Timestamp: timestamp}); val != output.FLB_OK {
outErdaInstance.Reset()
return val
}
// count++
}
err = outErdaInstance.Flush()
if err != nil {
outerda.LogError("Flush error", err)
outErdaInstance.Reset()
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
}
//export FLBPluginExit
func FLBPluginExit() int {
for pid, instance := range instanceMap {
if err := instance.Close(); err != nil {
logrus.Errorf("close output pluginID<%s> failed, err: %s", pid, err)
}
}
return output.FLB_OK
}
func main() {
}