Skip to content

Commit 58249cf

Browse files
P403n1x87mabdinur
andauthored
refactor(profiling): eager code provenance file-package mapping (#4926)
## Description This refactor ensures that the file-to-package mapping is created as early as possible in the parent process, when code provenance is enabled, to avoid repeating the work in child worker processes. ## Checklist - [ ] Followed the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) when writing a release note. - [ ] Add additional sections for `feat` and `fix` pull requests. - [ ] [Library documentation](https://github.com/DataDog/dd-trace-py/tree/1.x/docs) and/or [Datadog's documentation site](https://github.com/DataDog/documentation/) is updated. Link to doc PR in description. ## Reviewer Checklist - [x] Title is accurate. - [x] Description motivates each change. - [x] No unnecessary changes were introduced in this PR. - [x] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Tests provided or description of manual testing performed is included in the code or PR. - [x] Release note has been added and follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines), or else `changelog/no-changelog` label added. - [x] All relevant GitHub issues are correctly linked. - [x] Backports are identified and tagged with Mergifyio. Co-authored-by: Munir Abdinur <[email protected]>
1 parent 539f3f4 commit 58249cf

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

ddtrace/profiling/exporter/_packages.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# -*- encoding: utf-8 -*-
2-
import enum
32
import logging
43
import os
54
import sys
65

6+
from ddtrace.internal.utils.formats import asbool
7+
78

89
try:
910
import pathlib
@@ -90,30 +91,28 @@ def _build_package_file_mapping():
9091
return mapping
9192

9293

93-
class _load_status(enum.Enum):
94-
FAILED = "failed"
95-
96-
97-
_FILE_PACKAGE_MAPPING = None # type: typing.Optional[typing.Union[typing.Dict[str, Distribution], _load_status]]
94+
_FILE_PACKAGE_MAPPING = None # type: typing.Optional[typing.Dict[str, Distribution]]
95+
if asbool(os.getenv("DD_PROFILING_ENABLE_CODE_PROVENANCE", False)):
96+
# DEV: If code provenance is enabled, create the mapping as soon as this
97+
# module is imported. This should happen in the parent process to
98+
# guarantee that the work is not repeated in child worker processes.
99+
try:
100+
_FILE_PACKAGE_MAPPING = _build_package_file_mapping()
101+
except Exception:
102+
LOG.error(
103+
"Unable to build package file mapping, "
104+
"please report this to https://github.com/DataDog/dd-trace-py/issues",
105+
exc_info=True,
106+
)
98107

99108

100109
def filename_to_package(
101110
filename, # type: str
102111
):
103112
# type: (...) -> typing.Optional[Distribution]
104113
global _FILE_PACKAGE_MAPPING
114+
105115
if _FILE_PACKAGE_MAPPING is None:
106-
try:
107-
_FILE_PACKAGE_MAPPING = _build_package_file_mapping()
108-
except Exception:
109-
_FILE_PACKAGE_MAPPING = _load_status.FAILED
110-
LOG.error(
111-
"Unable to build package file mapping, "
112-
"please report this to https://github.com/DataDog/dd-trace-py/issues",
113-
exc_info=True,
114-
)
115-
return None
116-
elif _FILE_PACKAGE_MAPPING is _load_status.FAILED:
117116
return None
118117

119118
if filename not in _FILE_PACKAGE_MAPPING and filename.endswith(".pyc"):

tests/profiling/exporter/test_packages.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ def _raise():
4242

4343
# type: (...) -> None
4444
assert _packages.filename_to_package(_packages.__file__) is None
45-
assert len(caplog.records) == 1
46-
assert caplog.records[0].message == (
47-
"Unable to build package file mapping, please report this to https://github.com/DataDog/dd-trace-py/issues"
48-
)
4945

5046

5147
def test_filename_to_package():
5248
# type: (...) -> None
53-
_packages._FILE_PACKAGE_MAPPING = None
49+
_packages._FILE_PACKAGE_MAPPING = _packages._build_package_file_mapping()
5450

5551
package = _packages.filename_to_package(_packages.__file__)
5652
assert package.name == "ddtrace"

tests/profiling/exporter/test_pprof.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ddtrace.profiling.collector import _lock
99
from ddtrace.profiling.collector import memalloc
1010
from ddtrace.profiling.collector import stack_event
11+
from ddtrace.profiling.exporter import _packages
1112
from ddtrace.profiling.exporter import pprof
1213

1314

@@ -709,6 +710,7 @@ def test_pprof_exporter(gan):
709710

710711
@mock.patch("ddtrace.internal.utils.config.get_application_name")
711712
def test_pprof_exporter_libs(gan):
713+
_packages._FILE_PACKAGE_MAPPING = _packages._build_package_file_mapping()
712714
gan.return_value = "bonjour"
713715
exp = pprof.PprofExporter()
714716
TEST_EVENTS = {

0 commit comments

Comments
 (0)