Skip to content

Commit 6d6dfcb

Browse files
committed
api: proxy: custom: Adjust API for custom Go plugins
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 1e12f53 commit 6d6dfcb

File tree

6 files changed

+67
-5
lines changed

6 files changed

+67
-5
lines changed

include/fluent-bit/flb_api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <fluent-bit/flb_info.h>
2424
#include <fluent-bit/flb_output.h>
25+
#include <fluent-bit/flb_custom.h>
2526

2627
struct flb_api {
2728
const char *(*output_get_property) (const char *, struct flb_output_instance *);
@@ -33,6 +34,12 @@ struct flb_api {
3334
void (*log_print) (int, const char*, int, const char*, ...);
3435
int (*input_log_check) (struct flb_input_instance *, int);
3536
int (*output_log_check) (struct flb_output_instance *, int);
37+
38+
/* To preserve ABI, we need to add these APIs after the
39+
* input/output definitions. */
40+
const char *(*custom_get_property) (const char *, struct flb_custom_instance *);
41+
void *(*custom_get_cmt_instance) (struct flb_custom_instance *);
42+
int (*custom_log_check) (struct flb_custom_instance *, int);
3643
};
3744

3845
#ifdef FLB_CORE

include/fluent-bit/flb_custom.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,9 @@ int flb_custom_plugin_property_check(struct flb_custom_instance *ins,
107107
int flb_custom_init_all(struct flb_config *config);
108108
void flb_custom_set_context(struct flb_custom_instance *ins, void *context);
109109
void flb_custom_instance_destroy(struct flb_custom_instance *ins);
110+
#ifdef FLB_HAVE_METRICS
111+
void *flb_custom_get_cmt_instance(struct flb_custom_instance *ins);
112+
#endif
113+
int flb_custom_log_check(struct flb_custom_instance *ins, int l);
110114

111115
#endif

src/flb_api.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <fluent-bit/flb_input.h>
2626
#include <fluent-bit/flb_output.h>
27+
#include <fluent-bit/flb_custom.h>
2728

2829
struct flb_api *flb_api_create()
2930
{
@@ -37,15 +38,18 @@ struct flb_api *flb_api_create()
3738

3839
api->output_get_property = flb_output_get_property;
3940
api->input_get_property = flb_input_get_property;
41+
api->custom_get_property = flb_custom_get_property;
4042

4143
#ifdef FLB_HAVE_METRICS
4244
api->output_get_cmt_instance = flb_output_get_cmt_instance;
4345
api->input_get_cmt_instance = flb_input_get_cmt_instance;
46+
api->custom_get_cmt_instance = flb_custom_get_cmt_instance;
4447
#endif
4548

4649
api->log_print = flb_log_print;
4750
api->input_log_check = flb_input_log_check;
4851
api->output_log_check = flb_output_log_check;
52+
api->custom_log_check = flb_custom_log_check;
4953

5054
return api;
5155
}

src/flb_custom.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <fluent-bit/flb_pack.h>
2828
#include <fluent-bit/flb_metrics.h>
2929
#include <fluent-bit/flb_utils.h>
30+
#include <fluent-bit/flb_plugin_proxy.h>
3031
#include <fluent-bit/flb_upstream.h>
3132
#include <fluent-bit/flb_downstream.h>
3233
#include <chunkio/chunkio.h>
@@ -183,6 +184,24 @@ struct flb_custom_instance *flb_custom_new(struct flb_config *config,
183184
snprintf(instance->name, sizeof(instance->name) - 1,
184185
"%s.%i", plugin->name, id);
185186

187+
if (plugin->type == FLB_CUSTOM_PLUGIN_CORE) {
188+
instance->context = NULL;
189+
}
190+
else {
191+
struct flb_plugin_proxy_context *ctx;
192+
193+
ctx = flb_calloc(1, sizeof(struct flb_plugin_proxy_context));
194+
if (!ctx) {
195+
flb_errno();
196+
flb_free(instance);
197+
return NULL;
198+
}
199+
200+
ctx->proxy = plugin->proxy;
201+
202+
instance->context = ctx;
203+
}
204+
186205
instance->id = id;
187206
instance->alias = NULL;
188207
instance->p = plugin;
@@ -352,3 +371,22 @@ void flb_custom_set_context(struct flb_custom_instance *ins, void *context)
352371
{
353372
ins->context = context;
354373
}
374+
375+
#ifdef FLB_HAVE_METRICS
376+
void *flb_custom_get_cmt_instance(struct flb_custom_instance *ins)
377+
{
378+
return (void *)ins->cmt;
379+
}
380+
#endif
381+
382+
/* Check custom plugin's log level.
383+
* Not for core plugins but for Golang plugins.
384+
* Golang plugins do not have thread-local flb_worker_ctx information. */
385+
int flb_custom_log_check(struct flb_custom_instance *ins, int l)
386+
{
387+
if (ins->log_level < l) {
388+
return FLB_FALSE;
389+
}
390+
391+
return FLB_TRUE;
392+
}

src/flb_plugin_proxy.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ static int flb_proxy_register_input(struct flb_plugin_proxy *proxy,
423423
return 0;
424424
}
425425

426-
int flb_proxy_custom_cb_init(struct flb_custom_instance *ins,
426+
int flb_proxy_custom_cb_init(struct flb_custom_instance *c_ins,
427427
struct flb_config *config, void *data);
428428

429429
static int flb_proxy_custom_cb_exit(void *custom_context,
@@ -660,23 +660,32 @@ int flb_plugin_proxy_set(struct flb_plugin_proxy_def *def, int type,
660660
return 0;
661661
}
662662

663-
int flb_proxy_custom_cb_init(struct flb_custom_instance *ins,
663+
int flb_proxy_custom_cb_init(struct flb_custom_instance *c_ins,
664664
struct flb_config *config, void *data)
665665
{
666666
int ret = -1;
667667
struct flb_plugin_proxy_context *pc;
668668
struct flb_plugin_proxy *proxy;
669669

670-
pc = (struct flb_plugin_proxy_context *)(ins->context);
670+
pc = (struct flb_plugin_proxy_context *)(c_ins->context);
671671
proxy = pc->proxy;
672672

673+
/* Before to initialize, set the instance reference */
674+
pc->proxy->instance = c_ins;
675+
673676
if (proxy->def->proxy == FLB_PROXY_GOLANG) {
674677
#ifdef FLB_HAVE_PROXY_GO
675678
ret = proxy_go_custom_init(proxy);
676679
#endif
677680
}
678681

679-
return ret;
682+
if (ret == -1) {
683+
flb_error("[custom] could not initialize '%s' plugin",
684+
c_ins->p->name);
685+
return -1;
686+
}
687+
688+
return 0;
680689
}
681690

682691
int flb_proxy_custom_cb_exit(void *custom_context,

src/proxy/go/go.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*
4343
* - name: shortname of the plugin.
4444
* - description: plugin description.
45-
* - type: input, output, filter, processor, custom.
45+
* - type: input, output, filter, custom, whatever.
4646
* - proxy: type of proxy e.g. GOLANG
4747
* - flags: optional flags, not used by Go plugins at the moment.
4848
*

0 commit comments

Comments
 (0)