Skip to content

Commit

Permalink
fix(kafka): cast topic to str since it can return None (#10691)
Browse files Browse the repository at this point in the history
Since topic can be None for a message object we should always cast it to
a str or else it'll throw an error if we try to set it as a tag with
set_tag_str


https://docs.confluent.io/platform/current/clients/confluent-kafka-python/html/index.html#confluent_kafka.Message.topic

## Checklist
- [ ] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [x] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
ZStriker19 authored Sep 20, 2024
1 parent baac738 commit 33daba9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ddtrace/contrib/internal/kafka/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def _instrument_message(messages, pin, start_ns, instance, err):

for message in messages:
if message is not None and first_message is not None:
core.set_item("kafka_topic", first_message.topic())
core.set_item("kafka_topic", str(first_message.topic()))
core.dispatch("kafka.consume.start", (instance, first_message, span))

span.set_tag_str(MESSAGING_SYSTEM, kafkax.SERVICE)
Expand All @@ -260,7 +260,7 @@ def _instrument_message(messages, pin, start_ns, instance, err):
if first_message is not None:
message_key = first_message.key() or ""
message_offset = first_message.offset() or -1
span.set_tag_str(kafkax.TOPIC, first_message.topic())
span.set_tag_str(kafkax.TOPIC, str(first_message.topic()))

# If this is a deserializing consumer, do not set the key as a tag since we
# do not have the serialization function
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
kafka: Fixed an issue where a ``TypeError`` exception would be raised if the first message's ``topic()`` returned ``None`` during consumption.

0 comments on commit 33daba9

Please sign in to comment.