Skip to content

Fix/aiokafka 0.13 compatibility#4379

Merged
xrmx merged 8 commits intoopen-telemetry:mainfrom
dorlib:fix/aiokafka-0.13-compatibility
Apr 3, 2026
Merged

Fix/aiokafka 0.13 compatibility#4379
xrmx merged 8 commits intoopen-telemetry:mainfrom
dorlib:fix/aiokafka-0.13-compatibility

Conversation

@dorlib
Copy link
Copy Markdown
Contributor

@dorlib dorlib commented Mar 31, 2026

Description

_extract_send_partition() in the aiokafka instrumentation calls instance._serialize(topic, key, value) to serialize the key and value before partition assignment. In aiokafka 0.13, the internal _serialize method changed its signature from (self, topic, key, value) to (self, key, value, headers), causing the arguments to shift:

  • topic (a string) is passed where key is expected
  • key (often None) is passed where value is expected
  • value (the actual payload) is passed where headers is expected

This causes custom serializers to receive wrong arguments. For example, a value serializer expecting a Pydantic model receives None instead, producing:

AttributeError: 'NoneType' object has no attribute 'model_dump'

The fix replaces the _serialize() call with direct calls to _key_serializer and _value_serializer, which are stable across aiokafka versions:

# Before (breaks on aiokafka 0.13):
key_bytes, value_bytes = cast(
    "tuple[bytes | None, bytes | None]",
    instance._serialize(topic, key, value),
)

# After (works on both 0.12 and 0.13):
key_bytes = instance._key_serializer(key) if instance._key_serializer else key
value_bytes = instance._value_serializer(value) if instance._value_serializer else value

api_version parameter has been removed from producer client in aiokafka 0.13
link

Fixes #4378

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Existing unit tests updated (test_kafka_properties_extractor now mocks_key_serializer/_value_serializer instead of the removed _serialize call)
  • New regression test added (test_extract_send_partition_with_custom_serializers) that verifies the value serializer receives the actual value and the key serializer receives the actual key. this test fails on main and passes with the fix
  • Manually verified in a production-like environment with aiokafka 0.13.0, OTel auto-instrumentation enabled, and custom Avro/Pydantic serializers. clean logs, correct partition assignment, messages produced and consumed successfully
  • All 18 existing tests pass: tox -e py3-test-instrumentation-aiokafka

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

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

@dorlib dorlib requested a review from a team as a code owner March 31, 2026 13:29
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Mar 31, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@tammy-baylis-swi
Copy link
Copy Markdown
Contributor

Thank you for this fix!

I'm a bit concerned that regular testing didn't catch this, I guess because they run on aiokafka==0.11.0.

A bit brittle, but what do others (@open-telemetry/opentelemetry-python-contrib-approvers) think of adjusting the test coverage like the sqlalchemy instrumentor does? The tox.ini points to multiple test-requirements files, e.g. -1 and 2.

@tammy-baylis-swi tammy-baylis-swi moved this to Ready for review in Python PR digest Mar 31, 2026
Comment thread instrumentation/opentelemetry-instrumentation-aiokafka/tests/test_utils.py Outdated
@xrmx xrmx moved this from Ready for review to Reviewed PRs that need fixes in Python PR digest Apr 1, 2026
@dorlib dorlib force-pushed the fix/aiokafka-0.13-compatibility branch from 6596afd to 7f87154 Compare April 1, 2026 11:32
@dorlib dorlib force-pushed the fix/aiokafka-0.13-compatibility branch from 8cc9693 to 7f87154 Compare April 1, 2026 13:23
@dorlib dorlib force-pushed the fix/aiokafka-0.13-compatibility branch from 9faa9e0 to 65276c3 Compare April 1, 2026 13:28
@xrmx xrmx moved this from Reviewed PRs that need fixes to Approved PRs in Python PR digest Apr 2, 2026
@xrmx xrmx requested a review from tammy-baylis-swi April 2, 2026 16:01
Comment thread CHANGELOG.md
Copy link
Copy Markdown
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@tammy-baylis-swi tammy-baylis-swi left a comment

Choose a reason for hiding this comment

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

Lgtm too! Thank you

@xrmx xrmx enabled auto-merge (squash) April 3, 2026 08:23
@xrmx xrmx merged commit 58b554e into open-telemetry:main Apr 3, 2026
860 checks passed
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Python PR digest Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentation-aiokafka: Broken with aiokafka 0.13 — _serialize signature change causes argument misalignment

6 participants