Skip to content

Add consumer group sharding to kafka_consumer - #25886

Open
dsavov wants to merge 1 commit into
DataDog:masterfrom
dsavov:dsavov/kafka-consumer-shard-groups
Open

dsavov wants to merge 1 commit into
DataDog:masterfrom
dsavov:dsavov/kafka-consumer-shard-groups

Conversation

@dsavov

@dsavov dsavov commented Oct 31, 2026 •

Copy link
Copy Markdown

What does this PR do?

Adds two kafka_consumer instance options, consumer_groups_shard_count and consumer_groups_shard_index, to split the consumer groups of one Kafka cluster across several instances.

Each group belongs to shard zlib.crc32(group_id) % consumer_groups_shard_count, so every instance agrees on the assignment across processes and restarts. _get_consumer_groups drops the groups outside the instance's shard before any offsets are fetched, with monitor_unlisted_consumer_groups, consumer_groups_regex and consumer_groups alike. Highwater offsets follow, as they are only fetched for the partitions the shard's groups consume.

consumer_groups_shard_index is required when the count is above 1: defaulting it to 0 would silently collect only part of the groups. Out-of-range values raise ConfigurationError, and so does sharding together with enable_kafka_console, whose cluster-wide metadata collection would otherwise run on every shard. Without the options nothing changes.

Motivation

With monitor_unlisted_consumer_groups, one check run collects every group of the cluster, and that run can only go to one cluster check runner. On a cluster with about 17k groups (about 1.08M consumer contexts), a single run took about 30s at 1.8 cores and 1.6Gi, and still stopped at the 2M context limit. On a cluster with about 42k groups, runners were OOM-killed at 16Gi.

Running this branch against the 17k-group cluster as 3 instances on 3 runners, each instance covered between 5,731 and 5,831 groups with no overlap, ran in 6 to 9s and peaked at about 0.7Gi.

Two Cluster Agent changes build on this: DataDog/datadog-agent#59382 generates the shard instances from a single config, and DataDog/datadog-agent#59381 keeps them on different runners.

QA: kafka_consumer is only owned by @DataDog/agent-integrations, so this should be qa/skip-qa. I can't set labels from a fork.

Checklist before requesting review

  • I self-reviewed the full diff, and this description matches the final change
  • Tests cover the change, or this description explains why none are needed
  • Automated review comments, including Codex review, were addressed or answered
  • I, the human author, understand this change, can explain it to reviewers, and believe it meets the repository standards

@dd-octo-sts

dd-octo-sts Bot commented Oct 31, 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 627c14f — GitHub Run · Dispatcher Logs.

@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Pipelines  Code Coverage

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 1 Pipeline job failed

Validate repository | Run Validations / Validate

View more details · View in GitHub Actions

ℹ️ Info

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 92.95% (+3.19%)

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 44ba95a | Docs | View more details | Give us feedback!

@dsavov

dsavov commented Oct 31, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

1 similar comment
@dsavov

dsavov commented Oct 31, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@dsavov
dsavov marked this pull request as ready for review October 31, 2026 08:56
@dsavov
dsavov requested review from a team as code owners October 31, 2026 08:56
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T09:37:30.080732Z 44ba95a Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-datadog-us1-prod datadog-datadog-us1-prod 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.

Bits Code Review: FAIL

Combining sharding with Kafka Console counts each consumer-group membership change once per shard because metadata collection ignores shard ownership.

Open Bits AI session

🤖 Bits Code Review · Commit f506c7f

in_shard = self.config.in_consumer_groups_shard
if self.config._monitor_unlisted_consumer_groups:
return [grp for grp in self.client.list_consumer_groups() if grp]
return [grp for grp in self.client.list_consumer_groups() if grp and in_shard(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 Apply shard ownership to Kafka Console group metadata

With enable_kafka_console enabled across N shard instances, this filter partitions offset collection, but ClusterMetadataCollector independently describes every consumer group on every instance. Each membership change therefore emits kafka.consumer_group.membership_changes once per shard instead of once overall, inflating counts and duplicating membership events. Apply shard ownership to consumer-group metadata collection as well, or reject this configuration combination.

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🤖 Bits Code Review · Open Bits AI session

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.

Agreed. Fixed in fbb9a231 by rejecting the combination: consumer_groups_shard_count greater than 1 with enable_kafka_console (or enable_cluster_monitoring) now raises a ConfigurationError, with a test for both option names.

@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: f506c7f438

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

in_shard = self.config.in_consumer_groups_shard
if self.config._monitor_unlisted_consumer_groups:
return [grp for grp in self.client.list_consumer_groups() if grp]
return [grp for grp in self.client.list_consumer_groups() if grp and in_shard(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.

P1 Badge Shard Kafka Console group metadata too

When enable_kafka_console is enabled (which sets _monitor_unlisted_consumer_groups), this filters only the offset-discovery path. The same run invokes ClusterMetadataCollector.collect_all_metadata, and _collect_consumer_group_metadata in cluster_metadata.py:782-858 still lists, describes, and emits metrics/events for every group on every shard. Consequently an N-shard deployment retains the full metadata workload on each runner and can emit consumer_group.membership_changes once per shard with identical tags for a single transition, overcounting that metric; apply the shard predicate to this collector while keeping any truly cluster-wide summary on one shard.

Useful? React with 👍 / 👎.

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.

Good catch, thanks. Fixed in fbb9a231 by rejecting the combination: an instance with consumer_groups_shard_count greater than 1 and enable_kafka_console (or enable_cluster_monitoring) now raises a ConfigurationError, and the option docs say so. Sharding the Kafka Console metadata, with the cluster-wide part on a single shard, would be a larger change to ClusterMetadataCollector; I would rather keep it out of this PR.

@dsavov
dsavov force-pushed the dsavov/kafka-consumer-shard-groups branch from f506c7f to fbb9a23 Compare October 31, 2026 09:09
@dsavov

dsavov commented Oct 31, 2026

Copy link
Copy Markdown
Author

Sharding is currently rejected together with enable_kafka_console, since console collects cluster-wide metadata (brokers, topics, schemas, all groups, heartbeat) on every run. If you'd like console support, I can follow up by sharding the per-group metadata and collecting the cluster-wide parts on shard 0 only, but I'd need guidance on whether the console backend accepts group metadata and membership events from several instances of one cluster, and which instance should own the heartbeat. Happy to do it here or in a separate PR, whichever you prefer.

@dsavov

dsavov commented Oct 31, 2026

Copy link
Copy Markdown
Author

@codex review

@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: fbb9a23158

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".



def sharded_instance(kafka_instance, shard_index, shard_count=3, **options):
kafka_instance.pop("consumer_groups", None)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add type hints to the new test helper

The newly added sharded_instance helper leaves its arguments and return value unannotated even though it is a regular helper function, so add annotations for the instance mapping, shard values, options, and returned mapping to comply with the root repository requirement that new functions and methods carry type hints.

AGENTS.md reference: AGENTS.md:L33-L35

Useful? React with 👍 / 👎.

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.

Fixed in 44ba95a: sharded_instance now annotates its arguments and return type.

Add consumer_groups_shard_count and consumer_groups_shard_index so one
cluster's consumer groups can be split across several instances, and
therefore several cluster check runners. Groups are assigned by a stable
crc32 hash of the group ID and dropped before their offsets are fetched.
@dsavov
dsavov force-pushed the dsavov/kafka-consumer-shard-groups branch from fbb9a23 to 44ba95a Compare October 31, 2026 09:32
@dsavov

dsavov commented Oct 31, 2026

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 44ba95abc4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

This branch has not been deployed

No deployments
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.

1 participant