Skip to content

Commit 1205af9

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 1205af9

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

plugins/in_tail/tail_file.c

Lines changed: 1 addition & 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
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 5000
5+
rule "start_state" "/(^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})(.*)/" "cont"
6+
rule "cont" "/^(?!\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}).*/" "cont"

tests/runtime/in_tail.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,90 @@ 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+
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));
1418+
if(!TEST_CHECK(ret >= 0)) {
1419+
TEST_MSG("snprintf failed");
1420+
exit(EXIT_FAILURE);
1421+
}
1422+
1423+
ctx = test_tail_ctx_create(&cb_data, &file[0], sizeof(file)/sizeof(char *), FLB_TRUE);
1424+
if (!TEST_CHECK(ctx != NULL)) {
1425+
TEST_MSG("test_ctx_create failed");
1426+
exit(EXIT_FAILURE);
1427+
}
1428+
1429+
ret = flb_service_set(ctx->flb, "Parsers_File", DPATH "/parsers_multiline.conf", NULL);
1430+
TEST_CHECK(ret == 0);
1431+
1432+
ret = flb_input_set(ctx->flb, ctx->o_ffd,
1433+
"path", file[0],
1434+
"offset_key", offset_key,
1435+
"multiline.parser", "multiline-regex",
1436+
NULL);
1437+
TEST_CHECK(ret == 0);
1438+
1439+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
1440+
"format", "json",
1441+
NULL);
1442+
TEST_CHECK(ret == 0);
1443+
1444+
ret = write_msg(ctx, msg_before_tail, strlen(msg_before_tail));
1445+
if (!TEST_CHECK(ret > 0)) {
1446+
test_tail_ctx_destroy(ctx);
1447+
exit(EXIT_FAILURE);
1448+
}
1449+
1450+
ret = write_msg(ctx, msg_before_tail2, strlen(msg_before_tail2));
1451+
if (!TEST_CHECK(ret > 0)) {
1452+
test_tail_ctx_destroy(ctx);
1453+
exit(EXIT_FAILURE);
1454+
}
1455+
1456+
/* Start the engine */
1457+
ret = flb_start(ctx->flb);
1458+
TEST_CHECK(ret == 0);
1459+
1460+
ret = write_msg(ctx, msg_final, strlen(msg_final));
1461+
if (!TEST_CHECK(ret > 0)) {
1462+
test_tail_ctx_destroy(ctx);
1463+
exit(EXIT_FAILURE);
1464+
}
1465+
1466+
/* waiting to flush */
1467+
flb_time_msleep(500);
1468+
1469+
num = get_output_num();
1470+
if (!TEST_CHECK(num > 0)) {
1471+
TEST_MSG("no outputs");
1472+
}
1473+
1474+
test_tail_ctx_destroy(ctx);
1475+
}
1476+
13931477
void flb_test_skip_empty_lines()
13941478
{
13951479
struct flb_lib_out_cb cb_data;
@@ -2345,6 +2429,7 @@ TEST_LIST = {
23452429
{"path_key", flb_test_path_key},
23462430
{"exclude_path", flb_test_exclude_path},
23472431
{"offset_key", flb_test_offset_key},
2432+
{"multiline_offset_key", flb_test_multiline_offset_key},
23482433
{"skip_empty_lines", flb_test_skip_empty_lines},
23492434
{"skip_empty_lines_crlf", flb_test_skip_empty_lines_crlf},
23502435
{"ignore_older", flb_test_ignore_older},

0 commit comments

Comments
 (0)