Skip to content

Commit cf6ccc7

Browse files
committed
in_tail: fix last_processed_bytes calculation
Fix last_processed_bytes calculation in multiple line scenario. Signed-off-by: zshuang0316 <[email protected]>
1 parent 42e29d6 commit cf6ccc7

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

plugins/in_tail/tail_file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
648648
processed_bytes += len + 1;
649649
lines++;
650650
file->parsed = 0;
651-
file->last_processed_bytes += processed_bytes;
651+
file->last_processed_bytes = processed_bytes;
652652
}
653653

654654
#ifdef FLB_HAVE_UNICODE_ENCODER
@@ -1644,6 +1644,7 @@ int flb_tail_file_chunk(struct flb_tail_file *file)
16441644

16451645
/* Adjust the file offset and buffer */
16461646
file->stream_offset += processed_bytes;
1647+
file->last_processed_bytes = 0;
16471648
consume_bytes(file->buf_data, processed_bytes, file->buf_len);
16481649
file->buf_len -= processed_bytes;
16491650
file->buf_data[file->buf_len] = '\0';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[MULTILINE_PARSER]
2+
name multiline-regex
3+
type regex
4+
flush_timeout 2000
5+
rule "start_state" "/^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}\]/" "cont"
6+
rule "cont" "/^(?!\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}\]).*/" "cont"

tests/runtime/in_tail.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,91 @@ void flb_test_offset_key()
13901390
test_tail_ctx_destroy(ctx);
13911391
}
13921392

1393+
void flb_test_multiline_offset_key()
1394+
{
1395+
struct flb_lib_out_cb cb_data;
1396+
struct test_tail_ctx *ctx;
1397+
char *file[] = {"multiline_offset.log"};
1398+
char *offset_key = "OffsetKey";
1399+
char *msg_before_tail = "[2025-06-16 20:42:22,291] INFO - aaaaaaaaaaa";
1400+
char *msg_before_tail2 = "[2025-06-16 20:42:22,500] Error";
1401+
char *msg_final = "[2025-06-16 20:45:29,234] Fatal";
1402+
char expected_msg[1024] = {0};
1403+
int ret;
1404+
int num;
1405+
1406+
char *expected_strs[] = {msg_final, &expected_msg[0]};
1407+
struct str_list expected = {
1408+
.size = sizeof(expected_strs)/sizeof(char*),
1409+
.lists = &expected_strs[0],
1410+
};
1411+
1412+
clear_output_num();
1413+
1414+
cb_data.cb = cb_check_json_str_list;
1415+
cb_data.data = &expected;
1416+
1417+
// multiline offset is at the end of the message
1418+
ret = snprintf(&expected_msg[0], sizeof(expected_msg), "\"%s\":%ld", offset_key, strlen(msg_before_tail)+strlen(NEW_LINE)+strlen(msg_before_tail2)+strlen(NEW_LINE)+strlen(msg_final)+strlen(NEW_LINE));
1419+
if(!TEST_CHECK(ret >= 0)) {
1420+
TEST_MSG("snprintf failed");
1421+
exit(EXIT_FAILURE);
1422+
}
1423+
1424+
ctx = test_tail_ctx_create(&cb_data, &file[0], sizeof(file)/sizeof(char *), FLB_TRUE);
1425+
if (!TEST_CHECK(ctx != NULL)) {
1426+
TEST_MSG("test_ctx_create failed");
1427+
exit(EXIT_FAILURE);
1428+
}
1429+
1430+
ret = flb_service_set(ctx->flb, "Parsers_File", DPATH "/parsers_multiline.conf", NULL);
1431+
TEST_CHECK(ret == 0);
1432+
1433+
ret = flb_input_set(ctx->flb, ctx->o_ffd,
1434+
"path", file[0],
1435+
"offset_key", offset_key,
1436+
"multiline.parser", "multiline-regex",
1437+
NULL);
1438+
TEST_CHECK(ret == 0);
1439+
1440+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
1441+
"format", "json",
1442+
NULL);
1443+
TEST_CHECK(ret == 0);
1444+
1445+
ret = write_msg(ctx, msg_before_tail, strlen(msg_before_tail));
1446+
if (!TEST_CHECK(ret > 0)) {
1447+
test_tail_ctx_destroy(ctx);
1448+
exit(EXIT_FAILURE);
1449+
}
1450+
1451+
ret = write_msg(ctx, msg_before_tail2, strlen(msg_before_tail2));
1452+
if (!TEST_CHECK(ret > 0)) {
1453+
test_tail_ctx_destroy(ctx);
1454+
exit(EXIT_FAILURE);
1455+
}
1456+
1457+
/* Start the engine */
1458+
ret = flb_start(ctx->flb);
1459+
TEST_CHECK(ret == 0);
1460+
1461+
ret = write_msg(ctx, msg_final, strlen(msg_final));
1462+
if (!TEST_CHECK(ret > 0)) {
1463+
test_tail_ctx_destroy(ctx);
1464+
exit(EXIT_FAILURE);
1465+
}
1466+
1467+
/* wait up to 5s for at least one output */
1468+
wait_num_with_timeout(5000, &num);
1469+
1470+
num = get_output_num();
1471+
if (!TEST_CHECK(num > 0)) {
1472+
TEST_MSG("no outputs");
1473+
}
1474+
1475+
test_tail_ctx_destroy(ctx);
1476+
}
1477+
13931478
void flb_test_skip_empty_lines()
13941479
{
13951480
struct flb_lib_out_cb cb_data;
@@ -2345,6 +2430,7 @@ TEST_LIST = {
23452430
{"path_key", flb_test_path_key},
23462431
{"exclude_path", flb_test_exclude_path},
23472432
{"offset_key", flb_test_offset_key},
2433+
{"multiline_offset_key", flb_test_multiline_offset_key},
23482434
{"skip_empty_lines", flb_test_skip_empty_lines},
23492435
{"skip_empty_lines_crlf", flb_test_skip_empty_lines_crlf},
23502436
{"ignore_older", flb_test_ignore_older},

0 commit comments

Comments
 (0)