Skip to content

fix(profiling): make explicitly marked main packages "my code" #13649

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

Draft
wants to merge 1 commit 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
14 changes: 11 additions & 3 deletions ddtrace/internal/datadog/profiling/code_provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import sysconfig
import typing as t

from ddtrace.internal import gitmetadata
from ddtrace.internal.packages import _package_for_root_module_mapping


class Library:

def __init__(
self,
kind: str,
Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(self):
spec = importlib.util.find_spec(name)
if spec and spec.origin == "frozen":
python_stdlib.paths.add(f"<frozen {spec.name}>")
except Exception: # nosec
except Exception: # nosec
continue

self.libraries.append(python_stdlib)
Expand All @@ -69,16 +69,24 @@ def __init__(self):

libraries: t.Dict[str, Library] = {}

_, _, main_package = gitmetadata.get_git_tags()

site_packages = Path(sysconfig.get_path("purelib"))
for module, dist in module_to_distribution.items():
name = dist.name
# special case for __pycache__/filename.cpython-3xx.pyc -> filename.py
if module.startswith("__pycache__/"):
module = module[len("__pycache__/") :].split(".")[0] + ".py"

# If the user installed their code like a library and is running it
# as the main package (python -m my_package), and they explicitly
# specified that that's the main package, make sure it shows up as
# "my code" in the UI. Do this by leaving the "kind" blank
kind = "library" if name != main_package else ""

lib = libraries.get(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we remove main_package from libraries instead of doing a ternary in a loop?

if lib is None:
lib = Library(kind="library", name=name, version=dist.version, paths=set())
lib = Library(kind=kind, name=name, version=dist.version, paths=set())
libraries[name] = lib

# We assume that each module is a directory or a python file
Expand Down
5 changes: 5 additions & 0 deletions tests/profiling_v2/test_code_provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,8 @@ def test_stdlib_paths(self):

for path in stdlib_paths:
assert path.startswith("<frozen") or path == sysconfig.get_path("stdlib")

def test_main_package_my_code(self):
# TODO: set DD_MAIN_PACKAGE to something and confirm that the package
# does not have a "kind" in the code provenance
pass
Loading