Skip to content

Commit 145814e

Browse files
committed
encoding: 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 d4986e8 commit 145814e

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
@@ -568,6 +568,14 @@ static struct flb_config_map config_map[] = {
568568
},
569569
#endif
570570

571+
#ifdef FLB_HAVE_UTF8_ENCODER
572+
{
573+
FLB_CONFIG_MAP_STR, "encoding", NULL,
574+
0, FLB_FALSE, 0,
575+
"specify the charset encoder to decode message",
576+
},
577+
#endif
578+
571579
/* Multiline Options */
572580
#ifdef FLB_HAVE_PARSER
573581
{

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;
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);
@@ -290,6 +305,12 @@ int flb_tail_config_destroy(struct flb_tail_config *config)
290305
}
291306
#endif
292307

308+
#ifdef FLB_HAVE_UTF8_ENCODER
309+
if(config->encoding) {
310+
flb_encoding_close(config->encoding);
311+
}
312+
#endif
313+
293314
flb_free(config);
294315
return 0;
295316
}

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
@@ -90,6 +93,10 @@ struct flb_tail_config {
9093
sqlite3_stmt *stmt_offset;
9194
#endif
9295

96+
#ifdef FLB_HAVE_UTF8_ENCODER
97+
struct flb_encoding *encoding;
98+
#endif
99+
93100
/* Parser / Format */
94101
struct flb_parser *parser;
95102

plugins/in_tail/tail_file.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
196196
msgpack_sbuffer *out_sbuf;
197197
msgpack_packer *out_pck;
198198
struct flb_tail_config *ctx = file->config;
199+
#ifdef FLB_HAVE_UTF8_ENCODER
200+
char *decoded;
201+
size_t decoded_len;
202+
#endif
199203

200204
/* Create a temporal msgpack buffer */
201205
msgpack_sbuffer_init(&mp_sbuf);
@@ -238,6 +242,20 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
238242
line_len = len - crlf;
239243
repl_line = NULL;
240244

245+
246+
#ifdef FLB_HAVE_UTF8_ENCODER
247+
decoded = NULL;
248+
if(ctx->encoding) {
249+
ret = flb_encoding_decode(ctx->encoding, line, line_len, &decoded, &decoded_len);
250+
if (ret != FLB_ENCODING_SUCCESS) {
251+
flb_plg_error(ctx->ins, "encoding failed '%.*s'", line_len, line);
252+
goto go_next;
253+
}
254+
line = decoded;
255+
line_len = decoded_len;
256+
}
257+
#endif
258+
241259
if (ctx->docker_mode) {
242260
ret = flb_tail_dmode_process_content(now, line, line_len,
243261
&repl_line, &repl_line_len,
@@ -289,7 +307,7 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
289307
/* Parser failed, pack raw text */
290308
flb_time_get(&out_time);
291309
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
292-
data, len, file);
310+
line, len, file);
293311
}
294312
}
295313
else if (ctx->multiline == FLB_TRUE) {
@@ -332,6 +350,12 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
332350
processed_bytes += len + 1;
333351
file->parsed = 0;
334352
lines++;
353+
#ifdef FLB_HAVE_UTF8_ENCODER
354+
if(decoded) {
355+
flb_free(decoded);
356+
decoded = NULL;
357+
}
358+
#endif
335359
}
336360
file->parsed = file->buf_len;
337361
*bytes = processed_bytes;
@@ -344,6 +368,7 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
344368
out_sbuf->size);
345369

346370
msgpack_sbuffer_destroy(out_sbuf);
371+
347372
return lines;
348373
}
349374

0 commit comments

Comments
 (0)