Skip to content

refactor: don't use deprecated SpanAttributes in confluent kafka #3492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@

from opentelemetry import context, propagate
from opentelemetry.propagators import textmap
from opentelemetry.semconv._incubating.attributes.messaging_attributes import (
MESSAGING_DESTINATION_NAME,
MESSAGING_DESTINATION_TEMPORARY,
MESSAGING_KAFKA_DESTINATION_PARTITION,
MESSAGING_MESSAGE_ID,
MESSAGING_OPERATION,
MESSAGING_SYSTEM,
MessagingOperationTypeValues,
)
from opentelemetry.semconv.trace import (
MessagingDestinationKindValues,
MessagingOperationValues,
SpanAttributes,
)
from opentelemetry.trace import Link, SpanKind
Expand Down Expand Up @@ -114,32 +122,29 @@ def _enrich_span(
topic,
partition: Optional[int] = None,
offset: Optional[int] = None,
operation: Optional[MessagingOperationValues] = None,
operation: Optional[MessagingOperationTypeValues] = None,
):
if not span.is_recording():
return

span.set_attribute(SpanAttributes.MESSAGING_SYSTEM, "kafka")
span.set_attribute(SpanAttributes.MESSAGING_DESTINATION, topic)

span.set_attribute(MESSAGING_SYSTEM, "kafka")
span.set_attribute(MESSAGING_DESTINATION_NAME, topic)
Copy link
Contributor

Choose a reason for hiding this comment

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

This task is about updating the import, not changing the semantic conventions so the attributes set should have the same value but imported from another path. If the attribute is not available we must keep using the one from SpanAttributes until we move to a newer semantic convention.

Copy link
Author

Choose a reason for hiding this comment

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

Got it! take back deleted attribute value now.

Copy link
Contributor

@xrmx xrmx May 15, 2025

Choose a reason for hiding this comment

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

Yeah but still this changes messaging.destination to messaging.destination.name which we don't want to do.

if partition is not None:
span.set_attribute(SpanAttributes.MESSAGING_KAFKA_PARTITION, partition)

span.set_attribute(MESSAGING_KAFKA_DESTINATION_PARTITION, partition)
span.set_attribute(
SpanAttributes.MESSAGING_DESTINATION_KIND,
MessagingDestinationKindValues.QUEUE.value,
)

if operation:
span.set_attribute(SpanAttributes.MESSAGING_OPERATION, operation.value)
span.set_attribute(MESSAGING_OPERATION, operation.value)
else:
span.set_attribute(SpanAttributes.MESSAGING_TEMP_DESTINATION, True)
span.set_attribute(MESSAGING_DESTINATION_TEMPORARY, True)

