|
| 1 | +import asyncio |
| 2 | +from typing import Any, Generator |
| 3 | + |
| 4 | +import aioamqp |
| 5 | +import pytest |
| 6 | + |
| 7 | +from instana.singletons import tracer |
| 8 | +from tests.helpers import testenv |
| 9 | +from aioamqp.properties import Properties |
| 10 | +from aioamqp.envelope import Envelope |
| 11 | + |
| 12 | +testenv["rabbitmq_host"] = "127.0.0.1" |
| 13 | +testenv["rabbitmq_port"] = 5672 |
| 14 | + |
| 15 | + |
| 16 | +class TestAioamqp: |
| 17 | + @pytest.fixture(autouse=True) |
| 18 | + def _resource(self) -> Generator[None, None, None]: |
| 19 | + self.recorder = tracer.span_processor |
| 20 | + self.recorder.clear_spans() |
| 21 | + |
| 22 | + self.loop = asyncio.new_event_loop() |
| 23 | + asyncio.set_event_loop(None) |
| 24 | + yield |
| 25 | + self.loop.run_until_complete(self.delete_queue()) |
| 26 | + if self.loop.is_running(): |
| 27 | + self.loop.close() |
| 28 | + |
| 29 | + async def delete_queue(self) -> None: |
| 30 | + transport, protocol = await aioamqp.connect( |
| 31 | + testenv["rabbitmq_host"], |
| 32 | + testenv["rabbitmq_port"], |
| 33 | + ) |
| 34 | + channel = await protocol.channel() |
| 35 | + await channel.queue_delete("message_queue") |
| 36 | + await asyncio.sleep(1) |
| 37 | + |
| 38 | + async def publish_message(self) -> None: |
| 39 | + transport, protocol = await aioamqp.connect( |
| 40 | + testenv["rabbitmq_host"], |
| 41 | + testenv["rabbitmq_port"], |
| 42 | + ) |
| 43 | + channel = await protocol.channel() |
| 44 | + |
| 45 | + await channel.queue_declare(queue_name="message_queue") |
| 46 | + |
| 47 | + message = "Instana test message" |
| 48 | + await channel.basic_publish( |
| 49 | + message.encode(), exchange_name="", routing_key="message_queue" |
| 50 | + ) |
| 51 | + |
| 52 | + await protocol.close() |
| 53 | + transport.close() |
| 54 | + |
| 55 | + async def consume_message(self) -> None: |
| 56 | + async def callback( |
| 57 | + channel: Any, |
| 58 | + body: bytes, |
| 59 | + envelope: Envelope, |
| 60 | + properties: Properties, |
| 61 | + ) -> None: |
| 62 | + with tracer.start_as_current_span("callback-span"): |
| 63 | + await channel.basic_client_ack(delivery_tag=envelope.delivery_tag) |
| 64 | + |
| 65 | + _, protocol = await aioamqp.connect( |
| 66 | + testenv["rabbitmq_host"], testenv["rabbitmq_port"] |
| 67 | + ) |
| 68 | + channel = await protocol.channel() |
| 69 | + await channel.queue_declare(queue_name="message_queue") |
| 70 | + await channel.basic_consume(callback, queue_name="message_queue", no_ack=False) |
| 71 | + |
| 72 | + def test_basic_publish(self) -> None: |
| 73 | + with tracer.start_as_current_span("test-span"): |
| 74 | + self.loop.run_until_complete(self.publish_message()) |
| 75 | + |
| 76 | + spans = self.recorder.queued_spans() |
| 77 | + |
| 78 | + assert len(spans) == 2 |
| 79 | + publisher_span = spans[0] |
| 80 | + test_span = spans[1] |
| 81 | + |
| 82 | + assert publisher_span.n == "sdk" |
| 83 | + assert publisher_span.data["sdk"]["name"] == "aioamqp-publisher" |
| 84 | + assert publisher_span.p == test_span.s |
| 85 | + |
| 86 | + assert test_span.n == "sdk" |
| 87 | + assert not test_span.p |
| 88 | + |
| 89 | + def test_basic_consumer(self) -> None: |
| 90 | + with tracer.start_as_current_span("test-span"): |
| 91 | + self.loop.run_until_complete(self.publish_message()) |
| 92 | + self.loop.run_until_complete(self.consume_message()) |
| 93 | + |
| 94 | + spans = self.recorder.queued_spans() |
| 95 | + |
| 96 | + assert len(spans) == 4 |
| 97 | + |
| 98 | + publisher_span = spans[0] |
| 99 | + callback_span = spans[1] |
| 100 | + consumer_span = spans[2] |
| 101 | + test_span = spans[3] |
| 102 | + |
| 103 | + assert publisher_span.n == "sdk" |
| 104 | + assert publisher_span.data["sdk"]["name"] == "aioamqp-publisher" |
| 105 | + assert publisher_span.p == test_span.s |
| 106 | + assert ( |
| 107 | + publisher_span.data["sdk"]["custom"]["tags"]["aioamqp.exchange"] |
| 108 | + == "b'Instana test message'" |
| 109 | + ) |
| 110 | + |
| 111 | + assert callback_span.n == "sdk" |
| 112 | + assert callback_span.data["sdk"]["name"] == "callback-span" |
| 113 | + assert callback_span.data["sdk"]["type"] == "intermediate" |
| 114 | + assert callback_span.p == consumer_span.s |
| 115 | + |
| 116 | + assert consumer_span.n == "sdk" |
| 117 | + assert consumer_span.data["sdk"]["name"] == "aioamqp-consumer" |
| 118 | + assert consumer_span.data["sdk"]["custom"]["tags"]["aioamqp.callback"] |
| 119 | + assert ( |
| 120 | + consumer_span.data["sdk"]["custom"]["tags"]["aioamqp.message"] |
| 121 | + == "b'Instana test message'" |
| 122 | + ) |
| 123 | + assert ( |
| 124 | + consumer_span.data["sdk"]["custom"]["tags"]["aioamqp.routing_key"] |
| 125 | + == "message_queue" |
| 126 | + ) |
| 127 | + assert not consumer_span.data["sdk"]["custom"]["tags"]["exchange_name"] |
| 128 | + assert consumer_span.p == test_span.s |
| 129 | + |
| 130 | + assert test_span.n == "sdk" |
| 131 | + assert test_span.data["sdk"]["name"] == "test-span" |
0 commit comments