diff --git a/ddtrace/_trace/trace_handlers.py b/ddtrace/_trace/trace_handlers.py index 7ead053455b..94c38c1911b 100644 --- a/ddtrace/_trace/trace_handlers.py +++ b/ddtrace/_trace/trace_handlers.py @@ -1354,7 +1354,8 @@ def _on_aiokafka_send_start( span._set_attribute(SPAN_KIND, SpanKind.PRODUCER) span._set_attribute(TOMBSTONE, str(send_value is None)) span.set_tag(MESSAGE_KEY, send_key.decode("utf-8") if send_key else None) - span._set_attribute(PARTITION, partition or -1) + if partition is not None: + span._set_attribute(PARTITION, partition) span._set_attribute(_SPAN_MEASURED_KEY, 1) if config.aiokafka.distributed_tracing_enabled: @@ -1396,7 +1397,6 @@ def _on_aiokafka_getone_message( if message is not None: message_key = message.key.decode("utf-8") if message.key else None - message_offset = message.offset or -1 topic = str(message.topic) span._set_attribute(TOPIC, topic) span._set_attribute(TOMBSTONE, str(message.value is None)) @@ -1404,8 +1404,10 @@ def _on_aiokafka_getone_message( if isinstance(message_key, str): span.set_tag(MESSAGE_KEY, message_key) - span._set_attribute(PARTITION, message.partition or -1) - span._set_attribute(MESSAGE_OFFSET, message_offset) + if message.partition is not None: + span._set_attribute(PARTITION, message.partition) + if message.offset is not None: + span._set_attribute(MESSAGE_OFFSET, message.offset) if err is not None: span.set_exc_info(type(err), err, err.__traceback__) diff --git a/releasenotes/notes/aiokafka-zero-partition-offset-50aa5b8c6766e3ff.yaml b/releasenotes/notes/aiokafka-zero-partition-offset-50aa5b8c6766e3ff.yaml new file mode 100644 index 00000000000..a02391d0240 --- /dev/null +++ b/releasenotes/notes/aiokafka-zero-partition-offset-50aa5b8c6766e3ff.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + aiokafka: Fixes ``kafka.partition`` and ``kafka.message_offset`` span tags + being incorrectly set to ``-1``. Consumer spans no longer misreport ``0`` + (e.g. single-partition topics or the first message in a partition) due to + falsy evaluation. Producer spans and any span where the value is genuinely + unknown now omit these tags entirely rather than emitting a misleading ``-1``. diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_commit_with_offset.json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_commit_with_offset.json index 153c8991d05..703e774bf12 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_commit_with_offset.json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_commit_with_offset.json @@ -34,8 +34,8 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.message_offset": -1.0, - "kafka.partition": -1.0, + "kafka.message_offset": 0.0, + "kafka.partition": 0.0, "process_id": 510.0 }, "duration": 5840561, diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_failure.json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_failure.json index acbaa772e29..28ba2ad0555 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_failure.json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_failure.json @@ -35,7 +35,6 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.partition": -1.0, "process_id": 510.0 }, "duration": 104618323, diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_key[None].json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_key[None].json index 510f327f402..ca71b44efd0 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_key[None].json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_key[None].json @@ -33,8 +33,8 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.message_offset": -1.0, - "kafka.partition": -1.0, + "kafka.message_offset": 0.0, + "kafka.partition": 0.0, "process_id": 510.0 }, "duration": 2732458, diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_key[test_key].json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_key[test_key].json index 20b978f4361..1cf6f041ff1 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_key[test_key].json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_key[test_key].json @@ -34,8 +34,8 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.message_offset": -1.0, - "kafka.partition": -1.0, + "kafka.message_offset": 0.0, + "kafka.partition": 0.0, "process_id": 510.0 }, "duration": 4094210, diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_value[None].json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_value[None].json index 5a097359c30..67a7a6cb51e 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_value[None].json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_value[None].json @@ -34,8 +34,8 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.message_offset": -1.0, - "kafka.partition": -1.0, + "kafka.message_offset": 0.0, + "kafka.partition": 0.0, "process_id": 510.0 }, "duration": 3528046, diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_value[hueh_hueh_hueh].json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_value[hueh_hueh_hueh].json index cf99900d7eb..a4746c72227 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_value[hueh_hueh_hueh].json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_value[hueh_hueh_hueh].json @@ -34,8 +34,8 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.message_offset": -1.0, - "kafka.partition": -1.0, + "kafka.message_offset": 0.0, + "kafka.partition": 0.0, "process_id": 510.0 }, "duration": 2561725, diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_with_distributed_tracing.json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_with_distributed_tracing.json index 6697cdd3e80..59ce602dcd9 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_with_distributed_tracing.json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_and_wait_with_distributed_tracing.json @@ -75,8 +75,8 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.message_offset": -1.0, - "kafka.partition": -1.0, + "kafka.message_offset": 0.0, + "kafka.partition": 0.0, "process_id": 510.0 }, "duration": 1800834, diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_commit.json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_commit.json index 3cda6790555..b8222dd4e58 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_commit.json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka.test_send_commit.json @@ -34,8 +34,8 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.message_offset": -1.0, - "kafka.partition": -1.0, + "kafka.message_offset": 0.0, + "kafka.partition": 0.0, "process_id": 510.0 }, "duration": 2675113, diff --git a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka_dsm.test_data_streams_aiokafka_enabled.json b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka_dsm.test_data_streams_aiokafka_enabled.json index e61e85420d2..f71dab500f1 100644 --- a/tests/snapshots/tests.contrib.aiokafka.test_aiokafka_dsm.test_data_streams_aiokafka_enabled.json +++ b/tests/snapshots/tests.contrib.aiokafka.test_aiokafka_dsm.test_data_streams_aiokafka_enabled.json @@ -74,8 +74,8 @@ "_dd.top_level": 1.0, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1.0, - "kafka.message_offset": -1.0, - "kafka.partition": -1.0, + "kafka.message_offset": 0.0, + "kafka.partition": 0.0, "process_id": 648.0 }, "duration": 499024,