Skip to content

Commit 3a8abd9

Browse files
committed
ml: tests: Detect truncation ocurrence in multiline testcase.
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent e40c50b commit 3a8abd9

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

include/fluent-bit/multiline/flb_ml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
/* Return codes */
5959
#define FLB_MULTILINE_OK 0
60+
#define FLB_MULTILINE_PROCESSED 1 /* Reserved */
6061
#define FLB_MULTILINE_TRUNCATED 2
6162

6263
/* Maximum number of groups per stream */

src/multiline/flb_ml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ static int process_append(struct flb_ml_parser_ins *parser_i,
491491
if (ret == FLB_FALSE) {
492492
return -1;
493493
}
494-
return 0;
494+
return ret;
495495
}
496496

497497
static int ml_append_try_parser_type_text(struct flb_ml_parser_ins *parser,

src/multiline/flb_ml_rule.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,6 @@ int flb_ml_rule_process(struct flb_ml_parser *ml_parser,
417417

418418
/* Copy full map content in stream buffer */
419419
flb_ml_register_context(group, tm, full_map);
420-
421-
return 0;
422420
}
423421
}
424422

tests/internal/multiline.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,24 +1466,65 @@ static void test_buffer_limit_truncation()
14661466
uint64_t stream_id;
14671467
struct flb_config *config;
14681468
struct flb_ml *ml;
1469+
struct flb_ml_parser *mlp;
14691470
struct flb_ml_parser_ins *mlp_i;
1471+
struct flb_parser *p;
14701472
struct flb_time tm;
14711473

1474+
/*
1475+
* A realistic Docker log where the content of the "log" field will be
1476+
* concatenated, and that concatenated buffer is what should be truncated.
1477+
*/
1478+
char *line1 = "{\"log\": \"12345678901234567890\", \"stream\": \"stdout\"}";
1479+
char *line2 = "{\"log\": \"abcdefghijklmnopqrstuvwxyz\", \"stream\": \"stdout\"}";
1480+
14721481
config = flb_config_init();
1473-
config->multiline_buffer_limit = 32;
1482+
/* The buffer limit is for the concatenated 'log' content, not the full JSON */
1483+
config->multiline_buffer_limit = 80;
1484+
1485+
/* Use the dummy 'docker' parser for JSON extraction */
1486+
p = flb_parser_get("docker", config);
14741487

1488+
/* This parser will trigger on any content, ensuring concatenation. */
14751489
ml = flb_ml_create(config, "limit-test");
14761490
TEST_CHECK(ml != NULL);
14771491

1478-
mlp_i = flb_ml_parser_instance_create(ml, "docker");
1492+
mlp = flb_ml_parser_create(config, "test-concat", FLB_ML_REGEX,
1493+
NULL, FLB_FALSE, 1000,
1494+
"log", NULL, NULL,
1495+
p, "docker");
1496+
TEST_CHECK(mlp != NULL);
1497+
1498+
/* Define rules that will always match the test data */
1499+
ret = flb_ml_rule_create(mlp, "start_state", "/./", "cont", NULL);
1500+
TEST_CHECK(ret == 0);
1501+
ret = flb_ml_rule_create(mlp, "cont", "/./", "cont", NULL);
1502+
TEST_CHECK(ret == 0);
1503+
1504+
/* Finalize parser initialization */
1505+
ret = flb_ml_parser_init(mlp);
1506+
TEST_CHECK(ret == 0);
1507+
1508+
mlp_i = flb_ml_parser_instance_create(ml, "test-concat");
14791509
TEST_CHECK(mlp_i != NULL);
14801510

14811511
ret = flb_ml_stream_create(ml, "test", -1, flush_callback, NULL, &stream_id);
14821512
TEST_CHECK(ret == 0);
14831513

14841514
flb_time_get(&tm);
1485-
ret = flb_ml_append_text(ml, stream_id, &tm,
1486-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 36);
1515+
1516+
/* Append the first line. It will match the 'start_state' and start a block. */
1517+
ret = flb_ml_append_text(ml, stream_id, &tm, line1, strlen(line1));
1518+
printf("ret (line 1) = %d\n", ret);
1519+
TEST_CHECK(ret == FLB_MULTILINE_OK);
1520+
1521+
/*
1522+
* Append the second line. This will match the 'cont' state and concatenate.
1523+
* The concatenation (20 + 1 + 26 bytes) will exceed the 32-byte limit
1524+
* and correctly trigger the truncation logic.
1525+
*/
1526+
ret = flb_ml_append_text(ml, stream_id, &tm, line2, strlen(line2));
1527+
printf("ret (line 2) = %d\n", ret);
14871528
TEST_CHECK(ret == FLB_MULTILINE_TRUNCATED);
14881529

14891530
flb_ml_destroy(ml);

0 commit comments

Comments
 (0)