-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflb_api.go
253 lines (229 loc) · 6.04 KB
/
flb_api.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
Copyright 2021 Takahiro Yamashita
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"C"
"errors"
"go/types"
"log"
"strconv"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
"github.com/nokute78/fluentbit-plugin-out-expect/expect"
)
const Version = "0.0.1"
func getParameter(p unsafe.Pointer, key string, i int) (string, error) {
s := key + strconv.Itoa(i)
param := output.FLBPluginConfigKey(p, s)
if len(param) == 0 {
return "", errors.New("Not found")
}
return param, nil
}
//export FLBPluginRegister
func FLBPluginRegister(def unsafe.Pointer) int {
return output.FLBPluginRegister(def, "gexpect", "Check if a key/value is expected the key/value")
}
//export FLBPluginInit
// (fluentbit will call this)
// plugin (context) pointer to fluentbit context (state/ c code)
func FLBPluginInit(p unsafe.Pointer) int {
cnf := expect.Config{}
log.Printf("[expect] Ver: %s\n", Version)
// Exist
for i := 0; i < expect.ParamNumMax; i++ {
param, err := getParameter(p, expect.ConfigExistKeyName, i)
if err == nil {
p, err := expect.NewConfigLineFromJson(param)
if err != nil {
continue
}
cnf.SetExist(p, true)
}
param, err = getParameter(p, expect.ConfigNotExistKeyName, i)
if err == nil {
p, err := expect.NewConfigLineFromJson(param)
if err != nil {
continue
}
cnf.SetExist(p, false)
}
param, err = getParameter(p, expect.ConfigBoolKeyName, i)
if err == nil {
p, err := expect.NewConfigLineFromJson(param)
if err != nil {
continue
}
err = cnf.SetTypeCondition(p, types.Bool)
if err != nil {
log.Printf("bool config error=%s\n", err)
}
}
param, err = getParameter(p, expect.ConfigStrKeyName, i)
if err == nil {
p, err := expect.NewConfigLineFromJson(param)
if err != nil {
continue
}
err = cnf.SetTypeCondition(p, types.String)
if err != nil {
log.Printf("string config error=%s\n", err)
}
}
param, err = getParameter(p, expect.ConfigIntKeyName, i)
if err == nil {
p, err := expect.NewConfigLineFromJson(param)
if err != nil {
continue
}
err = cnf.SetTypeCondition(p, types.Int)
if err != nil {
log.Printf("int config error=%s\n", err)
}
}
param, err = getParameter(p, expect.ConfigUintKeyName, i)
if err == nil {
p, err := expect.NewConfigLineFromJson(param)
if err != nil {
continue
}
err = cnf.SetTypeCondition(p, types.Uint)
if err != nil {
log.Printf("uint config error=%s\n", err)
}
}
param, err = getParameter(p, expect.ConfigDoubleKeyName, i)
if err == nil {
p, err := expect.NewConfigLineFromJson(param)
if err != nil {
continue
}
err = cnf.SetTypeCondition(p, types.Float64)
if err != nil {
log.Printf("double config error=%s\n", err)
}
}
}
output.FLBPluginSetContext(p, cnf)
return output.FLB_OK
}
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
log.Println("[expect] Flush called for unknown instance")
return output.FLB_OK
}
func reportsErrors(reports []string, tag *C.char) {
log.Println(strconv.Itoa(len(reports)) + " error(s) detected! tag:" + C.GoString(tag))
for _, v := range reports {
log.Println(" " + v)
}
log.Println("")
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx unsafe.Pointer, data unsafe.Pointer, length C.int, tag *C.char) int {
cnf, ok := output.FLBPluginGetContext(ctx).(expect.Config)
if !ok {
log.Println("[expect] Context Conversion error")
return output.FLB_ERROR
}
dec := output.NewDecoder(data, int(length))
for {
reports := []string{}
ret, _, record := output.GetRecord(dec)
if ret != 0 {
break
}
for _, keys := range cnf.Exists {
_, ok := keys.GetValueFromMap(record)
if !ok {
reports = append(reports, "Exist key not found:"+keys.FlattenKeys)
}
}
for _, keys := range cnf.NotExists {
_, ok := keys.GetValueFromMap(record)
if ok {
reports = append(reports, "Not Exist key found:"+keys.FlattenKeys)
}
}
for _, tc := range cnf.TypeConditions {
v, ok := tc.Keys.GetValueFromMap(record)
if !ok {
reports = append(reports, "Key not found:"+tc.Keys.FlattenKeys)
continue
}
b, err := tc.Condition.IsMatch(v)
if err != nil {
reports = append(reports, "IsMatch error:"+tc.Keys.FlattenKeys)
} else if !b {
reports = append(reports, "Error. expect: value "+i2str(v)+" of "+tc.TypeConditionStr)
}
}
if len(reports) > 0 {
reportsErrors(reports, tag)
}
}
return output.FLB_OK
}
func i2str(v interface{}) string {
switch v.(type) {
case string:
return v.(string)
case bool:
b := v.(bool)
return strconv.FormatBool(b)
case int:
i := v.(int)
return strconv.FormatInt(int64(i), 10)
case int8:
i := v.(int8)
return strconv.FormatInt(int64(i), 10)
case int16:
i := v.(int16)
return strconv.FormatInt(int64(i), 10)
case int32:
i := v.(int32)
return strconv.FormatInt(int64(i), 10)
case int64:
i := v.(int64)
return strconv.FormatInt(i, 10)
case uint:
i := v.(uint)
return strconv.FormatUint(uint64(i), 10)
case uint8:
i := v.(uint8)
return strconv.FormatUint(uint64(i), 10)
case uint16:
i := v.(uint16)
return strconv.FormatUint(uint64(i), 10)
case uint32:
i := v.(uint32)
return strconv.FormatUint(uint64(i), 10)
case uint64:
i := v.(uint64)
return strconv.FormatUint(i, 10)
case float32:
f := v.(float32)
return strconv.FormatFloat(float64(f), 'f', -1, 32)
case float64:
f := v.(float64)
return strconv.FormatFloat(f, 'f', -1, 64)
}
return ""
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}
// dummy
func main() {
}