Skip to content

Commit 615bdd4

Browse files
committed
out_lib: add new option 'data_mode'
the new configuration option 'data_mode' allows to control what type of data the plugin pass to the callback set by the caller. By default it was always passing one log record at a time in the configured 'format'; now with 'data_mode' the user can set 'single_record' (default) or 'chunk'. When the option 'chunk' is passed, the whole binary chunk is passed to the callback as a reference (it's up to the caller to unpack and validate the content) Signed-off-by: Eduardo Silva <[email protected]>
1 parent b588925 commit 615bdd4

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

plugins/out_lib/out_lib.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ static int configure(struct flb_out_lib_config *ctx,
4848
}
4949
}
5050

51-
tmp = flb_output_get_property("max_records", ins);
52-
if (tmp) {
53-
ctx->max_records = atoi(tmp);
51+
if (strcasecmp(ctx->data_mode_str, "single_record") == 0) {
52+
ctx->data_mode = FLB_DATA_MODE_SINGLE_RECORD;
53+
}
54+
else if (strcasecmp(ctx->data_mode_str, "chunk") == 0) {
55+
ctx->data_mode = FLB_DATA_MODE_CHUNK;
5456
}
5557
else {
56-
ctx->max_records = 0;
58+
flb_plg_error(ctx->ins, "Invalid data_mode: %s", ctx->data_mode_str);
59+
return -1;
5760
}
5861

5962
return 0;
@@ -85,6 +88,8 @@ static int out_lib_init(struct flb_output_instance *ins,
8588
}
8689
ctx->ins = ins;
8790

91+
flb_output_config_map_set(ins, (void *) ctx);
92+
8893
if (cb_data) {
8994
/* Set user callback and data */
9095
ctx->cb_func = cb_data->cb;
@@ -125,6 +130,16 @@ static void out_lib_flush(struct flb_event_chunk *event_chunk,
125130
(void) i_ins;
126131
(void) config;
127132

133+
/*
134+
* if the plugin is configured with data_mode = 'chunk', we pass the chunk
135+
* as a reference to the callback function.
136+
*/
137+
if (ctx->data_mode == FLB_DATA_MODE_CHUNK) {
138+
ctx->cb_func(event_chunk->data, event_chunk->size, ctx->cb_data);
139+
FLB_OUTPUT_RETURN(FLB_OK);
140+
}
141+
142+
/* Everything else here is for data_mode = 'single_record' */
128143
msgpack_unpacked_init(&result);
129144
while (msgpack_unpack_next(&result,
130145
event_chunk->data,
@@ -187,6 +202,7 @@ static void out_lib_flush(struct flb_event_chunk *event_chunk,
187202
flb_free(buf);
188203
data_for_user = out_buf;
189204
data_size = len;
205+
190206
#ifdef FLB_HAVE_METRICS
191207
}
192208
#endif
@@ -211,6 +227,30 @@ static int out_lib_exit(void *data, struct flb_config *config)
211227
return 0;
212228
}
213229

230+
/* Configuration properties map */
231+
static struct flb_config_map config_map[] = {
232+
{
233+
FLB_CONFIG_MAP_STR, "format", NULL,
234+
0, FLB_FALSE, 0,
235+
"Specifies the data format to be printed. Supported formats are "
236+
"'msgpack' or 'json', json_lines and json_stream."
237+
},
238+
239+
{
240+
FLB_CONFIG_MAP_INT, "max_records", NULL,
241+
0, FLB_TRUE, offsetof(struct flb_out_lib_config, max_records),
242+
"Specifies the maximum number of log records to be printed."
243+
},
244+
245+
{
246+
FLB_CONFIG_MAP_STR, "data_mode", "single_record",
247+
0, FLB_TRUE, offsetof(struct flb_out_lib_config, data_mode_str),
248+
},
249+
250+
/* EOF */
251+
{0}
252+
};
253+
214254
struct flb_output_plugin out_lib_plugin = {
215255
.name = "lib",
216256
.description = "Library mode Output",
@@ -219,4 +259,5 @@ struct flb_output_plugin out_lib_plugin = {
219259
.cb_exit = out_lib_exit,
220260
.event_type = FLB_OUTPUT_LOGS | FLB_OUTPUT_METRICS,
221261
.flags = 0,
262+
.config_map = config_map
222263
};

plugins/out_lib/out_lib.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ enum {
3131
#define FLB_FMT_STR_MSGPACK "msgpack"
3232
#define FLB_FMT_STR_JSON "json"
3333

34+
#define FLB_DATA_MODE_SINGLE_RECORD 0 /* "single_record" */
35+
#define FLB_DATA_MODE_CHUNK 1 /* "chunk" */
36+
3437
struct flb_out_lib_config {
3538
int format;
3639
int max_records;
40+
int data_mode;
41+
flb_sds_t data_mode_str;
3742
int (*cb_func)(void *record, size_t size, void *data);
3843
void *cb_data;
3944
struct flb_output_instance *ins;

0 commit comments

Comments
 (0)