Skip to content

Clone labels before pipeline processing to fix batch structured_metadata bug - #218

Open
mampersat wants to merge 3 commits into
mainfrom
fix/batch-shared-labels-map
Open

mampersat wants to merge 3 commits into
mainfrom
fix/batch-shared-labels-map

Conversation

@mampersat

@mampersat mampersat commented Sep 18, 2026

Copy link
Copy Markdown

Summary

Clone entry.labels to survive downstream mutation

Test plan

  • Added Test_batch_add_SharedLabelsAcrossBatch, which batches 3 entries sharing one labels map through a structured_metadata stage and asserts all 3 keep the field.
  • Verified the new test fails on the pre-fix code with the exact reported symptom (2nd/3rd entries missing the field), and passes with the fix.
  • go test ./pkg/... passes.

…ata bug

structured_metadata mutates Entry.Labels in place, deleting a label once
it's promoted to structured metadata. parseCWEvent and processLogEvents
build one labels map per invocation and pass it by reference to every
entry in a batch, so the delete from the first entry was visible to every
later entry sharing that map -- only the first entry in a batch kept the
field.
@cla-assistant

cla-assistant Bot commented Sep 18, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cla-assistant

cla-assistant Bot commented Sep 18, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@mampersat
mampersat marked this pull request as ready for review September 18, 2026 16:57
Comment thread pkg/promtail_test.go
processor: pipeline,
}

batchSize = 131072 // large enough that add() never flushes mid-test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the precedent for this magic number? Should it be based on a config setting or something?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tristanburgess tristanburgess Sep 18, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks, what's the precedent for that magic number 😆
Can they both be commented with reference to documentation etc or fixed to point to something that's easier to know the correctness of like a config setting?

Comment thread pkg/promtail_test.go
// events), parseCWEvent/processLogEvents build one labels map and reuse it, by reference, for
// every entry{} in the batch. The structured_metadata stage mutates Entry.Labels in place
// (deleting a label once it's promoted to structured metadata), so without batch.add cloning
// e.labels first, only the first entry in the batch keeps the field -- every later entry's

@tristanburgess tristanburgess Sep 18, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only the first entry in the batch keeps the field

Why would this be true if every entry{} in the batch shares the same reference? Wouldn't they all see the mutated map?

Comment thread pkg/promtail_test.go
// Test_batch_add_SharedLabelsAcrossBatch reproduces
// https://github.com/grafana/support-escalations/issues/24182: when a Lambda invocation
// receives multiple log events in one batch (e.g. a CloudWatch put-log-events call with several
// events), parseCWEvent/processLogEvents build one labels map and reuse it, by reference, for

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseCWEvent/processLogEvents build one labels map and reuse it, by reference

Should parseCWEvent/processLogEvents be making the map copy for entry{}s in the first place then instead of setting up a reference only for it to be copied downstream? The semantics are a bit cleaner that way IMO

Alternatively, is it possible/would it make sense to have processing fix up the labels map before hand and have a separate collection for structured metadata entries, and pass both of those collections by reference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants