Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(asyncio): avoid overwriting coroutine names #12011

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ddtrace/contrib/internal/asyncio/patch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import sys

from ddtrace.internal.utils import get_argument_value
from ddtrace.internal.utils import set_argument_value
Expand All @@ -7,6 +8,9 @@
from ddtrace.trace import Pin


PY_VERSION = sys.version_info[:2]


def get_version():
# type: () -> str
return ""
Expand Down Expand Up @@ -49,5 +53,12 @@ async def traced_coro(*args_c, **kwargs_c):
pin.tracer.context_provider.activate(dd_active)
return await coro

args, kwargs = set_argument_value(args, kwargs, 1, "coro", traced_coro())
# try to persist the original function name this useful for debugging
tc = traced_coro()
if hasattr(coro, "__name__"):
tc.__name__ = coro.__name__
if hasattr(coro, "__qualname__"):
tc.__qualname__ = coro.__qualname__
args, kwargs = set_argument_value(args, kwargs, 1, "coro", tc)

return wrapped(*args, **kwargs)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
asyncio: Fix an issue where the name of a coroutine was being overridden by a ddtrace function.
30 changes: 30 additions & 0 deletions tests/contrib/asyncio/test_tracer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Ensure that the tracer works with asynchronous executions within the same ``IOLoop``."""
import asyncio
import os
import re

import pytest

Expand Down Expand Up @@ -208,3 +210,31 @@ async def test():
spans = tracer.get_spans()
assert len(spans) == 3
assert spans[0].trace_id == spans[1].trace_id == spans[2].trace_id


def test_asyncio_scheduled_tasks_debug_logs(run_python_code_in_subprocess):
code = """
import ddtrace

ddtrace.patch(asyncio=True)
import asyncio
import time
import pytest


async def my_function():
time.sleep(2)

if __name__ == "__main__":
asyncio.run(my_function())
"""
env = os.environ.copy()
env["PYTHONASYNCIODEBUG"] = "1"
out, err, status, _ = run_python_code_in_subprocess(code, env=env)
assert status == 0, err + out

pattern = rb"Executing <Task finished name=\'Task-1\' coro=<my_function\(\) done, "
rb"defined at .*/dd-trace-py/ddtrace/contrib/internal/asyncio/patch.py:.* result=None "
rb"created at .*/dd-trace-py/ddtrace/contrib/internal/asyncio/patch.py:.* took .* seconds"
match = re.match(pattern, err)
assert match, err
Loading