fix(kafka-python): only record known destination partition in producer spans#4858
Open
bourbonkk wants to merge 2 commits into
Open
fix(kafka-python): only record known destination partition in producer spans#4858bourbonkk wants to merge 2 commits into
bourbonkk wants to merge 2 commits into
Conversation
|
|
…r spans When neither a key nor an explicit partition is provided, the DefaultPartitioner selects a partition at random. The instrumentation called `_partition()` to record it in the span, but the producer's `send()` calls `_partition()` again independently and may pick a different partition, so `messaging.kafka.partition` frequently did not match where the message was actually delivered. Record the partition only when it can be determined without changing where the message lands (explicit partition, or key-based hash which is deterministic) and omit the attribute for the random keyless case. Fixes open-telemetry#4625 Assisted-by: Claude Opus 4.8 Signed-off-by: allen <allen.k1m@kakaocorp.com>
bourbonkk
force-pushed
the
fix/kafka-python-partition-4625
branch
from
July 23, 2026 06:28
10cc6d8 to
8823299
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #4625
When neither a
keynor an explicitpartitionis provided, theDefaultPartitionerselects a partition at random.extract_send_partitioncalledinstance._partition()to record it, but the producer'ssend()calls_partition()again independently and picks a different random partition, somessaging.kafka.partitionfrequently did not match where the message actually landed.This records the partition only when it can be determined without changing where the message lands:
partitionargument → used as-is (including0)keyprovided → deterministic key-hash partition, matchessend()_enrich_spanno longer sets the attribute toNone)Two earlier PRs (#4627, #4650) addressed this but went stale and were auto-closed; this continues that work with tests and a changelog entry, and keeps the correct partition for keyed messages.
Type of change
How Has This Been Tested?
tests/test_utils.pycovering explicit partition (incl.0), key-based (deterministic), keyless-random (omitted), error handling, and_enrich_spanrecording/omitting the attribute.py312-test-instrumentation-kafka-pythontest suite passes locally.Does This PR Require a Core Repo Change?
Checklist: