Skip to content

Conversation

@pvital
Copy link
Member

@pvital pvital commented Oct 28, 2025

This PR fixes a Tuple IndexError in both confluent_kafka_python.py and kafka_python.py implementations.

It handles both positional and keyword arguments for the topic parameter in the respective trace_kafka_produce and trace_kafka_send functions. The issue occurred when the topic was passed as a keyword argument, resulting in an empty args tuple and causing an IndexError when trying to access args[0].

  1. confluent_kafka_python.py:

    • Fixed the trace_kafka_produce function to handle both positional and keyword arguments for the topic parameter
    • Added safety checks to handle edge cases
    • Added tests to verify the fix works with different argument patterns
    • Committed the changes with a detailed commit message
  2. kafka_python.py:

    • Identified the same issue in the trace_kafka_send function
    • Implemented a similar fix to handle both positional and keyword arguments
    • Added tests to verify the fix works with different argument patterns
    • Committed the changes with a detailed commit message

Both fixes ensure that the Kafka instrumentation works correctly regardless of how the methods are called, improving the robustness of the Python sensor. The changes are minimal and focused on the specific issue, maintaining compatibility with existing code.

These changes should prevent the IndexError from occurring in the future, even when users pass the topic as a keyword argument instead of a positional argument.

@github-actions
Copy link

@pvital the signed-off-by was not found in the following 2 commits:

  • cadb262: fix: IndexError in the confluent_kafka_python.py
  • 3037546: fix: IndexError in the kafka_python.py

📝 What should I do to fix it?

All proposed commits should include a sign-off in their messages, ideally at the end.

❔ Why it is required

The Developer Certificate of Origin (DCO) is a lightweight way for contributors to certify that they wrote or otherwise have the right to submit the code they are contributing to the project. Here is the full text of the DCO, reformatted for readability:

By making a contribution to this project, I certify that:

a. The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or

b. The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or

c. The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

d. I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.

Contributors sign-off that they adhere to these requirements by adding a Signed-off-by line to commit messages.

This is my commit message

Signed-off-by: Random Developer <randomdeveloper@example.com>

Git even has a -s command line option to append this automatically to your commit message:

$ git commit -s -m 'This is my commit message'

@pvital pvital force-pushed the fix_confluent_kafka_index_out_of_range branch from 3037546 to 3234567 Compare October 28, 2025 17:14
@pvital pvital self-assigned this Oct 28, 2025
@pvital pvital added the bug label Oct 28, 2025
Fixed IndexError in `confluent_kafka_python.py` by handling both positional and keyword arguments for the topic parameter in the `trace_kafka_produce` function. The issue occurred when the topic was passed as a keyword argument, resulting in an empty args tuple and causing an IndexError when trying to access `args[0]`.

The solution:
1. Modified the `trace_kafka_produce` function to get the topic from either `args` or `kwargs`
2. Added safety checks to handle edge cases
3. Added two new test methods to verify the fix works with different argument patterns:
   - `test_trace_confluent_kafka_produce_with_keyword_topic`
   - `test_trace_confluent_kafka_produce_with_keyword_args`

This fix ensures that the Kafka instrumentation works correctly regardless of how the `produce` method is called, improving the robustness of the Python sensor.

Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
Fixed potential IndexError in `kafka_python.py` by handling both positional and keyword arguments for the topic parameter in the `trace_kafka_send` function. The issue is similar to the one fixed in `confluent_kafka_python.py`, where an IndexError could occur when the topic was passed as a keyword argument, resulting in an empty `args` tuple.

The solution:
1. Modified the `trace_kafka_send` function to get the topic from either `args` or `kwargs`
2. Added safety checks to handle edge cases
3. Added two new test methods to verify the fix works with different argument patterns:
   - `test_trace_kafka_python_send_with_keyword_topic`
   - `test_trace_kafka_python_send_with_keyword_args`

This fix ensures that the Kafka instrumentation works correctly regardless of how the `send` method is called, improving the robustness of the Python sensor.

Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
@pvital pvital force-pushed the fix_confluent_kafka_index_out_of_range branch from 3234567 to ee1db84 Compare October 29, 2025 09:03
@sonarqubecloud
Copy link

@pvital pvital marked this pull request as ready for review October 29, 2025 09:28
@pvital pvital requested a review from a team as a code owner October 29, 2025 09:28
Copy link
Contributor

@GSVarsha GSVarsha left a comment

Choose a reason for hiding this comment

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

LGTM

@pvital pvital merged commit b4263fc into main Oct 29, 2025
20 checks passed
@pvital pvital deleted the fix_confluent_kafka_index_out_of_range branch October 29, 2025 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants