Skip to content

Commit 81c998b

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 84176d1 commit 81c998b

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
@@ -620,6 +620,14 @@ static struct flb_config_map config_map[] = {
620620
},
621621
#endif
622622

623+
#ifdef FLB_HAVE_UTF8_ENCODER
624+
{
625+
FLB_CONFIG_MAP_STR, "encoding", NULL,
626+
0, FLB_FALSE, 0,
627+
"specify the charset encoder to decode message",
628+
},
629+
#endif
630+
623631
/* Multiline Options */
624632
#ifdef FLB_HAVE_PARSER
625633
{

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
@@ -97,6 +100,10 @@ struct flb_tail_config {
97100
sqlite3_stmt *stmt_offset;
98101
#endif
99102

103+
#ifdef FLB_HAVE_UTF8_ENCODER
104+
struct flb_encoding *encoding;
105+
#endif
106+
100107
/* Parser / Format */
101108
struct flb_parser *parser;
102109

plugins/in_tail/tail_file.c

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

226230
/* Create a temporary msgpack buffer */
227231
msgpack_sbuffer_init(&mp_sbuf);
@@ -269,6 +273,20 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
269273
line = data;
270274
line_len = len - crlf;
271275
repl_line = NULL;
276+
277+
#ifdef FLB_HAVE_UTF8_ENCODER
278+
decoded = NULL;
279+
if(ctx->encoding) {
280+
ret = flb_encoding_decode(ctx->encoding, line, line_len, &decoded, &decoded_len);
281+
if (ret != FLB_ENCODING_SUCCESS) {
282+
flb_plg_error(ctx->ins, "encoding failed '%.*s'", line_len, line);
283+
goto go_next;
284+
}
285+
line = decoded;
286+
line_len = decoded_len;
287+
}
288+
#endif
289+
272290
if (ctx->docker_mode) {
273291
ret = flb_tail_dmode_process_content(now, line, line_len,
274292
&repl_line, &repl_line_len,
@@ -313,7 +331,7 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
313331
/* Parser failed, pack raw text */
314332
flb_time_get(&out_time);
315333
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
316-
data, len, file, processed_bytes);
334+
line, len, file, processed_bytes);
317335
}
318336
}
319337
else if (ctx->multiline == FLB_TRUE) {
@@ -357,6 +375,12 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
357375
processed_bytes += len + 1;
358376
file->parsed = 0;
359377
lines++;
378+
#ifdef FLB_HAVE_UTF8_ENCODER
379+
if(decoded) {
380+
flb_free(decoded);
381+
decoded = NULL;
382+
}
383+
#endif
360384
}
361385
file->parsed = file->buf_len;
362386

@@ -377,6 +401,7 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
377401
}
378402

379403
msgpack_sbuffer_destroy(out_sbuf);
404+
380405
return lines;
381406
}
382407

0 commit comments

Comments
 (0)