|
| 1 | +import pytest |
| 2 | + |
| 3 | + |
| 4 | +@pytest.mark.subprocess( |
| 5 | + env=dict( |
| 6 | + DD_PROFILING_OUTPUT_PPROF="/tmp/test_stack_asyncio_gather", |
| 7 | + ), |
| 8 | + err=None, |
| 9 | +) |
| 10 | +# For macOS: err=None ignores expected stderr from tracer failing to connect to agent (not relevant to this test) |
| 11 | +def test_asyncio_gather() -> None: |
| 12 | + import asyncio |
| 13 | + import os |
| 14 | + import time |
| 15 | + import uuid |
| 16 | + |
| 17 | + from ddtrace import ext |
| 18 | + from ddtrace.internal.datadog.profiling import stack_v2 |
| 19 | + from ddtrace.profiling import profiler |
| 20 | + from ddtrace.trace import tracer |
| 21 | + from tests.profiling.collector import pprof_utils |
| 22 | + |
| 23 | + assert stack_v2.is_available, stack_v2.failure_msg |
| 24 | + |
| 25 | + sleep_time = 0.2 |
| 26 | + loop_run_time = 3 |
| 27 | + |
| 28 | + async def inner() -> None: |
| 29 | + start_time = time.time() |
| 30 | + while time.time() < start_time + loop_run_time: |
| 31 | + await asyncio.sleep(sleep_time) |
| 32 | + |
| 33 | + async def outer(): |
| 34 | + t1 = asyncio.create_task(inner(), name="inner 1") |
| 35 | + t2 = asyncio.create_task(inner(), name="inner 2") |
| 36 | + await asyncio.gather(t1, t2) |
| 37 | + |
| 38 | + resource = str(uuid.uuid4()) |
| 39 | + span_type = ext.SpanTypes.WEB |
| 40 | + |
| 41 | + p = profiler.Profiler(tracer=tracer) |
| 42 | + p.start() |
| 43 | + with tracer.trace("test_asyncio", resource=resource, span_type=span_type) as span: |
| 44 | + span_id = span.span_id |
| 45 | + local_root_span_id = span._local_root.span_id |
| 46 | + |
| 47 | + loop = asyncio.new_event_loop() |
| 48 | + asyncio.set_event_loop(loop) |
| 49 | + main_task = loop.create_task(outer(), name="outer") |
| 50 | + loop.run_until_complete(main_task) |
| 51 | + |
| 52 | + p.stop() |
| 53 | + |
| 54 | + output_filename = os.environ["DD_PROFILING_OUTPUT_PPROF"] + "." + str(os.getpid()) |
| 55 | + |
| 56 | + profile = pprof_utils.parse_newest_profile(output_filename) |
| 57 | + |
| 58 | + samples_with_span_id = pprof_utils.get_samples_with_label_key(profile, "span id") |
| 59 | + assert len(samples_with_span_id) > 0 |
| 60 | + |
| 61 | + # get samples with task_name |
| 62 | + samples = pprof_utils.get_samples_with_label_key(profile, "task name") |
| 63 | + # The next fails if stack_v2 is not properly configured with asyncio task |
| 64 | + # tracking via ddtrace.profiling._asyncio |
| 65 | + assert len(samples) > 0 |
| 66 | + |
| 67 | + pprof_utils.assert_profile_has_sample( |
| 68 | + profile, |
| 69 | + samples, |
| 70 | + expected_sample=pprof_utils.StackEvent( |
| 71 | + thread_name="MainThread", |
| 72 | + task_name="outer", |
| 73 | + span_id=span_id, |
| 74 | + local_root_span_id=local_root_span_id, |
| 75 | + locations=[ |
| 76 | + pprof_utils.StackLocation( |
| 77 | + function_name="outer", filename="test_stack_asyncio.py", line_no=outer.__code__.co_firstlineno + 3 |
| 78 | + ), |
| 79 | + # TODO: We should add the locations of the gathered Tasks here as they should be in the same Stack |
| 80 | + ], |
| 81 | + ), |
| 82 | + ) |
| 83 | + |
| 84 | + pprof_utils.assert_profile_has_sample( |
| 85 | + profile, |
| 86 | + samples, |
| 87 | + expected_sample=pprof_utils.StackEvent( |
| 88 | + thread_name="MainThread", |
| 89 | + task_name="inner 1", |
| 90 | + span_id=span_id, |
| 91 | + local_root_span_id=local_root_span_id, |
| 92 | + locations=[ |
| 93 | + pprof_utils.StackLocation( |
| 94 | + function_name="inner", filename="test_stack_asyncio.py", line_no=inner.__code__.co_firstlineno + 3 |
| 95 | + ), |
| 96 | + ], |
| 97 | + ), |
| 98 | + ) |
| 99 | + |
| 100 | + pprof_utils.assert_profile_has_sample( |
| 101 | + profile, |
| 102 | + samples, |
| 103 | + expected_sample=pprof_utils.StackEvent( |
| 104 | + thread_name="MainThread", |
| 105 | + task_name="inner 2", |
| 106 | + span_id=span_id, |
| 107 | + local_root_span_id=local_root_span_id, |
| 108 | + locations=[ |
| 109 | + pprof_utils.StackLocation( |
| 110 | + function_name="inner", filename="test_stack_asyncio.py", line_no=inner.__code__.co_firstlineno + 3 |
| 111 | + ), |
| 112 | + ], |
| 113 | + ), |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.subprocess( |
| 118 | + env=dict( |
| 119 | + DD_PROFILING_OUTPUT_PPROF="/tmp/test_stack_asyncio_wait", |
| 120 | + ), |
| 121 | + err=None, |
| 122 | +) |
| 123 | +# For macOS: err=None ignores expected stderr from tracer failing to connect to agent (not relevant to this test) |
| 124 | +def test_asyncio_wait() -> None: |
| 125 | + import asyncio |
| 126 | + import os |
| 127 | + import time |
| 128 | + import uuid |
| 129 | + |
| 130 | + from ddtrace import ext |
| 131 | + from ddtrace.internal.datadog.profiling import stack_v2 |
| 132 | + from ddtrace.profiling import profiler |
| 133 | + from ddtrace.trace import tracer |
| 134 | + from tests.profiling.collector import pprof_utils |
| 135 | + |
| 136 | + assert stack_v2.is_available, stack_v2.failure_msg |
| 137 | + |
| 138 | + sleep_time = 0.2 |
| 139 | + loop_run_time = 3 |
| 140 | + |
| 141 | + async def inner() -> None: |
| 142 | + start_time = time.time() |
| 143 | + while time.time() < start_time + loop_run_time: |
| 144 | + await asyncio.sleep(sleep_time) |
| 145 | + |
| 146 | + async def outer() -> None: |
| 147 | + t1 = asyncio.create_task(inner(), name="inner 1") |
| 148 | + t2 = asyncio.create_task(inner(), name="inner 2") |
| 149 | + await asyncio.wait(fs=(t1, t2), return_when=asyncio.ALL_COMPLETED) |
| 150 | + |
| 151 | + resource = str(uuid.uuid4()) |
| 152 | + span_type = ext.SpanTypes.WEB |
| 153 | + |
| 154 | + p = profiler.Profiler(tracer=tracer) |
| 155 | + p.start() |
| 156 | + with tracer.trace("test_asyncio", resource=resource, span_type=span_type) as span: |
| 157 | + span_id = span.span_id |
| 158 | + local_root_span_id = span._local_root.span_id |
| 159 | + |
| 160 | + loop = asyncio.new_event_loop() |
| 161 | + asyncio.set_event_loop(loop) |
| 162 | + main_task = loop.create_task(outer(), name="outer") |
| 163 | + loop.run_until_complete(main_task) |
| 164 | + |
| 165 | + p.stop() |
| 166 | + |
| 167 | + output_filename = os.environ["DD_PROFILING_OUTPUT_PPROF"] + "." + str(os.getpid()) |
| 168 | + |
| 169 | + profile = pprof_utils.parse_newest_profile(output_filename) |
| 170 | + |
| 171 | + samples_with_span_id = pprof_utils.get_samples_with_label_key(profile, "span id") |
| 172 | + assert len(samples_with_span_id) > 0 |
| 173 | + |
| 174 | + # get samples with task_name |
| 175 | + samples = pprof_utils.get_samples_with_label_key(profile, "task name") |
| 176 | + # The next fails if stack_v2 is not properly configured with asyncio task |
| 177 | + # tracking via ddtrace.profiling._asyncio |
| 178 | + assert len(samples) > 0 |
| 179 | + |
| 180 | + pprof_utils.assert_profile_has_sample( |
| 181 | + profile, |
| 182 | + samples, |
| 183 | + expected_sample=pprof_utils.StackEvent( |
| 184 | + thread_name="MainThread", |
| 185 | + task_name="outer", |
| 186 | + span_id=span_id, |
| 187 | + local_root_span_id=local_root_span_id, |
| 188 | + locations=[ |
| 189 | + pprof_utils.StackLocation( |
| 190 | + function_name="outer", filename="test_stack_asyncio.py", line_no=outer.__code__.co_firstlineno + 3 |
| 191 | + ), |
| 192 | + # TODO: We should add the locations of the gathered Tasks here as they should be in the same Stack |
| 193 | + ], |
| 194 | + ), |
| 195 | + ) |
| 196 | + |
| 197 | + pprof_utils.assert_profile_has_sample( |
| 198 | + profile, |
| 199 | + samples, |
| 200 | + expected_sample=pprof_utils.StackEvent( |
| 201 | + thread_name="MainThread", |
| 202 | + task_name="inner 1", |
| 203 | + span_id=span_id, |
| 204 | + local_root_span_id=local_root_span_id, |
| 205 | + locations=[ |
| 206 | + pprof_utils.StackLocation( |
| 207 | + function_name="inner", filename="test_stack_asyncio.py", line_no=inner.__code__.co_firstlineno + 3 |
| 208 | + ), |
| 209 | + ], |
| 210 | + ), |
| 211 | + ) |
| 212 | + |
| 213 | + pprof_utils.assert_profile_has_sample( |
| 214 | + profile, |
| 215 | + samples, |
| 216 | + expected_sample=pprof_utils.StackEvent( |
| 217 | + thread_name="MainThread", |
| 218 | + task_name="inner 2", |
| 219 | + span_id=span_id, |
| 220 | + local_root_span_id=local_root_span_id, |
| 221 | + locations=[ |
| 222 | + pprof_utils.StackLocation( |
| 223 | + function_name="inner", filename="test_stack_asyncio.py", line_no=inner.__code__.co_firstlineno + 3 |
| 224 | + ), |
| 225 | + ], |
| 226 | + ), |
| 227 | + ) |
0 commit comments