Skip to content

fix(kafka-python): only record known destination partition in producer spans#4858

Open
bourbonkk wants to merge 2 commits into
open-telemetry:mainfrom
bourbonkk:fix/kafka-python-partition-4625
Open

fix(kafka-python): only record known destination partition in producer spans#4858
bourbonkk wants to merge 2 commits into
open-telemetry:mainfrom
bourbonkk:fix/kafka-python-partition-4625

Conversation

@bourbonkk

Copy link
Copy Markdown
Contributor

Description

Fixes #4625

When neither a key nor an explicit partition is provided, the DefaultPartitioner selects a partition at random. extract_send_partition called instance._partition() to record it, but the producer's send() calls _partition() again independently and picks a different random partition, so messaging.kafka.partition frequently did not match where the message actually landed.

This records the partition only when it can be determined without changing where the message lands:

  • explicit partition argument → used as-is (including 0)
  • key provided → deterministic key-hash partition, matches send()
  • neither → omit the attribute instead of recording a wrong value (and _enrich_span no longer sets the attribute to None)

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

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added unit tests in tests/test_utils.py covering explicit partition (incl. 0), key-based (deterministic), keyless-random (omitted), error handling, and _enrich_span recording/omitting the attribute.
  • py312-test-instrumentation-kafka-python test suite passes locally.

Does This PR Require a Core Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@bourbonkk
bourbonkk requested a review from a team as a code owner July 23, 2026 06:25
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 23, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: bourbonkk / name: allen (8823299)

…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
bourbonkk force-pushed the fix/kafka-python-partition-4625 branch from 10cc6d8 to 8823299 Compare July 23, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Incorrect partition in opentelemetry-instrumentation-kafka-python

1 participant