Skip to content

Commit 9bd55b1

Browse files
Support aio_pika 8-style Connection class shape
1 parent 647badc commit 9bd55b1

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

instrumentation/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
| Instrumentation | Supported Packages | Metrics support |
33
| --------------- | ------------------ | --------------- |
4-
| [opentelemetry-instrumentation-aio-pika](./opentelemetry-instrumentation-aio-pika) | aio_pika ~= 7.2.0 | No
4+
| [opentelemetry-instrumentation-aio-pika](./opentelemetry-instrumentation-aio-pika) | aio_pika >= 7.2.0 | No
55
| [opentelemetry-instrumentation-aiohttp-client](./opentelemetry-instrumentation-aiohttp-client) | aiohttp ~= 3.0 | No
66
| [opentelemetry-instrumentation-aiopg](./opentelemetry-instrumentation-aiopg) | aiopg >= 0.13.0, < 2.0.0 | No
77
| [opentelemetry-instrumentation-asgi](./opentelemetry-instrumentation-asgi) | asgiref ~= 3.0 | No
@@ -42,4 +42,4 @@
4242
| [opentelemetry-instrumentation-tortoiseorm](./opentelemetry-instrumentation-tortoiseorm) | tortoise-orm >= 0.17.0 | No
4343
| [opentelemetry-instrumentation-urllib](./opentelemetry-instrumentation-urllib) | urllib | Yes
4444
| [opentelemetry-instrumentation-urllib3](./opentelemetry-instrumentation-urllib3) | urllib3 >= 1.0.0, < 2.0.0 | Yes
45-
| [opentelemetry-instrumentation-wsgi](./opentelemetry-instrumentation-wsgi) | wsgi | Yes
45+
| [opentelemetry-instrumentation-wsgi](./opentelemetry-instrumentation-wsgi) | wsgi | Yes

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ def set_destination(self, destination: str):
4949
self._attributes[SpanAttributes.MESSAGING_DESTINATION] = destination
5050

5151
def set_channel(self, channel: AbstractChannel):
52-
url = channel.connection.connection.url
52+
connection = channel.connection
53+
if getattr(connection, "connection"):
54+
# aio_rmq 7
55+
url = connection.connection.url
56+
else:
57+
# aio_rmq 8
58+
url = connection.url
5359
self._attributes.update({
5460
SpanAttributes.NET_PEER_NAME: url.host,
5561
SpanAttributes.NET_PEER_PORT: url.port

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
SERVER_URL = URL(
1616
f"amqp://{SERVER_USER}:{SERVER_PASS}@{SERVER_HOST}:{SERVER_PORT}/"
1717
)
18-
CONNECTION = Namespace(connection=Namespace(url=SERVER_URL))
18+
CONNECTION_7 = Namespace(connection=Namespace(url=SERVER_URL))
19+
CONNECTION_8 = Namespace(connection=Namespace(url=SERVER_URL))
1920
CHANNEL = Namespace(connection=CONNECTION, loop=None)
2021
MESSAGE = Namespace(
2122
properties=Namespace(

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

+57-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
from .consts import (
2727
CHANNEL,
28-
CONNECTION,
28+
CONNECTION_7,
29+
CONNECTION_8,
2930
CORRELATION_ID,
3031
EXCHANGE_NAME,
3132
MESSAGE,
@@ -37,7 +38,7 @@
3738
)
3839

3940

40-
class TestInstrumentedExchange(TestCase):
41+
class TestInstrumentedExchangeAioRmq7(TestCase):
4142
EXPECTED_ATTRIBUTES = {
4243
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
4344
SpanAttributes.MESSAGING_DESTINATION: f"{EXCHANGE_NAME},{ROUTING_KEY}",
@@ -54,7 +55,7 @@ def setUp(self):
5455
asyncio.set_event_loop(self.loop)
5556

5657
def test_get_publish_span(self):
57-
exchange = Exchange(CONNECTION, CHANNEL, EXCHANGE_NAME)
58+
exchange = Exchange(CONNECTION_7, CHANNEL, EXCHANGE_NAME)
5859
tracer = mock.MagicMock()
5960
PublishDecorator(tracer, exchange)._get_publish_span(
6061
MESSAGE, ROUTING_KEY
@@ -66,7 +67,59 @@ def test_get_publish_span(self):
6667
)
6768

6869
def _test_publish(self, exchange_type: Type[Exchange]):
69-
exchange = exchange_type(CONNECTION, CHANNEL, EXCHANGE_NAME)
70+
exchange = exchange_type(CONNECTION_7, CHANNEL, EXCHANGE_NAME)
71+
with mock.patch.object(
72+
PublishDecorator, "_get_publish_span"
73+
) as mock_get_publish_span:
74+
with mock.patch.object(
75+
Exchange, "publish", return_value=asyncio.sleep(0)
76+
) as mock_publish:
77+
decorated_publish = PublishDecorator(
78+
self.tracer, exchange
79+
).decorate(mock_publish)
80+
self.loop.run_until_complete(
81+
decorated_publish(MESSAGE, ROUTING_KEY)
82+
)
83+
mock_publish.assert_called_once()
84+
mock_get_publish_span.assert_called_once()
85+
86+
def test_publish(self):
87+
self._test_publish(Exchange)
88+
89+
def test_robust_publish(self):
90+
self._test_publish(RobustExchange)
91+
92+
93+
class TestInstrumentedExchangeAioRmq8(TestCase):
94+
EXPECTED_ATTRIBUTES = {
95+
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
96+
SpanAttributes.MESSAGING_DESTINATION: f"{EXCHANGE_NAME},{ROUTING_KEY}",
97+
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
98+
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
99+
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
100+
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
101+
SpanAttributes.MESSAGING_TEMP_DESTINATION: True,
102+
}
103+
104+
def setUp(self):
105+
self.tracer = get_tracer(__name__)
106+
self.loop = asyncio.new_event_loop()
107+
asyncio.set_event_loop(self.loop)
108+
109+
def test_get_publish_span(self):
110+
exchange = Exchange(CONNECTION_8, CHANNEL, EXCHANGE_NAME)
111+
tracer = mock.MagicMock()
112+
PublishDecorator(tracer, exchange)._get_publish_span(
113+
MESSAGE, ROUTING_KEY
114+
)
115+
tracer.start_span.assert_called_once_with(
116+
f"{EXCHANGE_NAME},{ROUTING_KEY} send",
117+
kind=SpanKind.PRODUCER,
118+
attributes=self.EXPECTED_ATTRIBUTES,
119+
)
120+
121+
def _test_publish(self, exchange_type: Type[Exchange]):
122+
exchange = exchange_type(CONNECTION_8, CHANNEL, EXCHANGE_NAME)
70123
with mock.patch.object(
71124
PublishDecorator, "_get_publish_span"
72125
) as mock_get_publish_span:

0 commit comments

Comments
 (0)