-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathout_drycc.go
More file actions
179 lines (166 loc) · 4.46 KB
/
out_drycc.go
File metadata and controls
179 lines (166 loc) · 4.46 KB
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
package main
import (
"C"
"unsafe"
"context"
"sort"
"strings"
"github.com/fluent/fluent-bit-go/output"
redis "github.com/redis/go-redis/v9"
)
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"time"
)
var (
Stream string
MaxLen int64
Revision string
BuildDate string
ControllerName string
ControllerRegex *regexp.Regexp
ExcludeNamespaces []string
ClusterClient *redis.ClusterClient
)
//export FLBPluginRegister
func FLBPluginRegister(ctx unsafe.Pointer) int {
fmt.Printf("Drycc output version %s %s", Revision, BuildDate)
return output.FLBPluginRegister(ctx, "drycc", "Ship fluent-bit logs to redis xstream")
}
// (fluentbit will call this)
// ctx (context) pointer to fluentbit context (state/ c code)
//
//export FLBPluginInit
func FLBPluginInit(ctx unsafe.Pointer) int {
var err error
addrs := strings.Split(output.FLBPluginConfigKey(ctx, "Addrs"), ",")
sort.Strings(addrs)
Stream = output.FLBPluginConfigKey(ctx, "Stream")
MaxLen, err = strconv.ParseInt(output.FLBPluginConfigKey(ctx, "Max_Len"), 10, 64)
if err != nil {
MaxLen = 1000
}
username := output.FLBPluginConfigKey(ctx, "Username")
password := output.FLBPluginConfigKey(ctx, "Password")
ControllerName = output.FLBPluginConfigKey(ctx, "Controller_Name")
ControllerRegex = regexp.MustCompile(output.FLBPluginConfigKey(ctx, "Controller_Regex"))
ExcludeNamespaces = strings.Split(output.FLBPluginConfigKey(ctx, "Exclude_Namespaces"), ",")
ClusterClient = redis.NewClusterClient(&redis.ClusterOptions{
ClusterSlots: func(context.Context) ([]redis.ClusterSlot, error) {
const slotsSize = 16383
var size = len(addrs)
var slotsRange = slotsSize / size
var slots []redis.ClusterSlot
for index, addr := range addrs {
start := slotsRange * index
end := start + slotsRange
if (slotsSize - end) < slotsRange {
end = slotsSize
}
slots = append(slots, redis.ClusterSlot{
Start: start,
End: end,
Nodes: []redis.ClusterNode{{Addr: addr}},
})
}
return slots, nil
},
Username: username,
Password: password, // "" == no password
RouteRandomly: true,
})
return output.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, _ *C.char) int {
status := output.FLB_OK
context, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
decoder := output.NewDecoder(data, int(length))
pipeline := ClusterClient.Pipeline()
for {
ret, ts, rec := output.GetRecord(decoder)
if ret != 0 {
break
}
// Get timestamp
rec["time"] = toTimestamp(ts)
if !checkRecord(rec) {
continue
}
if values, err := toValues(rec); err == nil {
if err := pipeline.XAdd(context, &redis.XAddArgs{
Stream: Stream,
NoMkStream: false,
MaxLen: MaxLen,
Approx: true,
ID: "*",
Values: values,
}).Err(); err != nil {
status = output.FLB_ERROR
}
} else {
status = output.FLB_ERROR
}
}
if _, err := pipeline.Exec(context); err != nil {
status = output.FLB_ERROR
}
return status
}
//export FLBPluginExit
func FLBPluginExit() int {
ClusterClient.Close()
return output.FLB_OK
}
func toMap(values map[interface{}]interface{}) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range values {
switch t := v.(type) {
case []byte:
// prevent encoding to base64
m[k.(string)] = string(t)
case map[interface{}]interface{}:
m[k.(string)] = toMap(t)
default:
m[k.(string)] = v
}
}
return m
}
func toValues(values map[interface{}]interface{}) (map[string]interface{}, error) {
data, err := json.Marshal(toMap(values))
if err != nil {
return nil, err
}
return map[string]interface{}{"data": data, "timestamp": time.Now().Unix()}, err
}
func toTimestamp(ts interface{}) string {
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()
}
return timestamp.Format(time.RFC3339Nano)
}
func checkRecord(record map[interface{}]interface{}) bool {
if kubernetes, ok := record["kubernetes"].(map[interface{}]interface{}); ok {
// drycc controller
container := fmt.Sprintf("%s", kubernetes["container_name"])
if ok && strings.HasPrefix(container, ControllerName) {
log := fmt.Sprintf("%s", record["log"])
if ok && len(ControllerRegex.FindStringSubmatch(log)) > 0 {
return true
}
}
}
return false
}
func main() {}