Skip to content

Commit 70ed82e

Browse files
[python_asyncio] Fix some flakiness
1 parent 49b9bc0 commit 70ed82e

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

scenarios/python_asyncio_3.11/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ RUN chmod 644 /app/*
1010
# Set the working directory to the location of the program
1111
WORKDIR /app
1212

13+
ENV ECHION_USE_FAST_COPY_MEMORY=1
14+
ENV _DD_PROFILING_STACK_ADAPTIVE_SAMPLING_ENABLED=0
15+
ENV DD_PROFILING_ENABLED=true
16+
1317
# Run the program when the container starts
14-
CMD python main.py
18+
CMD ddtrace-run python main.py

scenarios/python_asyncio_3.11/expected_profile.json

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"stack-content": [
99
{
1010
"regular_expression": ".*;.*run;.*;BaseEventLoop\\.run_until_complete;.*;main;my_coroutine",
11-
"value": 1025498000,
11+
"value": 2525498000,
1212
"error_margin": 20,
1313
"labels": [
1414
{
@@ -20,7 +20,32 @@
2020
{
2121
"key": "task name",
2222
"values": [
23-
"Task-1"
23+
"short_task"
24+
]
25+
}
26+
]
27+
}
28+
]
29+
},
30+
{
31+
"profile-type": "wall-time",
32+
"pprof-regex": "",
33+
"stack-content": [
34+
{
35+
"regular_expression": ".*;.*run;.*;BaseEventLoop\\.run_until_complete;.*;main;my_coroutine",
36+
"value": 5000000000,
37+
"error_margin": 10,
38+
"labels": [
39+
{
40+
"key": "thread name",
41+
"values": [
42+
"MainThread"
43+
]
44+
},
45+
{
46+
"key": "task name",
47+
"values": [
48+
"Task-3"
2449
]
2550
}
2651
]

scenarios/python_asyncio_3.11/main.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import asyncio
22
import os
33

4-
from ddtrace.profiling import Profiler
5-
64

75
async def my_coroutine(n: float) -> None:
86
await asyncio.sleep(n)
@@ -14,24 +12,12 @@ async def main() -> None:
1412
# - short_task runs my_coroutine() for 1 second
1513
# The profiler should capture both Tasks with their respective durations.
1614

17-
# Note: there currently is an issue in ddtrace that attaches one of the gathered Tasks to the "parent"
18-
# task. As a result, using an explicit name for the manually started Task would result in flakiness (as we cannot
19-
# know which Task name will be "absorbed" by the Parent).
20-
# For the time being, we thus don't name the Task, so that we will always have Task-1 and Task-2 in the Profile.
21-
22-
# Note: additionally, there is an issue in how we count wall time that results in blatantly incorrect results.
23-
# We are in the process of making asyncio better in dd-trace-py; we will update the correctness check once that
24-
# issue is fixed.
25-
26-
prof = Profiler()
27-
prof.start() # Should be as early as possible, eg before other imports, to ensure everything is profiled
28-
2915
# Give the Profiler some time to start up
3016
await asyncio.sleep(0.5)
3117

32-
execution_time_sec = float(os.environ.get("EXECUTION_TIME_SEC", "2"))
18+
execution_time_sec = float(os.environ.get("EXECUTION_TIME_SEC", "5"))
3319

34-
short_task = asyncio.create_task(my_coroutine(execution_time_sec / 2))
20+
short_task = asyncio.create_task(my_coroutine(execution_time_sec / 2.0), name="short_task")
3521

3622
# asyncio.gather will automatically wrap my_coroutine into a Task
3723
await asyncio.gather(short_task, my_coroutine(execution_time_sec))

0 commit comments

Comments
 (0)