Skip to content

kafka_consumer: prefilter consumer groups before fetching offsets - #25232

Merged
iliakur merged 4 commits into
DataDog:masterfrom
namtran1812:namtran/kafka-consumer-prefilter-groups
Oct 8, 2026
Merged

iliakur merged 4 commits into
DataDog:masterfrom
namtran1812:namtran/kafka-consumer-prefilter-groups

Conversation

@namtran1812

@namtran1812 namtran1812 commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Filters discovered Kafka consumer groups against consumer_groups_regex before fetching their offsets.

Previously, when consumer_groups_regex was configured, the check discovered all consumer groups and passed all of them to list_consumer_group_offsets. Regex filtering happened only afterward at the consumer group/topic/partition level.

The prefilter matches the complete discovered group ID. Topic and partition filtering still runs afterward. Patterns that previously matched across the group/topic comma (for example, a,. matching group a with a suitable topic) will no longer select that group; configure a pattern matching the group ID instead.

Motivation

On clusters with many consumer groups, a narrow consumer_groups_regex could still result in offset requests for every discovered group.

A regression test verifies that orders is submitted for offset retrieval while prefix-sharing groups such as orders-v2 and orders-archive are excluded.

monitor_unlisted_consumer_groups continues to bypass the prefilter and monitor all discovered groups.

Fixes #24911

Testing

  • Added coverage verifying regex-matched groups are filtered before offset retrieval.
  • Added coverage for configurations combining explicit consumer_groups and consumer_groups_regex.
  • Added coverage ensuring monitor_unlisted_consumer_groups continues to fetch all groups.
  • Added coverage for prefix-sharing group IDs and complete-ID matching.
  • Local unit and lint runs were blocked by insufficient disk space; CI is validating this revision.
  • Python syntax compilation and git diff --check pass.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add qa/required if this PR needs QA validation, or qa/skip-qa if it does not. Exactly one of the two is required.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

@dd-octo-sts

dd-octo-sts Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

✅ Dispatcher tests: passed

Dispatcher beta: informational only
Existing CI remains the merge signal.

  6/6 jobs

✅ 6 passed · nothing failed

Batches · ✅ batch-01 6/6

Dispatcher finished on 7f2a8f2 — GitHub Run · Dispatcher Logs.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 59ac06f530

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return [
grp
for grp in self.client.list_consumer_groups()
if grp and self.config._consumer_groups_compiled_group_regex.match(grp)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Require the prefilter to match the complete group ID

re.Pattern.match() accepts prefix matches, while the existing combined expression requires the group pattern to reach the comma separating the group from its topic. For example, consumer_groups_regex: {'orders': {}} should select only orders, but this prefilter also fetches offsets for orders-v2, orders-archive, and every other prefixed group before the later tuple-level filter discards them. On clusters with many similarly named groups—especially when a literal group is paired with a topic regex—this can preserve the large offset-fetch fan-out that the change is intended to eliminate; use a whole-ID or delimiter-aware match.

Useful? React with 👍 / 👎.

@datadog-prod-us1-4

datadog-prod-us1-4 Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Code Coverage

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 92.31% (+3.21%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 7e18ebf | Docs | Give us feedback!

return re.compile("|".join(patterns))

@staticmethod
def _compile_group_regex(consumer_groups_regex, consumer_groups):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should group patterns match across the group/topic delimiter, or only within the group ID? The PR description says topic/partition filtering behavior is unchanged, but it changes for one class of pattern.

With consumer_groups_regex: {'a,.': {}}, group a, topic b,c, partition 0: the tuple filter's pattern (a,.,.+,.+) matches "a,b,c,0", so the old code emitted the offset. The prefilter drops group a — a,. does not match "a" — so no offset request is made and the metric disappears. The divergence survives the fullmatch change suggested on kafka_consumer.py:299.

If patterns should match within the group ID only, note the divergence in the PR description. Otherwise, reject patterns containing an unescaped comma at validation time.

Comment thread kafka_consumer/tests/test_unit.py Resolved
Comment thread kafka_consumer/tests/test_unit.py Resolved
Signed-off-by: namtran1812 <158846154+namtran1812@users.noreply.github.com>
Signed-off-by: namtran1812 <158846154+namtran1812@users.noreply.github.com>
Comment thread kafka_consumer/tests/test_unit.py Resolved
Comment thread kafka_consumer/changelog.d/25232.fixed Resolved
Signed-off-by: namtran1812 <158846154+namtran1812@users.noreply.github.com>
@namtran1812

Copy link
Copy Markdown
Contributor Author

Hi @iliakur I just dded a regression test asserting that pattern a,. with discovered group a submits an empty list for offset retrieval, also pdated the changelog to document complete group-ID matching and the change for patterns spanning the group/topic comma

@ArmentaRoberto
ArmentaRoberto requested a review from iliakur October 6, 2026 14:51

@iliakur iliakur left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

hurray!

@iliakur
iliakur added this pull request to the merge queue Oct 8, 2026
Merged via the queue into DataDog:master with commit d1b9db2 Oct 8, 2026
42 of 45 checks passed
@cit-pr-commenter-54b7da

Copy link
Copy Markdown

evalya-impact-summary

evalya impact analysis
Impact analysis: RUN-ALL — every test task will run
Trigger:         empty diff (default branch, scheduled run, or shallow-clone fallback)
Test tasks:      0 (all selected)
Publish tasks:   2 (always emitted)
Diff:            empty (no diff information)

Learn more about CI impact filtering

@temporal-github-worker-1

Copy link
Copy Markdown

Backport evaluation: View Slack thread

steveny91 added a commit that referenced this pull request Oct 19, 2026
…oordinator-error-recovery

Resolve the conflict in kafka_consumer/tests/test_unit.py, where both
sides appended tests to the end of the file: keep master's consumer
group prefilter tests (#25232), followed by this branch's coordinator
error test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[kafka_consumer] consumer_groups_regex is applied after fetching offsets for all groups, causing a full admin fan-out

2 participants