|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import unittest |
| 5 | + |
| 6 | +import aiohttp |
| 7 | + |
| 8 | +from instana.singletons import async_tracer |
| 9 | +from instana.configurator import config |
| 10 | + |
| 11 | +from .helpers import testenv |
| 12 | + |
| 13 | + |
| 14 | +class TestAsyncio(unittest.TestCase): |
| 15 | + def setUp(self): |
| 16 | + """ Clear all spans before a test run """ |
| 17 | + self.recorder = async_tracer.recorder |
| 18 | + self.recorder.clear_spans() |
| 19 | + |
| 20 | + # New event loop for every test |
| 21 | + self.loop = asyncio.new_event_loop() |
| 22 | + asyncio.set_event_loop(None) |
| 23 | + |
| 24 | + # Restore default |
| 25 | + config['asyncio_task_context_propagation']['enabled'] = False |
| 26 | + |
| 27 | + def tearDown(self): |
| 28 | + """ Purge the queue """ |
| 29 | + pass |
| 30 | + |
| 31 | + async def fetch(self, session, url, headers=None): |
| 32 | + try: |
| 33 | + async with session.get(url, headers=headers) as response: |
| 34 | + return response |
| 35 | + except aiohttp.web_exceptions.HTTPException: |
| 36 | + pass |
| 37 | + |
| 38 | + def test_ensure_future_with_context(self): |
| 39 | + async def run_later(msg="Hello"): |
| 40 | + # print("run_later: %s" % async_tracer.active_span.operation_name) |
| 41 | + async with aiohttp.ClientSession() as session: |
| 42 | + return await self.fetch(session, testenv["wsgi_server"] + "/") |
| 43 | + |
| 44 | + async def test(): |
| 45 | + with async_tracer.start_active_span('test'): |
| 46 | + asyncio.ensure_future(run_later("Hello")) |
| 47 | + await asyncio.sleep(0.5) |
| 48 | + |
| 49 | + # Override default task context propagation |
| 50 | + config['asyncio_task_context_propagation']['enabled'] = True |
| 51 | + |
| 52 | + self.loop.run_until_complete(test()) |
| 53 | + |
| 54 | + spans = self.recorder.queued_spans() |
| 55 | + self.assertEqual(3, len(spans)) |
| 56 | + |
| 57 | + test_span = spans[0] |
| 58 | + wsgi_span = spans[1] |
| 59 | + aioclient_span = spans[2] |
| 60 | + |
| 61 | + self.assertEqual(test_span.t, wsgi_span.t) |
| 62 | + self.assertEqual(test_span.t, aioclient_span.t) |
| 63 | + |
| 64 | + self.assertEqual(test_span.p, None) |
| 65 | + self.assertEqual(wsgi_span.p, aioclient_span.s) |
| 66 | + self.assertEqual(aioclient_span.p, test_span.s) |
| 67 | + |
| 68 | + def test_ensure_future_without_context(self): |
| 69 | + async def run_later(msg="Hello"): |
| 70 | + # print("run_later: %s" % async_tracer.active_span.operation_name) |
| 71 | + async with aiohttp.ClientSession() as session: |
| 72 | + return await self.fetch(session, testenv["wsgi_server"] + "/") |
| 73 | + |
| 74 | + async def test(): |
| 75 | + with async_tracer.start_active_span('test'): |
| 76 | + asyncio.ensure_future(run_later("Hello")) |
| 77 | + await asyncio.sleep(0.5) |
| 78 | + |
| 79 | + self.loop.run_until_complete(test()) |
| 80 | + |
| 81 | + spans = self.recorder.queued_spans() |
| 82 | + |
| 83 | + self.assertEqual(2, len(spans)) |
| 84 | + self.assertEqual("sdk", spans[0].n) |
| 85 | + self.assertEqual("wsgi", spans[1].n) |
| 86 | + |
| 87 | + # Without the context propagated, we should get two separate traces |
| 88 | + self.assertNotEqual(spans[0].t, spans[1].t) |
| 89 | + |
| 90 | + if hasattr(asyncio, "create_task"): |
| 91 | + def test_create_task_with_context(self): |
| 92 | + async def run_later(msg="Hello"): |
| 93 | + # print("run_later: %s" % async_tracer.active_span.operation_name) |
| 94 | + async with aiohttp.ClientSession() as session: |
| 95 | + return await self.fetch(session, testenv["wsgi_server"] + "/") |
| 96 | + |
| 97 | + async def test(): |
| 98 | + with async_tracer.start_active_span('test'): |
| 99 | + asyncio.create_task(run_later("Hello")) |
| 100 | + await asyncio.sleep(0.5) |
| 101 | + |
| 102 | + # Override default task context propagation |
| 103 | + config['asyncio_task_context_propagation']['enabled'] = True |
| 104 | + |
| 105 | + self.loop.run_until_complete(test()) |
| 106 | + |
| 107 | + spans = self.recorder.queued_spans() |
| 108 | + self.assertEqual(3, len(spans)) |
| 109 | + |
| 110 | + test_span = spans[0] |
| 111 | + wsgi_span = spans[1] |
| 112 | + aioclient_span = spans[2] |
| 113 | + |
| 114 | + self.assertEqual(test_span.t, wsgi_span.t) |
| 115 | + self.assertEqual(test_span.t, aioclient_span.t) |
| 116 | + |
| 117 | + self.assertEqual(test_span.p, None) |
| 118 | + self.assertEqual(wsgi_span.p, aioclient_span.s) |
| 119 | + self.assertEqual(aioclient_span.p, test_span.s) |
| 120 | + |
| 121 | + def test_create_task_without_context(self): |
| 122 | + async def run_later(msg="Hello"): |
| 123 | + # print("run_later: %s" % async_tracer.active_span.operation_name) |
| 124 | + async with aiohttp.ClientSession() as session: |
| 125 | + return await self.fetch(session, testenv["wsgi_server"] + "/") |
| 126 | + |
| 127 | + async def test(): |
| 128 | + with async_tracer.start_active_span('test'): |
| 129 | + asyncio.create_task(run_later("Hello")) |
| 130 | + await asyncio.sleep(0.5) |
| 131 | + |
| 132 | + self.loop.run_until_complete(test()) |
| 133 | + |
| 134 | + spans = self.recorder.queued_spans() |
| 135 | + |
| 136 | + self.assertEqual(2, len(spans)) |
| 137 | + self.assertEqual("sdk", spans[0].n) |
| 138 | + self.assertEqual("wsgi", spans[1].n) |
| 139 | + |
| 140 | + # Without the context propagated, we should get two separate traces |
| 141 | + self.assertNotEqual(spans[0].t, spans[1].t) |
0 commit comments