Skip to content

Commit fb4b9ef

Browse files
committed
in_tail: flb_parser_do_encode_utf8 for UTF8 encoding and parsing in one step
Signed-off-by: Nigel Stewart <[email protected]>
1 parent e97dc53 commit fb4b9ef

File tree

7 files changed

+79
-19
lines changed

7 files changed

+79
-19
lines changed

include/fluent-bit/flb_encoder.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
#define FLB_ENCODER_H
2323

2424
#include <msgpack.h>
25+
#include "flb_parser.h"
2526

2627
typedef void *flb_encoder;
2728

2829
#ifdef FLB_HAVE_UTF8_ENCODER
2930
flb_encoder flb_get_encoder(const char *encoding);
3031
void flb_msgpack_encode_utf8(flb_encoder encoder, const char *module, msgpack_packer *pk, const void *b, size_t l);
32+
int flb_parser_do_encode_utf8(flb_encoder encoder, const char *module,
33+
struct flb_parser *parser, const char *buf, size_t length,
34+
void **out_buf, size_t *out_size, struct flb_time *out_time);
3135
#else
3236
static inline flb_encoder flb_get_encoder(const char *encoding)
3337
{
@@ -39,6 +43,13 @@ static inline void flb_msgpack_encode_utf8(flb_encoder encoder, const char *modu
3943
msgpack_pack_str(pk, l);
4044
msgpack_pack_str_body(pk, b, l);
4145
}
46+
47+
static inline void flb_parser_do_encode_utf8(flb_encoder encoder, const char *module,
48+
struct flb_parser *parser, const char *buf, size_t length,
49+
void **out_buf, size_t *out_size, struct flb_time *out_time)
50+
{
51+
return flb_parser_do(parser, buf, length, out_buf, out_size, out_time);
52+
}
4253
#endif
4354

4455
#endif /* FLB_ENCODER_H */

plugins/in_syslog/syslog_prot.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static inline int pack_line(struct flb_syslog *ctx,
4545

4646
msgpack_pack_array(&mp_pck, 2);
4747
flb_time_append_to_msgpack(time, &mp_pck, 0);
48-
flb_msgpack_encode_utf8(ctx->encoding, "in_syslog", &mp_pck, data, data_size);
48+
msgpack_sbuffer_write(&mp_sbuf, data, data_size);
4949

5050
flb_input_chunk_append_raw(ctx->i_ins, NULL, 0, mp_sbuf.data, mp_sbuf.size);
5151
msgpack_sbuffer_destroy(&mp_sbuf);
@@ -98,7 +98,7 @@ int syslog_prot_process(struct syslog_conn *conn)
9898
}
9999

100100
/* Process the string */
101-
ret = flb_parser_do(ctx->parser, p, len,
101+
ret = flb_parser_do_encode_utf8(ctx->encoding, "in_syslog", ctx->parser, p, len,
102102
&out_buf, &out_size, &out_time);
103103
if (ret >= 0) {
104104
pack_line(ctx, &out_time, out_buf, out_size);
@@ -130,7 +130,7 @@ int syslog_prot_process_udp(char *buf, size_t size, struct flb_syslog *ctx)
130130
size_t out_size;
131131
struct flb_time out_time = {0};
132132

133-
ret = flb_parser_do(ctx->parser, buf, size,
133+
ret = flb_parser_do_encode_utf8(ctx->encoding, "in_syslog", ctx->parser, buf, size,
134134
&out_buf, &out_size, &out_time);
135135
if (ret >= 0) {
136136
if (flb_time_to_double(&out_time) == 0) {

plugins/in_tail/tail_dockermode.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ void flb_tail_dmode_flush(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
295295
}
296296
#endif
297297
flb_time_get(&out_time);
298-
flb_tail_file_pack_line(mp_sbuf, mp_pck, &out_time,
298+
/* Assumed to be UTF-8, no need to encode? */
299+
flb_tail_file_pack_line(NULL, "in_tail", mp_sbuf, mp_pck, &out_time,
299300
repl_line, repl_line_len, file);
300301

301302
dmode_flush_end:

plugins/in_tail/tail_file.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <fluent-bit/flb_info.h>
2828
#include <fluent-bit/flb_input.h>
2929
#include <fluent-bit/flb_parser.h>
30-
#include <fluent-bit/flb_encoder.h>
3130
#ifdef FLB_HAVE_REGEX
3231
#include <fluent-bit/flb_regex.h>
3332
#include <fluent-bit/flb_hash.h>
@@ -183,7 +182,8 @@ int flb_tail_pack_line_map(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
183182
return 0;
184183
}
185184

186-
int flb_tail_file_pack_line(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
185+
int flb_tail_file_pack_line(flb_encoder encoder, const char *module,
186+
msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
187187
struct flb_time *time, char *data, size_t data_size,
188188
struct flb_tail_file *file)
189189
{
@@ -208,7 +208,7 @@ int flb_tail_file_pack_line(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
208208

209209
msgpack_pack_str(mp_pck, ctx->key_len);
210210
msgpack_pack_str_body(mp_pck, ctx->key, ctx->key_len);
211-
flb_msgpack_encode_utf8(ctx->encoding, "in_tail", mp_pck, data, data_size);
211+
flb_msgpack_encode_utf8(encoder, "in_tail", mp_pck, data, data_size);
212212

213213
return 0;
214214
}
@@ -302,7 +302,7 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
302302
#ifdef FLB_HAVE_PARSER
303303
if (ctx->parser) {
304304
/* Common parser (non-multiline) */
305-
ret = flb_parser_do(ctx->parser, line, line_len,
305+
ret = flb_parser_do_encode_utf8(ctx->encoding, "in_tail", ctx->parser, line, line_len,
306306
&out_buf, &out_size, &out_time);
307307
if (ret >= 0) {
308308
if (flb_time_to_double(&out_time) == 0) {
@@ -328,7 +328,8 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
328328
else {
329329
/* Parser failed, pack raw text */
330330
flb_time_get(&out_time);
331-
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
331+
flb_tail_file_pack_line(ctx->encoding, "in_tail",
332+
out_sbuf, out_pck, &out_time,
332333
data, len, file);
333334
}
334335
}
@@ -342,7 +343,8 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
342343
flb_tail_mult_flush(out_sbuf, out_pck, file, ctx);
343344

344345
flb_time_get(&out_time);
345-
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
346+
flb_tail_file_pack_line(ctx->encoding, "in_tail",
347+
out_sbuf, out_pck, &out_time,
346348
line, line_len, file);
347349
}
348350
else if (ret == FLB_TAIL_MULT_MORE) {
@@ -355,12 +357,14 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
355357
}
356358
else {
357359
flb_time_get(&out_time);
358-
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
360+
flb_tail_file_pack_line(ctx->encoding, "in_tail",
361+
out_sbuf, out_pck, &out_time,
359362
line, line_len, file);
360363
}
361364
#else
362365
flb_time_get(&out_time);
363-
flb_tail_file_pack_line(out_sbuf, out_pck, &out_time,
366+
flb_tail_file_pack_line(ctx->encoding, "in_tail",
367+
out_sbuf, out_pck, &out_time,
364368
line, line_len, file);
365369
#endif
366370

plugins/in_tail/tail_file.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <fluent-bit/flb_compat.h>
2828
#include <fluent-bit/flb_input.h>
29+
#include <fluent-bit/flb_encoder.h>
2930

3031
#include "tail.h"
3132
#include "tail_fs.h"
@@ -63,8 +64,8 @@ int flb_tail_file_rotated_purge(struct flb_input_instance *i_ins,
6364
int flb_tail_pack_line_map(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
6465
struct flb_time *time, char **data,
6566
size_t *data_size, struct flb_tail_file *file);
66-
int flb_tail_file_pack_line(msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
67+
int flb_tail_file_pack_line(flb_encoder encoder, const char *module,
68+
msgpack_sbuffer *mp_sbuf, msgpack_packer *mp_pck,
6769
struct flb_time *time, char *data, size_t data_size,
6870
struct flb_tail_file *file);
69-
7071
#endif

plugins/in_tail/tail_multiline.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static int pack_line(char *data, size_t data_size, struct flb_tail_file *file,
137137
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
138138
flb_time_get(&out_time);
139139

140-
flb_tail_file_pack_line(&mp_sbuf, &mp_pck, &out_time, data, data_size, file);
140+
flb_tail_file_pack_line(NULL, "in_tail", &mp_sbuf, &mp_pck, &out_time, data, data_size, file);
141141
flb_input_chunk_append_raw(ctx->i_ins,
142142
file->tag_buf,
143143
file->tag_len,
@@ -239,7 +239,8 @@ static inline void flb_tail_mult_append_raw(char *buf, int size,
239239
struct flb_tail_config *config)
240240
{
241241
/* Append the raw string */
242-
flb_msgpack_encode_utf8(config->encoding, "in_tail", &file->mult_pck, buf, size);
242+
msgpack_pack_str(&file->mult_pck, size);
243+
msgpack_pack_str_body(&file->mult_pck, buf, size);
243244
}
244245

245246
/* Check if the last key value type of a map is string or not */
@@ -284,7 +285,7 @@ int flb_tail_mult_process_content(time_t now,
284285
msgpack_unpacked result;
285286

286287
/* Always check if this line is the beginning of a new multiline message */
287-
ret = flb_parser_do(ctx->mult_parser_firstline,
288+
ret = flb_parser_do_encode_utf8(ctx->encoding, "in_tail", ctx->mult_parser_firstline,
288289
buf, len,
289290
&out_buf, &out_size, &out_time);
290291
if (ret >= 0) {

src/flb_encoder.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ void flb_msgpack_encode_utf8(flb_encoder encoder, const char *module, msgpack_pa
5757
if (buffer && !tutf8e_encoder_buffer_encode(encoder, b, l, buffer, &size) && size) {
5858
msgpack_pack_str(pk, size);
5959
msgpack_pack_str_body(pk, buffer, size);
60-
free(buffer);
60+
flb_free(buffer);
6161
return;
6262
}
6363
/* Not expecting to get here ordinarily */
64-
free(buffer);
64+
flb_free(buffer);
6565
flb_warn("[%s] failed to encode to UTF8", module);
6666
}
6767
}
@@ -74,5 +74,47 @@ void flb_msgpack_encode_utf8(flb_encoder encoder, const char *module, msgpack_pa
7474
msgpack_pack_str(pk, l);
7575
msgpack_pack_str_body(pk, b, l);
7676
}
77+
78+
int flb_parser_do_encode_utf8(flb_encoder encoder, const char *module,
79+
struct flb_parser *parser, const char *b, size_t l,
80+
void **out_buf, size_t *out_size, struct flb_time *out_time)
81+
{
82+
if (encoder) {
83+
size_t size = 0;
84+
if (!tutf8e_encoder_buffer_length(encoder, b, l, &size) && size) {
85+
/* Already UTF8 encoded? */
86+
if (size == l) {
87+
}
88+
/* Small enough for encoding to stack? */
89+
else if (size<=TUTF8_BUFFER_SIZE) {
90+
char buffer[TUTF8_BUFFER_SIZE];
91+
if (!tutf8e_encoder_buffer_encode(encoder, b, l, buffer, &size) && size) {
92+
return flb_parser_do(parser, buffer, size, out_buf, out_size, out_time);
93+
}
94+
/* Not expecting to get here ordinarily */
95+
flb_warn("[%s] failed to encode to UTF8", module);
96+
}
97+
/* malloc/free the encoded copy */
98+
else {
99+
char *buffer = (char *) flb_malloc(size);
100+
if (buffer && !tutf8e_encoder_buffer_encode(encoder, b, l, buffer, &size) && size) {
101+
int ret = flb_parser_do(parser, buffer, size, out_buf, out_size, out_time);
102+
flb_free(buffer);
103+
return ret;
104+
}
105+
/* Not expecting to get here ordinarily */
106+
flb_free(buffer);
107+
flb_warn("[%s] failed to encode to UTF8", module);
108+
}
109+
}
110+
else {
111+
flb_warn("[%s] failed to encode to UTF8", module);
112+
}
113+
}
114+
115+
/* Could not or need not encode to UTF8 */
116+
return flb_parser_do(parser, b, l, out_buf, out_size, out_time);
117+
}
118+
77119
#endif
78120

0 commit comments

Comments
 (0)