Skip to content

Commit ad2beeb

Browse files
zshuang0316claude
andcommitted
input_log: fix input metrics double-count with conditional routing
With conditional routing active, each batch is fanned out into one chunk copy per matching route plus a base append. Every append called flb_input_chunk_append_raw(), which incremented cmt_records/cmt_bytes, so a batch matching N routes was counted N+1 times in fluentbit_input_records_total and fluentbit_input_bytes_total. Mark all per-route copies and the base append with FLB_INPUT_CHUNK_SKIP_INPUT_METRICS, then account the batch once in input_log_append_processed_internal() after at least one append is accepted. Counting after acceptance means a fully rejected batch (over mem_buf_limit with no space to free) is not recorded, while a partially accepted batch is not lost if a route copy pauses the input or the base append later fails. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: zshuang0316 <zshuang0316@163.com>
1 parent c3be242 commit ad2beeb

1 file changed

Lines changed: 68 additions & 16 deletions

File tree

src/flb_input_log.c

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,8 @@ static int split_and_append_route_payloads(struct flb_input_instance *ins,
11351135
size_t tag_len,
11361136
const void *buf,
11371137
size_t buf_size,
1138-
int local_append)
1138+
int local_append,
1139+
int *any_appended)
11391140
{
11401141
int ret;
11411142
int appended;
@@ -1397,18 +1398,21 @@ static int split_and_append_route_payloads(struct flb_input_instance *ins,
13971398
continue;
13981399
}
13991400

1401+
/* internal route copy: skip metrics, the batch is counted once */
14001402
if (local_append == FLB_TRUE) {
1401-
ret = flb_input_chunk_append_raw_local(ins,
1403+
ret = flb_input_chunk_append_raw_local_flags(ins,
14021404
FLB_INPUT_LOGS,
1405+
FLB_INPUT_CHUNK_SKIP_INPUT_METRICS,
14031406
payload->total_records,
14041407
payload->tag,
14051408
flb_sds_len(payload->tag),
14061409
payload->data,
14071410
payload->size);
14081411
}
14091412
else {
1410-
ret = flb_input_chunk_append_raw(ins,
1413+
ret = flb_input_chunk_append_raw_flags(ins,
14111414
FLB_INPUT_LOGS,
1415+
FLB_INPUT_CHUNK_SKIP_INPUT_METRICS,
14121416
payload->total_records,
14131417
payload->tag,
14141418
flb_sds_len(payload->tag),
@@ -1445,6 +1449,14 @@ static int split_and_append_route_payloads(struct flb_input_instance *ins,
14451449
return -1;
14461450
}
14471451

1452+
/*
1453+
* Report acceptance so the caller counts the batch once, even if a
1454+
* later route copy in this loop fails.
1455+
*/
1456+
if (any_appended != NULL) {
1457+
*any_appended = FLB_TRUE;
1458+
}
1459+
14481460
appended++;
14491461
}
14501462

@@ -1473,6 +1485,9 @@ static int input_log_append_processed_internal(struct flb_input_instance *ins,
14731485
int ret;
14741486
int conditional_result;
14751487
int conditional_handled = FLB_FALSE;
1488+
int conditional_active = FLB_FALSE;
1489+
int base_flags = 0;
1490+
int accepted = FLB_FALSE;
14761491
size_t dummy = 0;
14771492
const char *base_tag = tag;
14781493
size_t base_tag_len = tag_len;
@@ -1492,29 +1507,66 @@ static int input_log_append_processed_internal(struct flb_input_instance *ins,
14921507
base_tag_len = strlen(base_tag);
14931508
}
14941509

1495-
conditional_result = split_and_append_route_payloads(ins, records, tag, tag_len,
1496-
buf, buf_size,
1497-
local_append);
1498-
if (conditional_result < 0) {
1499-
return -1;
1510+
/*
1511+
* With conditional routing the batch is fanned out into per-route copies
1512+
* plus a base append, all skipping input metrics. It is accounted once
1513+
* below, after at least one append is accepted, so a fully rejected batch
1514+
* is not counted. The non-routed path keeps counting in
1515+
* flb_input_chunk_append_raw().
1516+
*/
1517+
conditional_active = (cfl_list_size(&ins->routes_direct) > 0 &&
1518+
input_has_conditional_routes(ins) == FLB_TRUE);
1519+
if (conditional_active == FLB_TRUE) {
1520+
base_flags = FLB_INPUT_CHUNK_SKIP_INPUT_METRICS;
15001521
}
15011522

1523+
conditional_result = split_and_append_route_payloads(ins, records, tag, tag_len,
1524+
buf, buf_size,
1525+
local_append, &accepted);
15021526
if (conditional_result > 0) {
15031527
conditional_handled = FLB_TRUE;
15041528
}
15051529

15061530
/*
1507-
* Always call flb_input_chunk_append_raw to ensure non-conditional routes
1508-
* receive data even when conditional routes exist. The conditional routing
1509-
* should be additive, not exclusive.
1531+
* Base append carries the full buffer for non-conditional routes (routing
1532+
* is additive). It is skipped on split failure.
15101533
*/
1511-
if (local_append == FLB_TRUE) {
1512-
ret = flb_input_chunk_append_raw_local(ins, FLB_INPUT_LOGS, records,
1513-
tag, tag_len, buf, buf_size);
1534+
if (conditional_result >= 0) {
1535+
if (local_append == FLB_TRUE) {
1536+
ret = flb_input_chunk_append_raw_local_flags(ins, FLB_INPUT_LOGS,
1537+
base_flags, records,
1538+
tag, tag_len, buf, buf_size);
1539+
}
1540+
else {
1541+
ret = flb_input_chunk_append_raw_flags(ins, FLB_INPUT_LOGS,
1542+
base_flags, records,
1543+
tag, tag_len, buf, buf_size);
1544+
}
1545+
if (ret == 0) {
1546+
accepted = FLB_TRUE;
1547+
}
15141548
}
15151549
else {
1516-
ret = flb_input_chunk_append_raw(ins, FLB_INPUT_LOGS, records,
1517-
tag, tag_len, buf, buf_size);
1550+
ret = -1;
1551+
}
1552+
1553+
#ifdef FLB_HAVE_METRICS
1554+
/* count the batch once, now that at least one append was accepted */
1555+
if (conditional_active == FLB_TRUE && accepted == FLB_TRUE &&
1556+
records > 0 && buf_size > 0) {
1557+
uint64_t ts = cfl_time_now();
1558+
char *name = (char *) flb_input_name(ins);
1559+
1560+
cmt_counter_add(ins->cmt_records, ts, records, 1, (char *[]) {name});
1561+
cmt_counter_add(ins->cmt_bytes, ts, buf_size, 1, (char *[]) {name});
1562+
1563+
flb_metrics_sum(FLB_METRIC_N_RECORDS, records, ins->metrics);
1564+
flb_metrics_sum(FLB_METRIC_N_BYTES, buf_size, ins->metrics);
1565+
}
1566+
#endif
1567+
1568+
if (conditional_result < 0) {
1569+
return -1;
15181570
}
15191571

15201572
if (ret == 0 && conditional_handled == FLB_TRUE && base_tag) {

0 commit comments

Comments
 (0)