-
Notifications
You must be signed in to change notification settings - Fork 2
/
cshared.go
605 lines (501 loc) · 14 KB
/
cshared.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package plugin
/*
#include <stdlib.h>
*/
import "C"
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"unsafe"
"github.com/vmihailenco/msgpack/v5"
cmetrics "github.com/calyptia/cmetrics-go"
"github.com/calyptia/plugin/input"
metricbuilder "github.com/calyptia/plugin/metric/cmetric"
"github.com/calyptia/plugin/output"
)
const (
// maxBufferedMessages is the number of messages that will be buffered
// between each fluent-bit interval (approx 1 second).
defaultMaxBufferedMessages = 300000
// collectInterval is set to the interval present before in core-fluent-bit.
collectInterval = 1000 * time.Nanosecond
)
var (
unregister func()
cmt *cmetrics.Context
logger Logger
maxBufferedMessages = defaultMaxBufferedMessages
)
// FLBPluginPreRegister -
//
//export FLBPluginPreRegister
func FLBPluginPreRegister(hotReloading C.int) int {
if hotReloading == C.int(1) {
registerWG.Add(1)
}
return input.FLB_OK
}
// FLBPluginRegister registers a plugin in the context of the fluent-bit runtime, a name and description
// can be provided.
//
//export FLBPluginRegister
func FLBPluginRegister(def unsafe.Pointer) int {
defer registerWG.Done()
if theInput == nil && theOutput == nil {
fmt.Fprintf(os.Stderr, "no input or output registered\n")
return input.FLB_RETRY
}
if theInput != nil {
out := input.FLBPluginRegister(def, theName, theDesc)
unregister = func() {
input.FLBPluginUnregister(def)
}
return out
}
out := output.FLBPluginRegister(def, theName, theDesc)
unregister = func() {
output.FLBPluginUnregister(def)
}
return out
}
func cleanup() int {
if unregister != nil {
unregister()
unregister = nil
}
if runCancel != nil {
runCancel()
runCancel = nil
}
if !theInputLock.TryLock() {
return input.FLB_OK
}
defer theInputLock.Unlock()
if theChannel != nil {
defer close(theChannel)
}
return input.FLB_OK
}
// FLBPluginInit this method gets invoked once by the fluent-bit runtime at initialisation phase.
// here all the plugin context should be initialized and any data or flag required for
// plugins to execute the collect or flush callback.
//
//export FLBPluginInit
func FLBPluginInit(ptr unsafe.Pointer) int {
initWG.Add(1)
defer initWG.Done()
if theInput == nil && theOutput == nil {
fmt.Fprintf(os.Stderr, "no input or output registered\n")
return input.FLB_RETRY
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var err error
if theInput != nil {
conf := &flbInputConfigLoader{ptr: ptr}
cmt, err = input.FLBPluginGetCMetricsContext(ptr)
if err != nil {
return input.FLB_ERROR
}
logger = &flbInputLogger{ptr: ptr}
fbit := &Fluentbit{
Conf: conf,
Metrics: makeMetrics(cmt),
Logger: logger,
}
err = theInput.Init(ctx, fbit)
if maxbuffered := fbit.Conf.String("go.MaxBufferedMessages"); maxbuffered != "" {
maxbuffered, err := strconv.Atoi(maxbuffered)
if err != nil {
maxBufferedMessages = maxbuffered
}
}
} else {
conf := &flbOutputConfigLoader{ptr: ptr}
cmt, err = output.FLBPluginGetCMetricsContext(ptr)
if err != nil {
return output.FLB_ERROR
}
logger = &flbOutputLogger{ptr: ptr}
fbit := &Fluentbit{
Conf: conf,
Metrics: makeMetrics(cmt),
Logger: logger,
}
err = theOutput.Init(ctx, fbit)
}
if err != nil {
fmt.Fprintf(os.Stderr, "init: %v\n", err)
return input.FLB_ERROR
}
return input.FLB_OK
}
// flbPluginReset is meant to reset the plugin between tests.
func flbPluginReset() {
theInputLock.Lock()
defer theInputLock.Unlock()
defer func() {
if ret := recover(); ret != nil {
fmt.Fprintf(os.Stderr, "Channel is already closed")
return
}
}()
close(theChannel)
theInput = nil
}
func testFLBPluginInputCallback() ([]byte, error) {
data := unsafe.Pointer(nil)
var csize C.size_t
FLBPluginInputCallback(&data, &csize)
if data == nil {
return []byte{}, nil
}
defer C.free(data)
return C.GoBytes(data, C.int(csize)), nil
}
// prepareOutputFlush is a testing utility.
func prepareOutputFlush(output OutputPlugin) error {
theOutput = output
FLBPluginOutputPreRun(0)
return nil
}
// Lock used to synchronize access to theInput variable.
var theInputLock sync.Mutex
// prepareInputCollector is meant to prepare resources for input collectors
func prepareInputCollector(multiInstance bool) {
runCtx, runCancel = context.WithCancel(context.Background())
if !multiInstance {
theChannel = make(chan Message, maxBufferedMessages)
}
theInputLock.Lock()
if multiInstance {
if theChannel == nil {
theChannel = make(chan Message, maxBufferedMessages)
}
defer theInputLock.Unlock()
}
go func(theChannel chan<- Message) {
if !multiInstance {
defer theInputLock.Unlock()
}
go func(theChannel chan<- Message) {
err := theInput.Collect(runCtx, theChannel)
if err != nil {
fmt.Fprintf(os.Stderr, "collect error: %v\n", err)
}
}(theChannel)
<-runCtx.Done()
log.Printf("goroutine will be stopping: name=%q\n", theName)
}(theChannel)
}
// FLBPluginInputPreRun this method gets invoked by the fluent-bit runtime, once the plugin has been
// initialized, the plugin invoked only once before executing the input callbacks.
//
//export FLBPluginInputPreRun
func FLBPluginInputPreRun(useHotReload C.int) int {
registerWG.Wait()
prepareInputCollector(true)
return input.FLB_OK
}
// FLBPluginInputPause this method gets invoked by the fluent-bit runtime, once the plugin has been
// paused, the plugin invoked this method and entering paused state.
//
//export FLBPluginInputPause
func FLBPluginInputPause() {
if runCancel != nil {
runCancel()
runCancel = nil
}
if !theInputLock.TryLock() {
return
}
defer theInputLock.Unlock()
if theChannel != nil {
close(theChannel)
theChannel = nil
}
}
// FLBPluginInputResume this method gets invoked by the fluent-bit runtime, once the plugin has been
// resumeed, the plugin invoked this method and re-running state.
//
//export FLBPluginInputResume
func FLBPluginInputResume() {
prepareInputCollector(true)
}
// FLBPluginOutputPreExit this method gets invoked by the fluent-bit runtime, once the plugin has been
// exited, the plugin invoked this method and entering exiting state.
//
//export FLBPluginOutputPreExit
func FLBPluginOutputPreExit() {
if runCancel != nil {
runCancel()
runCancel = nil
}
if !theInputLock.TryLock() {
return
}
defer theInputLock.Unlock()
if theChannel != nil {
close(theChannel)
theChannel = nil
}
}
// FLBPluginOutputPreRun -
//
//export FLBPluginOutputPreRun
func FLBPluginOutputPreRun(useHotReload C.int) int {
registerWG.Wait()
var err error
runCtx, runCancel = context.WithCancel(context.Background())
theChannel = make(chan Message)
go func(runCtx context.Context) {
go func(runCtx context.Context) {
err = theOutput.Flush(runCtx, theChannel)
}(runCtx)
<-runCtx.Done()
log.Printf("goroutine will be stopping: name=%q\n", theName)
}(runCtx)
if err != nil {
fmt.Fprintf(os.Stderr, "run: %s\n", err)
return output.FLB_ERROR
}
return output.FLB_OK
}
// FLBPluginInputCallback this method gets invoked by the fluent-bit runtime, once the plugin has been
// initialized, the plugin implementation is responsible for handling the incoming data and the context
// that gets past, for long-living collectors the plugin itself should keep a running thread and fluent-bit
// will not execute further callbacks.
//
// This function will invoke Collect only once to preserve backward
// compatible behavior. There are unit tests to enforce this behavior.
//
//export FLBPluginInputCallback
func FLBPluginInputCallback(data *unsafe.Pointer, csize *C.size_t) int {
initWG.Wait()
if theInput == nil {
fmt.Fprintf(os.Stderr, "no input registered\n")
return input.FLB_RETRY
}
buf := bytes.NewBuffer([]byte{})
for loop := min(len(theChannel), maxBufferedMessages); loop > 0; loop-- {
select {
case msg, ok := <-theChannel:
if !ok {
return input.FLB_ERROR
}
b, err := msgpack.Marshal([]any{&EventTime{msg.Time}, msg.Record})
if err != nil {
fmt.Fprintf(os.Stderr, "msgpack marshal: %s\n", err)
return input.FLB_ERROR
}
buf.Grow(len(b))
buf.Write(b)
case <-runCtx.Done():
err := runCtx.Err()
if err != nil && !errors.Is(err, context.Canceled) {
fmt.Fprintf(os.Stderr, "run: %s\n", err)
return input.FLB_ERROR
}
// enforce a runtime gc, to prevent the thread finalizer on
// fluent-bit to kick in before any remaining data has not been GC'ed
// causing a sigsegv.
defer runtime.GC()
loop = 0
default:
loop = 0
}
}
if buf.Len() > 0 {
b := buf.Bytes()
cdata := C.CBytes(b)
*data = cdata
if csize != nil {
*csize = C.size_t(len(b))
}
}
return input.FLB_OK
}
// FLBPluginInputCleanupCallback releases the memory used during the input callback
//
//export FLBPluginInputCleanupCallback
func FLBPluginInputCleanupCallback(data unsafe.Pointer) int {
C.free(data)
return input.FLB_OK
}
// FLBPluginFlush callback gets invoked by the fluent-bit runtime once there is data for the corresponding
// plugin in the pipeline, a data pointer, length and a tag are passed to the plugin interface implementation.
//
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, clength C.int, ctag *C.char) int {
initWG.Wait()
if theOutput == nil {
fmt.Fprintf(os.Stderr, "no output registered\n")
return output.FLB_RETRY
}
var err error
select {
case <-runCtx.Done():
err = runCtx.Err()
if err != nil && !errors.Is(err, context.Canceled) {
fmt.Fprintf(os.Stderr, "run: %s\n", err)
return output.FLB_ERROR
}
return output.FLB_OK
default:
}
in := C.GoBytes(data, clength)
tag := C.GoString(ctag)
if err := pluginFlush(tag, in); err != nil {
fmt.Fprintf(os.Stderr, "flush: %s\n", err)
return output.FLB_ERROR
}
return output.FLB_OK
}
func pluginFlush(tag string, b []byte) error {
dec := msgpack.NewDecoder(bytes.NewReader(b))
for {
select {
case <-runCtx.Done():
err := runCtx.Err()
if err != nil && !errors.Is(err, context.Canceled) {
fmt.Fprintf(os.Stderr, "run: %s\n", err)
return fmt.Errorf("run: %w", err)
}
return nil
default:
}
msg, err := decodeMsg(dec, tag)
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return err
}
theChannel <- msg
}
}
// decodeMsg should be called with an already initialized decoder.
func decodeMsg(dec *msgpack.Decoder, tag string) (Message, error) {
var out Message
var entry []msgpack.RawMessage
err := dec.Decode(&entry)
if errors.Is(err, io.EOF) {
return out, err
}
if err != nil {
return out, fmt.Errorf("msgpack unmarshal: %w", err)
}
if l := len(entry); l < 2 {
return out, fmt.Errorf("msgpack unmarshal: expected 2 elements, got %d", l)
}
eventTime := &EventTime{}
if err := msgpack.Unmarshal(entry[0], &eventTime); err != nil {
var eventWithMetadata []msgpack.RawMessage // for Fluent Bit V2 metadata type of format
if err := msgpack.Unmarshal(entry[0], &eventWithMetadata); err != nil {
return out, fmt.Errorf("msgpack unmarshal event with metadata: %w", err)
}
if len(eventWithMetadata) < 1 {
return out, fmt.Errorf("msgpack unmarshal event time with metadata: expected 1 element, got %d", len(eventWithMetadata))
}
if err := msgpack.Unmarshal(eventWithMetadata[0], &eventTime); err != nil {
return out, fmt.Errorf("msgpack unmarshal event time with metadata: %w", err)
}
return out, fmt.Errorf("msgpack unmarshal event time: %w", err)
}
var record map[string]any
if err := msgpack.Unmarshal(entry[1], &record); err != nil {
return out, fmt.Errorf("msgpack unmarshal event record: %w", err)
}
out.Time = eventTime.Time.UTC()
out.Record = record
out.tag = &tag
return out, nil
}
// FLBPluginExit method is invoked once the plugin instance is exited from the fluent-bit context.
//
//export FLBPluginExit
func FLBPluginExit() int {
return cleanup()
}
type flbInputConfigLoader struct {
ptr unsafe.Pointer
}
func (f *flbInputConfigLoader) String(key string) string {
return unquote(input.FLBPluginConfigKey(f.ptr, key))
}
func unquote(s string) string {
if tmp, err := strconv.Unquote(s); err == nil {
return tmp
}
// unescape literal newlines
if strings.Contains(s, `\n`) {
if tmp2, err := strconv.Unquote(`"` + s + `"`); err == nil {
return tmp2
}
}
return s
}
type flbOutputConfigLoader struct {
ptr unsafe.Pointer
}
func (f *flbOutputConfigLoader) String(key string) string {
return unquote(output.FLBPluginConfigKey(f.ptr, key))
}
type flbInputLogger struct {
ptr unsafe.Pointer
}
func (f *flbInputLogger) Error(format string, a ...any) {
message := fmt.Sprintf(format, a...)
input.FLBPluginLogPrint(f.ptr, input.FLB_LOG_ERROR, message)
}
func (f *flbInputLogger) Warn(format string, a ...any) {
message := fmt.Sprintf(format, a...)
input.FLBPluginLogPrint(f.ptr, input.FLB_LOG_WARN, message)
}
func (f *flbInputLogger) Info(format string, a ...any) {
message := fmt.Sprintf(format, a...)
input.FLBPluginLogPrint(f.ptr, input.FLB_LOG_INFO, message)
}
func (f *flbInputLogger) Debug(format string, a ...any) {
message := fmt.Sprintf(format, a...)
input.FLBPluginLogPrint(f.ptr, input.FLB_LOG_DEBUG, message)
}
type flbOutputLogger struct {
ptr unsafe.Pointer
}
func (f *flbOutputLogger) Error(format string, a ...any) {
message := fmt.Sprintf(format, a...)
output.FLBPluginLogPrint(f.ptr, output.FLB_LOG_ERROR, message)
}
func (f *flbOutputLogger) Warn(format string, a ...any) {
message := fmt.Sprintf(format, a...)
output.FLBPluginLogPrint(f.ptr, output.FLB_LOG_WARN, message)
}
func (f *flbOutputLogger) Info(format string, a ...any) {
message := fmt.Sprintf(format, a...)
output.FLBPluginLogPrint(f.ptr, output.FLB_LOG_INFO, message)
}
func (f *flbOutputLogger) Debug(format string, a ...any) {
message := fmt.Sprintf(format, a...)
output.FLBPluginLogPrint(f.ptr, output.FLB_LOG_DEBUG, message)
}
func makeMetrics(cmp *cmetrics.Context) Metrics {
return &metricbuilder.Builder{
Namespace: "fluentbit",
SubSystem: "plugin",
Context: cmp,
OnError: func(err error) {
fmt.Fprintf(os.Stderr, "metrics: %s\n", err)
},
}
}