Skip to content

Commit 67661d4

Browse files
refactor: semcov opentelemetry instrumentation aio pika (#3667)
* feat: refactor semcov opentelemetry-instrumentation * fix: return shema senconv * refactor: opentelemetry-instrumentation-aio-pika * fix: revert unintended changes to _semconv.py * fix: change schema * fix: change schema * fix: spanattribute messaging id * fix: spanattribute messaging id --------- Co-authored-by: Emídio Neto <[email protected]>
1 parent 6525da4 commit 67661d4

File tree

4 files changed

+56
-30
lines changed

4 files changed

+56
-30
lines changed

instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/span_builder.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@
1616
from aio_pika.abc import AbstractChannel, AbstractMessage
1717

1818
from opentelemetry.instrumentation.utils import is_instrumentation_enabled
19+
from opentelemetry.semconv._incubating.attributes.messaging_attributes import (
20+
MESSAGING_MESSAGE_ID,
21+
MESSAGING_OPERATION,
22+
MESSAGING_SYSTEM,
23+
)
24+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
25+
NET_PEER_NAME,
26+
NET_PEER_PORT,
27+
)
1928
from opentelemetry.semconv.trace import (
2029
MessagingOperationValues,
2130
SpanAttributes,
2231
)
2332
from opentelemetry.trace import Span, SpanKind, Tracer
2433

25-
_DEFAULT_ATTRIBUTES = {SpanAttributes.MESSAGING_SYSTEM: "rabbitmq"}
34+
_DEFAULT_ATTRIBUTES = {MESSAGING_SYSTEM: "rabbitmq"}
2635

2736

2837
class SpanBuilder:
@@ -44,6 +53,8 @@ def set_operation(self, operation: MessagingOperationValues):
4453

4554
def set_destination(self, destination: str):
4655
self._destination = destination
56+
# TODO: Update this implementation once the semantic conventions for messaging stabilize
57+
# See: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/messaging.md
4758
self._attributes[SpanAttributes.MESSAGING_DESTINATION] = destination
4859

4960
def set_channel(self, channel: AbstractChannel):
@@ -61,17 +72,15 @@ def set_channel(self, channel: AbstractChannel):
6172
url = connection.url
6273
self._attributes.update(
6374
{
64-
SpanAttributes.NET_PEER_NAME: url.host,
65-
SpanAttributes.NET_PEER_PORT: url.port or 5672,
75+
NET_PEER_NAME: url.host,
76+
NET_PEER_PORT: url.port or 5672,
6677
}
6778
)
6879

