Skip to content

GH-4306: Stop listener observations for filtered records#4330

Merged
artembilan merged 1 commit into
spring-projects:mainfrom
BDARJH:fix/gh-4306-observation-filter-leak
Mar 6, 2026
Merged

GH-4306: Stop listener observations for filtered records#4330
artembilan merged 1 commit into
spring-projects:mainfrom
BDARJH:fix/gh-4306-observation-filter-leak

Conversation

@BDARJH

@BDARJH BDARJH commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #4306

Problem

When observation is enabled for record listeners, the container starts an observation per record.
For adapter-aware listeners, the container expects the delegate adapter to stop it.

With FilteringMessageListenerAdapter, filtered records do not call delegate onMessage().
In that path, the observation could remain active and leak.

Changes

  • Added ObservationRegistry support to FilteringMessageListenerAdapter.
  • Explicitly stop the current observation when a record is filtered out.
  • Updated KafkaMessageListenerContainer observation setup to propagate ObservationRegistry
    through delegating filtering listener chains.

Tests

  • Added filtered-record observation test:
    verifyFilteredRecordStopsObservation
  • Added retry + filter + observation combination test:
    verifyFilteredRetryableRecordStopsObservation
  • Verified MicrometerMetricsTests passes.

Notes

The fix is intentionally scoped to the filtered-record short-circuit path to avoid changing
the existing async/reply observation lifecycle behavior.

@BDARJH BDARJH force-pushed the fix/gh-4306-observation-filter-leak branch from 2c99531 to 74bc1cb Compare March 5, 2026 06:23

@artembilan artembilan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is very elegant solution and I see how you have thought it through.
Please, add your name to the @author list of all the affected classes.

Thank you!

/**
* Set the {@link ObservationRegistry} to stop observations for filtered records.
* @param observationRegistry the observation registry.
* @since 4.0.3

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, refine the version for real upcoming release - 4.0.4: https://github.com/spring-projects/spring-kafka/milestone/289

@BDARJH BDARJH Mar 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I’ve added my name to the author list in all affected classes and updated the since tag to 4.0.4.
Thanks for the review. 😄 👍

@BDARJH BDARJH force-pushed the fix/gh-4306-observation-filter-leak branch from 74bc1cb to dbbee11 Compare March 6, 2026 03:59
@artembilan artembilan merged commit be32638 into spring-projects:main Mar 6, 2026
3 checks passed
@artembilan

Copy link
Copy Markdown
Member

@BDARJH ,

thank you for the contribution; looking forward for more!

One note: consider to do a good commit messages instead of posting everything to the PR description.
When we open PR, the commit message becomes exactly that PR description automatically.
Then on merge, I would not need to copy/paste your text to the final commit message.

And another note: no need in squashing commits on contribution.
It is much easier to review and follow progress.
We do squash them in the end on merge.

Just couple remarks how to make your contribution process much easier.

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.

Observations for filtered messages are leaked, filling memory

2 participants