Skip to content

Commit 66fb080

Browse files
Warren Fernandesedsiper
authored andcommitted
examples: add example of multi-instance output plugin
This change also updates the out_gstdout example to have argument names that match more accurately what is being passed in by fluent-bit. Signed-off-by: Jason Keene <[email protected]>
1 parent 8253747 commit 66fb080

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

examples/out_gstdout/out_gstdout.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package main
22

3-
import "github.com/fluent/fluent-bit-go/output"
43
import (
4+
"C"
55
"fmt"
66
"unsafe"
7-
"C"
7+
8+
"github.com/fluent/fluent-bit-go/output"
89
)
910

1011
//export FLBPluginRegister
11-
func FLBPluginRegister(ctx unsafe.Pointer) int {
12-
return output.FLBPluginRegister(ctx, "gstdout", "Stdout GO!")
12+
func FLBPluginRegister(def unsafe.Pointer) int {
13+
return output.FLBPluginRegister(def, "gstdout", "Stdout GO!")
1314
}
1415

1516
//export FLBPluginInit
1617
// (fluentbit will call this)
17-
// ctx (context) pointer to fluentbit context (state/ c code)
18-
func FLBPluginInit(ctx unsafe.Pointer) int {
18+
// plugin (context) pointer to fluentbit context (state/ c code)
19+
func FLBPluginInit(plugin unsafe.Pointer) int {
1920
// Example to retrieve an optional configuration parameter
20-
param := output.FLBPluginConfigKey(ctx, "param")
21+
param := output.FLBPluginConfigKey(plugin, "param")
2122
fmt.Printf("[flb-go] plugin parameter = '%s'\n", param)
2223
return output.FLB_OK
2324
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all:
2+
go build -buildmode=c-shared -o out_multiinstance.so
3+
4+
clean:
5+
rm -rf *.so *.h *~
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This example demonstrates using a plugin where there are multiple instances
2+
and `FLBPluginFlushCtx` is used to disambiguate multiple instances of the
3+
output plugin.

examples/out_multiinstance/out.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"C"
5+
"log"
6+
"unsafe"
7+
8+
"github.com/fluent/fluent-bit-go/output"
9+
)
10+
11+
//export FLBPluginRegister
12+
func FLBPluginRegister(def unsafe.Pointer) int {
13+
return output.FLBPluginRegister(def, "multiinstance", "Testing multiple instances.")
14+
}
15+
16+
//export FLBPluginInit
17+
func FLBPluginInit(plugin unsafe.Pointer) int {
18+
id := output.FLBPluginConfigKey(plugin, "id")
19+
log.Printf("[multiinstance] id = %q", id)
20+
// Set the context to point to any Go variable
21+
output.FLBPluginSetContext(plugin, unsafe.Pointer(&id))
22+
return output.FLB_OK
23+
}
24+
25+
//export FLBPluginFlush
26+
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
27+
log.Print("Flush called for unknown instance")
28+
return output.FLB_OK
29+
}
30+
31+
//export FLBPluginFlushCtx
32+
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
33+
// Cast context back into the original type for the Go variable
34+
id := (*string)(ctx)
35+
log.Printf("Flush called for id: %s", *id)
36+
37+
dec := output.NewDecoder(data, int(length))
38+
39+
for {
40+
ret, _, _ := output.GetRecord(dec)
41+
if ret != 0 {
42+
break
43+
}
44+
}
45+
46+
return output.FLB_OK
47+
}
48+
49+
//export FLBPluginExit
50+
func FLBPluginExit() int {
51+
return output.FLB_OK
52+
}
53+
54+
func main() {
55+
}

0 commit comments

Comments
 (0)