-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathout_drycc.go
More file actions
155 lines (141 loc) · 3.76 KB
/
out_drycc.go
File metadata and controls
155 lines (141 loc) · 3.76 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
package main
import (
"C"
"unsafe"
"context"
"strings"
"github.com/fluent/fluent-bit-go/output"
"github.com/valkey-io/valkey-go"
"github.com/valkey-io/valkey-go/valkeycompat"
)
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"time"
)
var (
Stream string
MaxLen int64
Revision string
BuildDate string
ControllerName string
ControllerRegex *regexp.Regexp
ExcludeNamespaces []string
ValkeyClient valkey.Client
)
//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 valkey 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
Stream = output.FLBPluginConfigKey(ctx, "Stream")
MaxLen, err = strconv.ParseInt(output.FLBPluginConfigKey(ctx, "Max_Len"), 10, 64)
if err != nil {
MaxLen = 1000
}
ValkeyURL := output.FLBPluginConfigKey(ctx, "Valkey_URL")
ControllerName = output.FLBPluginConfigKey(ctx, "Controller_Name")
ControllerRegex = regexp.MustCompile(output.FLBPluginConfigKey(ctx, "Controller_Regex"))
ExcludeNamespaces = strings.Split(output.FLBPluginConfigKey(ctx, "Exclude_Namespaces"), ",")
ValkeyClient, err = valkey.NewClient(valkey.MustParseURL(ValkeyURL))
if err != nil {
return output.FLB_ERROR
}
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 := valkeycompat.NewAdapter(ValkeyClient).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 {
pipeline.XAdd(context, valkeycompat.XAddArgs{
Stream: Stream,
NoMkStream: false,
MaxLen: MaxLen,
Approx: true,
ID: "*",
Values: values,
})
} else {
status = output.FLB_ERROR
}
}
if _, err := pipeline.Exec(context); err != nil {
status = output.FLB_ERROR
}
return status
}
//export FLBPluginExit
func FLBPluginExit() int {
ValkeyClient.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() {}