From ff997cd9d9d4d1ec833fecede90a62672c3aaa8f Mon Sep 17 00:00:00 2001 From: Toomas Ormisson Date: Sat, 13 Jul 2024 14:24:30 +0200 Subject: [PATCH] Refer to _incubating values to fix attribute names on aio-pika --- .../pyproject.toml | 2 +- .../instrumentation/aio_pika/span_builder.py | 18 +++--- .../tests/test_required_attributes.py | 56 +++++++++++++++++++ 3 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 instrumentation/opentelemetry-instrumentation-aio-pika/tests/test_required_attributes.py diff --git a/instrumentation/opentelemetry-instrumentation-aio-pika/pyproject.toml b/instrumentation/opentelemetry-instrumentation-aio-pika/pyproject.toml index 0a3f972068..d047fbc9e1 100644 --- a/instrumentation/opentelemetry-instrumentation-aio-pika/pyproject.toml +++ b/instrumentation/opentelemetry-instrumentation-aio-pika/pyproject.toml @@ -26,7 +26,7 @@ classifiers = [ ] dependencies = [ "opentelemetry-api ~= 1.25", - "opentelemetry-instrumentation == 0.46b0", + "opentelemetry-instrumentation == 0.47b0.dev", "wrapt >= 1.0.0, < 2.0.0", ] diff --git a/instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/span_builder.py b/instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/span_builder.py index c62b1ea9bf..2c7d0eae55 100644 --- a/instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/span_builder.py +++ b/instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/span_builder.py @@ -16,10 +16,14 @@ from aio_pika.abc import AbstractChannel, AbstractMessage from opentelemetry.instrumentation.utils import is_instrumentation_enabled + from opentelemetry.semconv.trace import ( MessagingOperationValues, - SpanAttributes, ) +# opentelemetry-semantic-conventions/src/opentelemetry/semconv/_incubating/attributes/messaging_attributes.py +from opentelemetry.semconv._incubating.attributes import messaging_attributes as SpanAttributes +from opentelemetry.semconv._incubating.attributes import net_attributes as NetAttributes + from opentelemetry.trace import Span, SpanKind, Tracer _DEFAULT_ATTRIBUTES = {SpanAttributes.MESSAGING_SYSTEM: "rabbitmq"} @@ -44,7 +48,7 @@ def set_operation(self, operation: MessagingOperationValues): def set_destination(self, destination: str): self._destination = destination - self._attributes[SpanAttributes.MESSAGING_DESTINATION] = destination + self._attributes[SpanAttributes.MESSAGING_DESTINATION_NAME] = destination def set_channel(self, channel: AbstractChannel): if hasattr(channel, "_connection"): @@ -61,8 +65,8 @@ def set_channel(self, channel: AbstractChannel): url = connection.url self._attributes.update( { - SpanAttributes.NET_PEER_NAME: url.host, - SpanAttributes.NET_PEER_PORT: url.port or 5672, + NetAttributes.NET_PEER_NAME: url.host, + NetAttributes.NET_PEER_PORT: url.port or 5672, } ) @@ -74,16 +78,16 @@ def set_message(self, message: AbstractMessage): ] = properties.message_id if properties.correlation_id: self._attributes[ - SpanAttributes.MESSAGING_CONVERSATION_ID + SpanAttributes.MESSAGING_MESSAGE_CONVERSATION_ID ] = properties.correlation_id def build(self) -> Optional[Span]: if not is_instrumentation_enabled(): return None if self._operation: - self._attributes[SpanAttributes.MESSAGING_OPERATION] = self._operation.value + self._attributes[SpanAttributes.MESSAGING_OPERATION_TYPE] = self._operation.value else: - self._attributes[SpanAttributes.MESSAGING_TEMP_DESTINATION] = True + self._attributes[SpanAttributes.MESSAGING_DESTINATION_TEMPORARY] = True span = self._tracer.start_span( self._generate_span_name(), kind=self._kind, attributes=self._attributes ) diff --git a/instrumentation/opentelemetry-instrumentation-aio-pika/tests/test_required_attributes.py b/instrumentation/opentelemetry-instrumentation-aio-pika/tests/test_required_attributes.py new file mode 100644 index 0000000000..06724dedeb --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-aio-pika/tests/test_required_attributes.py @@ -0,0 +1,56 @@ +from unittest import TestCase + +from opentelemetry.instrumentation.aio_pika.span_builder import SpanBuilder +from opentelemetry.trace import Span, get_tracer + +# https://opentelemetry.io/docs/specs/semconv/messaging/messaging-spans/#messaging-attributes +# use as example: opentelemetry-instrumentation-redis/tests/test_redis.py + + +REQUIRED_SPAN_ATTRIBUTES = { + "messaging.operation.type": { + "type": str() + }, + "messaging.system": { + "type": str() + }, +} +CONDITIONALLY_REQUIRED_SPAN_ATTRIBUTES = { + "error.type" : { + "type": str() + }, + "messaging.batch.message_count" : { + "type": int() + }, + "messaging.destination.anonymous" : { + "type": bool() + }, + "messaging.destination.name" : { + "type": str() + }, + "messaging.destination.template" : { + "type": str() + }, + "messaging.destination.temporary" : { + "type": bool() + }, + "server.address" : { + "type": str() + }, +} + + +class TestBuilder(TestCase): + def test_consumer_required_attributes(self): + builder = SpanBuilder(get_tracer(__name__)) + builder.set_as_consumer() + builder.set_destination("destination") + span = builder.build() + self.assertTrue(isinstance(span, Span)) + + def test_producer_required_attributes(self): + builder = SpanBuilder(get_tracer(__name__)) + builder.set_as_producer() + builder.set_destination("destination") + span = builder.build() + self.assertTrue(isinstance(span, Span))