|
1 | 1 | # -*- encoding: utf-8 -*- |
2 | | -import enum |
3 | 2 | import logging |
4 | 3 | import os |
5 | 4 | import sys |
6 | 5 |
|
| 6 | +from ddtrace.internal.utils.formats import asbool |
| 7 | + |
7 | 8 |
|
8 | 9 | try: |
9 | 10 | import pathlib |
@@ -90,30 +91,28 @@ def _build_package_file_mapping(): |
90 | 91 | return mapping |
91 | 92 |
|
92 | 93 |
|
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 | + ) |
98 | 107 |
|
99 | 108 |
|
100 | 109 | def filename_to_package( |
101 | 110 | filename, # type: str |
102 | 111 | ): |
103 | 112 | # type: (...) -> typing.Optional[Distribution] |
104 | 113 | global _FILE_PACKAGE_MAPPING |
| 114 | + |
105 | 115 | 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: |
117 | 116 | return None |
118 | 117 |
|
119 | 118 | if filename not in _FILE_PACKAGE_MAPPING and filename.endswith(".pyc"): |
|
0 commit comments