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