Skip to content

Commit 106a72f

Browse files
[python] Add asyncio check
1 parent ff5e849 commit 106a72f

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ARG BASE_IMAGE="prof-python-3.11"
2+
FROM $BASE_IMAGE
3+
4+
RUN pip install ddtrace
5+
6+
# Copy the Python target into the container
7+
COPY ./scenarios/python_asyncio_3.11/main.py /app/
8+
RUN chmod 644 /app/*
9+
10+
# Set the working directory to the location of the program
11+
WORKDIR /app
12+
13+
# Run the program when the container starts
14+
CMD python main.py
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"test_name": "python_asyncio",
3+
"pprof-regex": "",
4+
"stacks": [
5+
{
6+
"profile-type": "wall-time",
7+
"pprof-regex": "",
8+
"stack-content": [
9+
{
10+
"regular_expression": ".*;.*run;.*;BaseEventLoop\\.run_until_complete;.*;main;my_coroutine",
11+
"value": 1025498000,
12+
"error_margin": 20,
13+
"labels": [
14+
{
15+
"key": "thread name",
16+
"values": [
17+
"MainThread"
18+
]
19+
},
20+
{
21+
"key": "task name",
22+
"values": [
23+
"Task-1"
24+
]
25+
}
26+
]
27+
}
28+
]
29+
}
30+
],
31+
"scale_by_duration": false
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import asyncio
3+
4+
5+
async def my_coroutine(n: float) -> None:
6+
await asyncio.sleep(n)
7+
8+
9+
async def main() -> None:
10+
# Simple application that creates two Tasks with different durations:
11+
# - "unnamed Task" runs my_coroutine() for 2 second
12+
# - short_task runs my_coroutine() for 1 second
13+
# The profiler should capture both Tasks with their respective durations.
14+
15+
# Note: there currently is an issue in ddtrace that attaches one of the gathered Tasks to the "parent"
16+
# task. As a result, using an explicit name for the manually started Task would result in flakiness (as we cannot
17+
# know which Task name will be "absorbed" by the Parent).
18+
# 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.
19+
20+
# Note: additionally, there is an issue in how we count wall time that results in blatantly incorrect results.
21+
# We are in the process of making asyncio better in dd-trace-py; we will update the correctness check once that
22+
# issue is fixed.
23+
24+
from ddtrace.profiling import Profiler
25+
26+
prof = Profiler()
27+
prof.start() # Should be as early as possible, eg before other imports, to ensure everything is profiled
28+
29+
# Give the Profiler some time to start up
30+
await asyncio.sleep(0.5)
31+
32+
EXECUTION_TIME_SEC = float(os.environ.get("EXECUTION_TIME_SEC", "2"))
33+
34+
short_task = asyncio.create_task(my_coroutine(EXECUTION_TIME_SEC / 2))
35+
36+
# asyncio.gather will automatically wrap my_coroutine into a Task
37+
await asyncio.gather(short_task, my_coroutine(EXECUTION_TIME_SEC))
38+
39+
40+
if __name__ == "__main__":
41+
asyncio.run(main())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ddtrace

0 commit comments

Comments
 (0)