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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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: 11 additions & 2 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 @@ -87,6 +87,15 @@ def __init__(self):
if module.endswith(".py") or module_path.is_dir():
lib.paths.add(str(module_path))

# 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 (but not deleting the
# library so we can still associate the library with its files)
_, _, main_package = gitmetadata.get_git_tags()
Copy link
Contributor

Choose a reason for hiding this comment

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

(unrelated) I find it a bit weird that we have to retrieve this information via a git metadata utility module 🤔

if info := libraries.get(main_package, None):
info.kind = ""

self.libraries.extend(libraries.values())

def to_dict(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- profiling: |
Fix a bug where profile frames from the package specified by DD_MAIN_PACKAGE
were marked as "library" code in the profiler UI
17 changes: 17 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,20 @@ def test_stdlib_paths(self):

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

@pytest.mark.subprocess(
env=dict(DD_MAIN_PACKAGE="ddtrace"),
)
def test_main_package_my_code(self):
import json

from ddtrace.internal.datadog.profiling.code_provenance import json_str_to_export

json_str = json_str_to_export()
json_obj = json.loads(json_str)
# Really what we're checking is that whatever package the user calls the
# "main" pacakge isn't called a library. Normally this would matter if
# the user installs their package as a dependency and then runs it as
# the main package. But for the sake of this test, just call ddtrace the
# "main" package.
assert any(info["name"] == "ddtrace" and info["kind"] == "" for info in json_obj["v1"])
Loading