-
Notifications
You must be signed in to change notification settings - Fork 344
Capture Kafka consumer group membership on join #11989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ public String instrumentedType() { | |
| public String[] helperClassNames() { | ||
| return new String[] { | ||
| packageName + ".KafkaConsumerInfo", | ||
| "datadog.trace.instrumentation.kafka_common.KafkaConfigHelper", | ||
| "datadog.trace.instrumentation.kafka_common.PendingConfig", | ||
| "datadog.trace.instrumentation.kafka_common.MetadataState", | ||
| }; | ||
|
|
@@ -55,5 +56,8 @@ public void methodAdvice(MethodTransformer transformer) { | |
| transformer.applyAdvice( | ||
| isMethod().and(named("sendOffsetCommitRequest")).and(takesArguments(1)), | ||
| packageName + ".ConsumerCoordinatorAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod().and(named("onJoinComplete")).and(takesArguments(4)), | ||
| packageName + ".JoinGroupAdvice"); | ||
|
Comment on lines
+59
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With Kafka 3.8+ consumers configured with Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package datadog.trace.instrumentation.kafka_clients38; | ||
|
|
||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import datadog.trace.instrumentation.kafka_common.KafkaConfigHelper; | ||
| import datadog.trace.instrumentation.kafka_common.MetadataState; | ||
| import net.bytebuddy.asm.Advice; | ||
| import org.apache.kafka.clients.Metadata; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.apache.kafka.clients.consumer.internals.ConsumerCoordinator; | ||
|
|
||
| public class JoinGroupAdvice { | ||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void trackJoinGroup( | ||
| @Advice.This ConsumerCoordinator coordinator, | ||
| @Advice.Argument(0) final int generationId, | ||
| @Advice.Argument(1) final String memberId, | ||
| @Advice.Argument(2) final String memberProtocol) { | ||
| if (memberId == null || memberId.isEmpty()) { | ||
| return; | ||
| } | ||
| KafkaConsumerInfo kafkaConsumerInfo = | ||
| InstrumentationContext.get(ConsumerCoordinator.class, KafkaConsumerInfo.class) | ||
| .get(coordinator); | ||
| if (kafkaConsumerInfo == null) { | ||
| return; | ||
| } | ||
| // Only report when the membership changes (new member id or new generation) to avoid | ||
| // re-reporting an unchanged membership. | ||
| if (memberId.equals(kafkaConsumerInfo.getLastReportedMemberId().orElse(null)) | ||
| && generationId == kafkaConsumerInfo.getLastReportedGenerationId()) { | ||
| return; | ||
| } | ||
| kafkaConsumerInfo.setLastReportedMembership(memberId, generationId); | ||
|
|
||
| String consumerGroup = kafkaConsumerInfo.getConsumerGroup().orElse(null); | ||
| Metadata consumerMetadata = kafkaConsumerInfo.getmetadata().orElse(null); | ||
| String clusterId = null; | ||
| if (consumerMetadata != null) { | ||
| MetadataState metadataState = | ||
| InstrumentationContext.get(Metadata.class, MetadataState.class).get(consumerMetadata); | ||
| clusterId = metadataState != null ? metadataState.clusterId : null; | ||
| } | ||
| KafkaConfigHelper.reportConsumerGroupMember( | ||
| clusterId, consumerGroup, memberId, generationId, memberProtocol); | ||
| } | ||
|
|
||
| public static void muzzleCheck(ConsumerRecord record) { | ||
| // Match ConsumerCoordinatorAdvice: only apply for kafka versions with headers | ||
| record.headers(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,9 @@ public class MsgPackDatastreamsPayloadWriter implements DatastreamsPayloadWriter | |
| private static final byte[] CONFIG_TYPE = "Type".getBytes(ISO_8859_1); | ||
| private static final byte[] CONFIG_KAFKA_CLUSTER_ID = "KafkaClusterId".getBytes(ISO_8859_1); | ||
| private static final byte[] CONFIG_CONSUMER_GROUP = "ConsumerGroup".getBytes(ISO_8859_1); | ||
| private static final byte[] CONFIG_MEMBER_ID = "MemberId".getBytes(ISO_8859_1); | ||
| private static final byte[] CONFIG_GENERATION_ID = "GenerationId".getBytes(ISO_8859_1); | ||
| private static final byte[] CONFIG_MEMBER_PROTOCOL = "MemberProtocol".getBytes(ISO_8859_1); | ||
| private static final byte[] CONFIG_ENTRIES = "Config".getBytes(ISO_8859_1); | ||
|
|
||
| private static final int INITIAL_CAPACITY = 512 * 1024; | ||
|
|
@@ -290,7 +293,8 @@ private void writeKafkaConfigs(List<KafkaConfigReport> configs, Writable packer) | |
| packer.writeUTF8(CONFIGS); | ||
| packer.startArray(configs.size()); | ||
| for (KafkaConfigReport config : configs) { | ||
| packer.startMap(4); // Type, KafkaClusterId, ConsumerGroup, Config | ||
| // Type, KafkaClusterId, ConsumerGroup, MemberId, GenerationId, MemberProtocol, Config | ||
| packer.startMap(7); | ||
|
|
||
| packer.writeUTF8(CONFIG_TYPE); | ||
| packer.writeString(config.getType(), null); | ||
|
|
@@ -301,6 +305,15 @@ private void writeKafkaConfigs(List<KafkaConfigReport> configs, Writable packer) | |
| packer.writeUTF8(CONFIG_CONSUMER_GROUP); | ||
| packer.writeString(config.getConsumerGroup(), null); | ||
|
|
||
| packer.writeUTF8(CONFIG_MEMBER_ID); | ||
| packer.writeString(config.getMemberId(), null); | ||
|
|
||
| packer.writeUTF8(CONFIG_GENERATION_ID); | ||
| packer.writeLong(config.getGenerationId()); | ||
|
|
||
| packer.writeUTF8(CONFIG_MEMBER_PROTOCOL); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
DSM kafka configuration pipeline may fail to parse reports, leading to data loss and broken consumer group monitoring. This breaks production observability for Kafka consumers using Data Streams Monitoring. Assertion details
Was this helpful? React 👍 or 👎 |
||
| packer.writeString(config.getMemberProtocol(), null); | ||
|
|
||
| packer.writeUTF8(CONFIG_ENTRIES); | ||
| Map<String, String> entries = config.getConfig(); | ||
| packer.startMap(entries.size()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a consumer completes its first join before
MetadataState.clusterIdhas been populated, this records the(memberId, generationId)as already reported and then sends a membership report with an empty cluster id. When the later metadata update fills in the cluster id, there is no rejoin and the guard above suppresses re-emitting the same membership, unlike the existing config path which stores pending reports until cluster id is available. This leaves downstream member rows permanently missing the Kafka cluster id for that startup ordering; consider keeping the membership pending or only updatinglastReportedMembershipafter a report with a known cluster id.Useful? React with 👍 / 👎.