Skip to content

Commit 8253747

Browse files
Jason Keeneedsiper
authored andcommitted
output: add function to set context on the plugin
This change also updates the argument names to the other helper functions to match more accurately what is being called with in fluent-bit. Signed-off-by: Warren Fernandes <[email protected]>
1 parent 241a4c7 commit 8253747

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

output/flb_output.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,28 @@
2020
#ifndef FLBGO_OUTPUT_H
2121
#define FLBGO_OUTPUT_H
2222

23-
#include <stdio.h>
24-
2523
struct flb_api {
2624
char *(*output_get_property) (char *, void *);
2725
};
2826

27+
struct flb_plugin_proxy_context {
28+
void *remote_context;
29+
};
30+
31+
/* This structure is used for initialization.
32+
* It matches the one in proxy/go/go.c in fluent-bit source code.
33+
*/
2934
struct flbgo_output_plugin {
30-
char *name;
35+
void *_;
3136
struct flb_api *api;
32-
void *o_ins;
33-
int (*cb_init)();
34-
int (*cb_flush)(void *, size_t, char *);
35-
int (*cb_exit)(void *);
37+
struct flb_output_instance *o_ins;
38+
struct flb_plugin_proxy_context *context;
3639
};
3740

38-
char *output_get_property(char *key, void *ctx)
41+
char *output_get_property(char *key, void *plugin)
3942
{
40-
struct flbgo_output_plugin *plugin = ctx;
41-
return plugin->api->output_get_property(key, plugin->o_ins);
43+
struct flbgo_output_plugin *p = plugin;
44+
return p->api->output_get_property(key, p->o_ins);
4245
}
4346

4447
#endif

output/flb_plugin.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
#define FLB_PROXY_OUTPUT_PLUGIN 2
3030
#define FLB_PROXY_GOLANG 11
3131

32-
/* Structure used for registration, it match the one on flb_plugin_proxy.h */
33-
struct flb_plugin_proxy {
32+
/* This structure is used for registration.
33+
* It matches the one in flb_plugin_proxy.h in fluent-bit source code.
34+
*/
35+
struct flb_plugin_proxy_def {
3436
int type;
3537
int proxy;
3638
int flags;

output/output.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,51 @@ package output
2323
#include "flb_output.h"
2424
*/
2525
import "C"
26-
import "fmt"
27-
import "unsafe"
26+
import (
27+
"unsafe"
28+
)
2829

2930
// Define constants matching Fluent Bit core
30-
const FLB_ERROR = C.FLB_ERROR
31-
const FLB_OK = C.FLB_OK
32-
const FLB_RETRY = C.FLB_RETRY
31+
const (
32+
FLB_ERROR = C.FLB_ERROR
33+
FLB_OK = C.FLB_OK
34+
FLB_RETRY = C.FLB_RETRY
3335

34-
const FLB_PROXY_OUTPUT_PLUGIN = C.FLB_PROXY_OUTPUT_PLUGIN
35-
const FLB_PROXY_GOLANG = C.FLB_PROXY_GOLANG
36+
FLB_PROXY_OUTPUT_PLUGIN = C.FLB_PROXY_OUTPUT_PLUGIN
37+
FLB_PROXY_GOLANG = C.FLB_PROXY_GOLANG
38+
)
3639

3740
// Local type to define a plugin definition
38-
type FLBPlugin C.struct_flb_plugin_proxy
41+
type FLBPluginProxyDef C.struct_flb_plugin_proxy_def
3942
type FLBOutPlugin C.struct_flbgo_output_plugin
4043

4144
// When the FLBPluginInit is triggered by Fluent Bit, a plugin context
4245
// is passed and the next step is to invoke this FLBPluginRegister() function
4346
// to fill the required information: type, proxy type, flags name and
4447
// description.
45-
func FLBPluginRegister(ctx unsafe.Pointer, name string, desc string) int {
46-
p := (*FLBPlugin) (unsafe.Pointer(ctx))
48+
func FLBPluginRegister(def unsafe.Pointer, name, desc string) int {
49+
p := (*FLBPluginProxyDef)(def)
4750
p._type = FLB_PROXY_OUTPUT_PLUGIN
4851
p.proxy = FLB_PROXY_GOLANG
4952
p.flags = 0
50-
p.name = C.CString(name)
53+
p.name = C.CString(name)
5154
p.description = C.CString(desc)
5255
return 0
5356
}
5457

5558
// Release resources allocated by the plugin initialization
56-
func FLBPluginUnregister(ctx unsafe.Pointer) {
57-
p := (*FLBPlugin) (unsafe.Pointer(ctx))
58-
fmt.Printf("[flbgo] unregistering %v\n", p)
59+
func FLBPluginUnregister(def unsafe.Pointer) {
60+
p := (*FLBPluginProxyDef)(def)
5961
C.free(unsafe.Pointer(p.name))
6062
C.free(unsafe.Pointer(p.description))
6163
}
6264

63-
func FLBPluginConfigKey(ctx unsafe.Pointer, key string) string {
65+
func FLBPluginConfigKey(plugin unsafe.Pointer, key string) string {
6466
_key := C.CString(key)
65-
return C.GoString(C.output_get_property(_key, unsafe.Pointer(ctx)))
67+
return C.GoString(C.output_get_property(_key, plugin))
68+
}
69+
70+
func FLBPluginSetContext(plugin, ctx unsafe.Pointer) {
71+
p := (*FLBOutPlugin)(plugin)
72+
p.context.remote_context = ctx
6673
}

0 commit comments

Comments
 (0)