# https://stackoverflow.com/questions/65935155/identify-and-find-specific-message-in-kafka-topic
# A message within Kafka is uniquely defined by its topic name, topic partition and offset.
if partition is not None and offset is not None and topic:
span.set_attribute(
SpanAttributes.MESSAGING_MESSAGE_ID,
MESSAGING_MESSAGE_ID,
f"{topic}.{partition}.{offset}",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
KafkaContextGetter,
KafkaContextSetter,
)
from opentelemetry.semconv._incubating.attributes.messaging_attributes import (
MESSAGING_DESTINATION_NAME,
MESSAGING_KAFKA_DESTINATION_PARTITION,
MESSAGING_MESSAGE_ID,
MESSAGING_OPERATION,
MESSAGING_SYSTEM,
)
from opentelemetry.semconv.trace import (
MessagingDestinationKindValues,
SpanAttributes,
Expand Down Expand Up @@ -122,36 +129,36 @@ def test_poll(self) -> None:
{
"name": "topic-10 process",
"attributes": {
SpanAttributes.MESSAGING_OPERATION: "process",
SpanAttributes.MESSAGING_KAFKA_PARTITION: 0,
SpanAttributes.MESSAGING_SYSTEM: "kafka",
SpanAttributes.MESSAGING_DESTINATION: "topic-10",
MESSAGING_OPERATION: "process",
MESSAGING_KAFKA_DESTINATION_PARTITION: 0,
MESSAGING_SYSTEM: "kafka",
MESSAGING_DESTINATION_NAME: "topic-10",
SpanAttributes.MESSAGING_DESTINATION_KIND: MessagingDestinationKindValues.QUEUE.value,
SpanAttributes.MESSAGING_MESSAGE_ID: "topic-10.0.0",
MESSAGING_MESSAGE_ID: "topic-10.0.0",
},
},
{"name": "recv", "attributes": {}},
{
"name": "topic-20 process",
"attributes": {
SpanAttributes.MESSAGING_OPERATION: "process",
SpanAttributes.MESSAGING_KAFKA_PARTITION: 2,
SpanAttributes.MESSAGING_SYSTEM: "kafka",
SpanAttributes.MESSAGING_DESTINATION: "topic-20",
MESSAGING_OPERATION: "process",
MESSAGING_KAFKA_DESTINATION_PARTITION: 2,
MESSAGING_SYSTEM: "kafka",
MESSAGING_DESTINATION_NAME: "topic-20",
SpanAttributes.MESSAGING_DESTINATION_KIND: MessagingDestinationKindValues.QUEUE.value,
SpanAttributes.MESSAGING_MESSAGE_ID: "topic-20.2.4",
MESSAGING_MESSAGE_ID: "topic-20.2.4",
},
},
{"name": "recv", "attributes": {}},
{
"name": "topic-30 process",
"attributes": {
SpanAttributes.MESSAGING_OPERATION: "process",
SpanAttributes.MESSAGING_KAFKA_PARTITION: 1,
SpanAttributes.MESSAGING_SYSTEM: "kafka",
SpanAttributes.MESSAGING_DESTINATION: "topic-30",
MESSAGING_OPERATION: "process",
MESSAGING_KAFKA_DESTINATION_PARTITION: 1,
MESSAGING_SYSTEM: "kafka",
MESSAGING_DESTINATION_NAME: "topic-30",
SpanAttributes.MESSAGING_DESTINATION_KIND: MessagingDestinationKindValues.QUEUE.value,
SpanAttributes.MESSAGING_MESSAGE_ID: "topic-30.1.3",
MESSAGING_MESSAGE_ID: "topic-30.1.3",
},
},
{"name": "recv", "attributes": {}},
Expand Down Expand Up @@ -190,29 +197,29 @@ def test_consume(self) -> None:
{
"name": "topic-1 process",
"attributes": {
SpanAttributes.MESSAGING_OPERATION: "process",
SpanAttributes.MESSAGING_SYSTEM: "kafka",
SpanAttributes.MESSAGING_DESTINATION: "topic-1",
MESSAGING_OPERATION: "process",
MESSAGING_SYSTEM: "kafka",
MESSAGING_DESTINATION_NAME: "topic-1",
SpanAttributes.MESSAGING_DESTINATION_KIND: MessagingDestinationKindValues.QUEUE.value,
},
},
{"name": "recv", "attributes": {}},
{
"name": "topic-2 process",
"attributes": {
SpanAttributes.MESSAGING_OPERATION: "process",
SpanAttributes.MESSAGING_SYSTEM: "kafka",
SpanAttributes.MESSAGING_DESTINATION: "topic-2",
MESSAGING_OPERATION: "process",
MESSAGING_SYSTEM: "kafka",
MESSAGING_DESTINATION_NAME: "topic-2",
SpanAttributes.MESSAGING_DESTINATION_KIND: MessagingDestinationKindValues.QUEUE.value,
},
},
{"name": "recv", "attributes": {}},
{
"name": "topic-3 process",
"attributes": {
SpanAttributes.MESSAGING_OPERATION: "process",
SpanAttributes.MESSAGING_SYSTEM: "kafka",
SpanAttributes.MESSAGING_DESTINATION: "topic-3",
MESSAGING_OPERATION: "process",
MESSAGING_SYSTEM: "kafka",
MESSAGING_DESTINATION_NAME: "topic-3",
SpanAttributes.MESSAGING_DESTINATION_KIND: MessagingDestinationKindValues.QUEUE.value,
},
},
Expand Down Expand Up @@ -247,12 +254,12 @@ def test_close(self) -> None:
{
"name": "topic-a process",
"attributes": {
SpanAttributes.MESSAGING_OPERATION: "process",
SpanAttributes.MESSAGING_KAFKA_PARTITION: 0,
SpanAttributes.MESSAGING_SYSTEM: "kafka",
SpanAttributes.MESSAGING_DESTINATION: "topic-a",
MESSAGING_OPERATION: "process",
MESSAGING_KAFKA_DESTINATION_PARTITION: 0,
MESSAGING_SYSTEM: "kafka",
MESSAGING_DESTINATION_NAME: "topic-a",
SpanAttributes.MESSAGING_DESTINATION_KIND: MessagingDestinationKindValues.QUEUE.value,
SpanAttributes.MESSAGING_MESSAGE_ID: "topic-a.0.0",
MESSAGING_MESSAGE_ID: "topic-a.0.0",
},
},
]
Expand Down Expand Up @@ -286,7 +293,7 @@ def _compare_spans(self, spans, expected_spans):

def _assert_topic(self, span, expected_topic: str) -> None:
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
span.attributes[MESSAGING_DESTINATION_NAME],
expected_topic,
)

Expand Down