6980
def set_message(self, message: AbstractMessage):
7081
properties = message.properties
7182
if properties.message_id:
72-
self._attributes[SpanAttributes.MESSAGING_MESSAGE_ID] = (
73-
properties.message_id
74-
)
83+
self._attributes[MESSAGING_MESSAGE_ID] = properties.message_id
7584
if properties.correlation_id:
7685
self._attributes[SpanAttributes.MESSAGING_CONVERSATION_ID] = (
7786
properties.correlation_id
@@ -81,10 +90,10 @@ def build(self) -> Optional[Span]:
8190
if not is_instrumentation_enabled():
8291
return None
8392
if self._operation:
84-
self._attributes[SpanAttributes.MESSAGING_OPERATION] = (
85-
self._operation.value
86-
)
93+
self._attributes[MESSAGING_OPERATION] = self._operation.value
8794
else:
95+
# TODO: Update this implementation once the semantic conventions for messaging stabilize
96+
# See: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/messaging.md
8897
self._attributes[SpanAttributes.MESSAGING_TEMP_DESTINATION] = True
8998
span = self._tracer.start_span(
9099
self._generate_span_name(),

instrumentation/opentelemetry-instrumentation-aio-pika/tests/consts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
AIOPIKA_VERSION_INFO = tuple(int(v) for v in aiopika_version.split("."))
77
MESSAGE_ID = "meesage_id"
88
CORRELATION_ID = "correlation_id"
9-
MESSAGING_SYSTEM = "rabbitmq"
9+
MESSAGING_SYSTEM_VALUE = "rabbitmq"
1010
EXCHANGE_NAME = "exchange_name"
1111
QUEUE_NAME = "queue_name"
1212
ROUTING_KEY = "routing_key"

instrumentation/opentelemetry-instrumentation-aio-pika/tests/test_callback_decorator.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
from opentelemetry.instrumentation.aio_pika.callback_decorator import (
2020
CallbackDecorator,
2121
)
22+
from opentelemetry.semconv._incubating.attributes.messaging_attributes import (
23+
MESSAGING_MESSAGE_ID,
24+
MESSAGING_OPERATION,
25+
MESSAGING_SYSTEM,
26+
)
27+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
28+
NET_PEER_NAME,
29+
NET_PEER_PORT,
30+
)
2231
from opentelemetry.semconv.trace import SpanAttributes
2332
from opentelemetry.trace import SpanKind, get_tracer
2433

@@ -30,7 +39,7 @@
3039
EXCHANGE_NAME,
3140
MESSAGE,
3241
MESSAGE_ID,
33-
MESSAGING_SYSTEM,
42+
MESSAGING_SYSTEM_VALUE,
3443
QUEUE_NAME,
3544
SERVER_HOST,
3645
SERVER_PORT,
@@ -40,13 +49,13 @@
4049
@skipIf(AIOPIKA_VERSION_INFO >= (8, 0), "Only for aio_pika 7")
4150
class TestInstrumentedQueueAioRmq7(TestCase):
4251
EXPECTED_ATTRIBUTES = {
43-
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
52+
MESSAGING_SYSTEM: MESSAGING_SYSTEM_VALUE,
4453
SpanAttributes.MESSAGING_DESTINATION: EXCHANGE_NAME,
45-
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
46-
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
47-
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
54+
NET_PEER_NAME: SERVER_HOST,
55+
NET_PEER_PORT: SERVER_PORT,
56+
MESSAGING_MESSAGE_ID: MESSAGE_ID,
4857
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
49-
SpanAttributes.MESSAGING_OPERATION: "receive",
58+
MESSAGING_OPERATION: "receive",
5059
}
5160

5261
def setUp(self):
@@ -80,13 +89,13 @@ def test_decorate_callback(self):
8089
@skipIf(AIOPIKA_VERSION_INFO <= (8, 0), "Only for aio_pika 8")
8190
class TestInstrumentedQueueAioRmq8(TestCase):
8291
EXPECTED_ATTRIBUTES = {
83-
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
92+
MESSAGING_SYSTEM: MESSAGING_SYSTEM_VALUE,
8493
SpanAttributes.MESSAGING_DESTINATION: EXCHANGE_NAME,
85-
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
86-
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
87-
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
94+
NET_PEER_NAME: SERVER_HOST,
95+
NET_PEER_PORT: SERVER_PORT,
96+
MESSAGING_MESSAGE_ID: MESSAGE_ID,
8897
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
89-
SpanAttributes.MESSAGING_OPERATION: "receive",
98+
MESSAGING_OPERATION: "receive",
9099
}
91100

92101
def setUp(self):

instrumentation/opentelemetry-instrumentation-aio-pika/tests/test_publish_decorator.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
from opentelemetry.instrumentation.aio_pika.publish_decorator import (
2222
PublishDecorator,
2323
)
24+
from opentelemetry.semconv._incubating.attributes.messaging_attributes import (
25+
MESSAGING_MESSAGE_ID,
26+
MESSAGING_SYSTEM,
27+
)
28+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
29+
NET_PEER_NAME,
30+
NET_PEER_PORT,
31+
)
2432
from opentelemetry.semconv.trace import SpanAttributes
2533
from opentelemetry.trace import SpanKind, get_tracer
2634

@@ -34,7 +42,7 @@
3442
EXCHANGE_NAME,
3543
MESSAGE,
3644
MESSAGE_ID,
37-
MESSAGING_SYSTEM,
45+
MESSAGING_SYSTEM_VALUE,
3846
ROUTING_KEY,
3947
SERVER_HOST,
4048
SERVER_PORT,
@@ -44,11 +52,11 @@
4452
@skipIf(AIOPIKA_VERSION_INFO >= (8, 0), "Only for aio_pika 7")
4553
class TestInstrumentedExchangeAioRmq7(TestCase):
4654
EXPECTED_ATTRIBUTES = {
47-
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
55+
MESSAGING_SYSTEM: MESSAGING_SYSTEM_VALUE,
4856
SpanAttributes.MESSAGING_DESTINATION: f"{EXCHANGE_NAME},{ROUTING_KEY}",
49-
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
50-
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
51-
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
57+
NET_PEER_NAME: SERVER_HOST,
58+
NET_PEER_PORT: SERVER_PORT,
59+
MESSAGING_MESSAGE_ID: MESSAGE_ID,
5260
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
5361
SpanAttributes.MESSAGING_TEMP_DESTINATION: True,
5462
}
@@ -123,11 +131,11 @@ def test_publish_works_with_not_recording_span_robust(self):
123131
@skipIf(AIOPIKA_VERSION_INFO <= (8, 0), "Only for aio_pika 8")
124132
class TestInstrumentedExchangeAioRmq8(TestCase):
125133
EXPECTED_ATTRIBUTES = {
126-
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
134+
MESSAGING_SYSTEM: MESSAGING_SYSTEM_VALUE,
127135
SpanAttributes.MESSAGING_DESTINATION: f"{EXCHANGE_NAME},{ROUTING_KEY}",
128-
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
129-
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
130-
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
136+
NET_PEER_NAME: SERVER_HOST,
137+
NET_PEER_PORT: SERVER_PORT,
138+
MESSAGING_MESSAGE_ID: MESSAGE_ID,
131139
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
132140
SpanAttributes.MESSAGING_TEMP_DESTINATION: True,
133141
}

0 commit comments

Comments
 (0)