opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

Fix aiokafka multiple values headers error.

Open fcfangcc opened this issue 8 months ago • 1 comments
trafficstars

Description

When using the send_and_wait method, a TypeError occurs:
TypeError: original_send() got multiple values for argument 'headers'

This happens because of the following lines in the OpenTelemetry AIoKafka instrumentation code:
[opentelemetry-instrumentation-aiokafka/utils.py#L263-L266](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/81eaea57f919055af56ca94e9bc523e234ca21a7/instrumentation/opentelemetry-instrumentation-aiokafka/src/opentelemetry/instrumentation/aiokafka/utils.py#L263-L266).

If headers is present in args[5], these lines modify kwargs, causing headers to appear in both args and kwargs, leading to the error.

Example code triggering the issue(run with otel):

import asyncio

from aiokafka import AIOKafkaProducer


async def test():
    producer = AIOKafkaProducer(
        bootstrap_servers='bootstrap_servers',
    )
    await producer.start()
    await producer.send_and_wait('topic', value=b'test', headers=[])  # that's ok
    await producer.send_and_wait('topic', value=b'test')  # error here


asyncio.run(test())

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • [x] 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?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [ ] test_wrap_send_with_headers_as_args

Does This PR Require a Core Repo Change?

  • [ ] Yes. - Link to PR:
  • [x] No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

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

fcfangcc avatar Mar 05 '25 08:03 fcfangcc