Skip to content

Commit

Permalink
Refer to _incubating values to fix attribute names on aio-pika
Browse files Browse the repository at this point in the history
  • Loading branch information
P6rguVyrst committed Jul 13, 2024
1 parent 23e66eb commit ff997cd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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"):
Expand All @@ -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,
}
)

Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit ff997cd

Please sign in to comment.