Skip to content

Commit 3d12f63

Browse files
committed
in_tail: add flb_encoding support to in_tail-plugin
* Adds new option: "encoding" to in_tail. * If encoding fails. message is skipped. Signed-off-by: Jukka Pihl <[email protected]>
1 parent ed9a5af commit 3d12f63

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

plugins/in_tail/tail.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,14 @@ static struct flb_config_map config_map[] = {
613613
},
614614
#endif
615615

616+
#ifdef FLB_HAVE_UTF8_ENCODER
617+
{
618+
FLB_CONFIG_MAP_STR, "encoding", NULL,
619+
0, FLB_FALSE, 0,
620+
"specify the charset encoder to decode message",
621+
},
622+
#endif
623+
616624
/* Multiline Options */
617625
#ifdef FLB_HAVE_PARSER
618626
{

plugins/in_tail/tail_config.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *ins,
5656
#ifdef FLB_HAVE_SQLDB
5757
ctx->db_sync = 1; /* sqlite sync 'normal' */
5858
#endif
59+
#ifdef FLB_HAVE_UTF8_ENCODER
60+
ctx->encoding = NULL;
61+
#endif
5962

6063
/* Load the config map */
6164
ret = flb_input_config_map_set(ins, (void *) ctx);
@@ -151,6 +154,18 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *ins,
151154
}
152155
#endif
153156

157+
#ifdef FLB_HAVE_UTF8_ENCODER
158+
tmp = flb_input_get_property("encoding", ins);
159+
if (tmp) {
160+
ctx->encoding = flb_encoding_open(tmp);
161+
if (!ctx->encoding) {
162+
flb_plg_error(ctx->ins,"illegal encoding: %s", tmp);
163+
flb_tail_config_destroy(ctx);
164+
return NULL;
165+
}
166+
}
167+
#endif
168+
154169
/* Config: Docker mode */
155170
if(ctx->docker_mode == FLB_TRUE) {
156171
ret = flb_tail_dmode_create(ctx, ins, config);
@@ -344,6 +359,12 @@ int flb_tail_config_destroy(struct flb_tail_config *config)
344359
}
345360
#endif
346361

362+
#ifdef FLB_HAVE_UTF8_ENCODER
363+
if(config->encoding) {
364+
flb_encoding_close(config->encoding);
365+
}
366+
#endif
367+
347368
flb_free(config);
348369
return 0;
349370
}

plugins/in_tail/tail_config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#ifdef FLB_HAVE_REGEX
3030
#include <fluent-bit/flb_regex.h>
3131
#endif
32+
#ifdef FLB_HAVE_UTF8_ENCODER
33+
#include <fluent-bit/flb_encoding.h>
34+
#endif
3235

3336
/* Metrics */
3437
#ifdef FLB_HAVE_METRICS
@@ -94,6 +97,10 @@ struct flb_tail_config {
9497
sqlite3_stmt *stmt_offset;
9598
#endif
9699

100+
#ifdef FLB_HAVE_UTF8_ENCODER
101+
struct flb_encoding *encoding;
102+
#endif
103+
97104
/* Parser / Format */
98105
struct flb_parser *parser;
99106

plugins/in_tail/tail_file.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
221221
msgpack_sbuffer *out_sbuf;
222222
msgpack_packer *out_pck;
223223
struct flb_tail_config *ctx = file->config;
224+
#ifdef FLB_HAVE_UTF8_ENCODER
225+
char *decoded;
226+
size_t decoded_len;
227+
#endif
224228

225229
/* Create a temporary msgpack buffer */
226230
msgpack_sbuffer_init(&mp_sbuf);
@@ -262,6 +266,20 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
262266
line = data;
263267
line_len = len - crlf;
264268
repl_line = NULL;
269+
270+
#ifdef FLB_HAVE_UTF8_ENCODER
271+
decoded = NULL;
272+
if(ctx->encoding) {
273+
ret = flb_encoding_decode(ctx->encoding, line, line_len, &decoded, &decoded_len);
274+
if (ret != FLB_ENCODING_SUCCESS) {
275+
flb_plg_error(ctx->ins, "encoding failed '%.*s'", line_len, line);
276+
goto go_next;
277+
}
278+
line = decoded;
279+
line_len = decoded_len;
280+
}
281+
#endif
282+
265283
if (ctx->docker_mode) {
266284
ret = flb_tail_dmode_process_content(now, line, line_len,
267285
&repl_line, &repl_line_len,
@@ -306,7 +324,7 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
306324
/* Parser failed, pack raw text */
307325
flb_time_get(&out_time);
308326
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
309-
data, len, file, processed_bytes);
327+
line, len, file, processed_bytes);
310328
}
311329
}
312330
else if (ctx->multiline == FLB_TRUE) {
@@ -350,6 +368,12 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
350368
processed_bytes += len + 1;
351369
file->parsed = 0;
352370
lines++;
371+
#ifdef FLB_HAVE_UTF8_ENCODER
372+
if(decoded) {
373+
flb_free(decoded);
374+
decoded = NULL;
375+
}
376+
#endif
353377
}
354378
file->parsed = file->buf_len;
355379
*bytes = processed_bytes;
@@ -362,6 +386,7 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
362386
out_sbuf->size);
363387

364388
msgpack_sbuffer_destroy(out_sbuf);
389+
365390
return lines;
366391
}
367392

0 commit comments

Comments
 (0)