|
2 | 2 | Bootstrapping code that is run when using the `ddtrace-run` Python entrypoint |
3 | 3 | Add all monkey-patching that needs to run by default here |
4 | 4 | """ |
| 5 | +# _____ ___ _________ _____ ______ _____ ___ _ _ _____ |
| 6 | +# |_ _|| \/ || ___ \| _ || ___ \|_ _| / _ \ | \ | ||_ _| |
| 7 | +# | | | . . || |_/ /| | | || |_/ / | | / /_\ \| \| | | | |
| 8 | +# | | | |\/| || __/ | | | || / | | | _ || . ` | | | |
| 9 | +# _| |_ | | | || | \ \_/ /| |\ \ | | | | | || |\ | | | |
| 10 | +# \___/ \_| |_/\_| \___/ \_| \_| \_/ \_| |_/\_| \_/ \_/ |
| 11 | +# DO NOT MODIFY THIS FILE! |
| 12 | +# Only do so if you know what you're doing. This file contains boilerplate code |
| 13 | +# to allow injecting a custom sitecustomize.py file into the Python process to |
| 14 | +# perform the correct initialisation for the library. All the actual |
| 15 | +# initialisation logic should be placed in preload.py. |
5 | 16 | from ddtrace import LOADED_MODULES # isort:skip |
6 | 17 |
|
7 | 18 | import logging # noqa |
|
11 | 22 |
|
12 | 23 | from ddtrace import config # noqa |
13 | 24 | from ddtrace._logger import _configure_log_injection |
14 | | -from ddtrace.debugging._config import di_config # noqa |
15 | | -from ddtrace.debugging._config import ed_config # noqa |
16 | | -from ddtrace.internal.compat import PY2 # noqa |
17 | 25 | from ddtrace.internal.logger import get_logger # noqa |
18 | 26 | from ddtrace.internal.module import ModuleWatchdog # noqa |
19 | 27 | from ddtrace.internal.module import find_loader # noqa |
20 | | -from ddtrace.internal.runtime.runtime_metrics import RuntimeWorker # noqa |
21 | | -from ddtrace.internal.tracemethods import _install_trace_methods # noqa |
22 | 28 | from ddtrace.internal.utils.formats import asbool # noqa |
23 | | -from ddtrace.internal.utils.formats import parse_tags_str # noqa |
24 | | -from ddtrace.settings.asm import config as asm_config # noqa |
25 | 29 |
|
26 | 30 | # Debug mode from the tracer will do the same here, so only need to do this otherwise. |
27 | 31 | if config.logs_injection: |
|
43 | 47 | ) |
44 | 48 |
|
45 | 49 |
|
46 | | -if PY2: |
47 | | - _unloaded_modules = [] |
48 | | - |
49 | | - |
50 | 50 | def is_module_installed(module_name): |
51 | 51 | return find_loader(module_name) is not None |
52 | 52 |
|
53 | 53 |
|
54 | 54 | def cleanup_loaded_modules(): |
55 | 55 | def drop(module_name): |
56 | 56 | # type: (str) -> None |
57 | | - if PY2: |
58 | | - # Store a reference to deleted modules to avoid them being garbage |
59 | | - # collected |
60 | | - _unloaded_modules.append(sys.modules[module_name]) |
61 | 57 | del sys.modules[module_name] |
62 | 58 |
|
63 | 59 | MODULES_REQUIRING_CLEANUP = ("gevent",) |
@@ -88,16 +84,10 @@ def drop(module_name): |
88 | 84 | "google.protobuf", # the upb backend in >= 4.21 does not like being unloaded |
89 | 85 | ] |
90 | 86 | ) |
91 | | - if PY2: |
92 | | - KEEP_MODULES_PY2 = frozenset(["encodings", "codecs", "copy_reg"]) |
93 | 87 | for m in list(_ for _ in sys.modules if _ not in LOADED_MODULES): |
94 | 88 | if any(m == _ or m.startswith(_ + ".") for _ in KEEP_MODULES): |
95 | 89 | continue |
96 | 90 |
|
97 | | - if PY2: |
98 | | - if any(m == _ or m.startswith(_ + ".") for _ in KEEP_MODULES_PY2): |
99 | | - continue |
100 | | - |
101 | 91 | drop(m) |
102 | 92 |
|
103 | 93 | # TODO: The better strategy is to identify the core modues in LOADED_MODULES |
@@ -127,61 +117,9 @@ def _(threading): |
127 | 117 |
|
128 | 118 |
|
129 | 119 | try: |
130 | | - from ddtrace import tracer |
131 | | - |
132 | | - profiling = asbool(os.getenv("DD_PROFILING_ENABLED", False)) |
133 | | - |
134 | | - if profiling: |
135 | | - log.debug("profiler enabled via environment variable") |
136 | | - import ddtrace.profiling.auto # noqa: F401 |
137 | | - |
138 | | - if di_config.enabled or ed_config.enabled: |
139 | | - from ddtrace.debugging import DynamicInstrumentation |
140 | | - |
141 | | - DynamicInstrumentation.enable() |
| 120 | + import ddtrace.bootstrap.preload as preload # Perform the actual initialisation |
142 | 121 |
|
143 | | - if config._runtime_metrics_enabled: |
144 | | - RuntimeWorker.enable() |
145 | | - |
146 | | - if asbool(os.getenv("DD_IAST_ENABLED", False)): |
147 | | - from ddtrace.appsec._iast._utils import _is_python_version_supported |
148 | | - |
149 | | - if _is_python_version_supported(): |
150 | | - from ddtrace.appsec._iast._ast.ast_patching import _should_iast_patch |
151 | | - from ddtrace.appsec._iast._loader import _exec_iast_patched_module |
152 | | - |
153 | | - ModuleWatchdog.register_pre_exec_module_hook(_should_iast_patch, _exec_iast_patched_module) |
154 | | - |
155 | | - if asbool(os.getenv("DD_TRACE_ENABLED", default=True)): |
156 | | - from ddtrace import patch_all |
157 | | - |
158 | | - # We need to clean up after we have imported everything we need from |
159 | | - # ddtrace, but before we register the patch-on-import hooks for the |
160 | | - # integrations. |
161 | | - cleanup_loaded_modules() |
162 | | - modules_to_patch = os.getenv("DD_PATCH_MODULES") |
163 | | - modules_to_str = parse_tags_str(modules_to_patch) |
164 | | - modules_to_bool = {k: asbool(v) for k, v in modules_to_str.items()} |
165 | | - patch_all(**modules_to_bool) |
166 | | - |
167 | | - if config.trace_methods: |
168 | | - _install_trace_methods(config.trace_methods) |
169 | | - else: |
170 | | - cleanup_loaded_modules() |
171 | | - |
172 | | - # Only the import of the original sitecustomize.py is allowed after this |
173 | | - # point. |
174 | | - |
175 | | - if "DD_TRACE_GLOBAL_TAGS" in os.environ: |
176 | | - env_tags = os.getenv("DD_TRACE_GLOBAL_TAGS") |
177 | | - tracer.set_tags(parse_tags_str(env_tags)) |
178 | | - |
179 | | - if sys.version_info >= (3, 7) and config._otel_enabled: |
180 | | - from opentelemetry.trace import set_tracer_provider |
181 | | - |
182 | | - from ddtrace.opentelemetry import TracerProvider |
183 | | - |
184 | | - set_tracer_provider(TracerProvider()) |
| 122 | + cleanup_loaded_modules() |
185 | 123 |
|
186 | 124 | # Check for and import any sitecustomize that would have normally been used |
187 | 125 | # had ddtrace-run not been used. |
@@ -223,22 +161,15 @@ def _(threading): |
223 | 161 | else: |
224 | 162 | log.debug("additional sitecustomize found in: %s", sys.path) |
225 | 163 |
|
226 | | - if config._remote_config_enabled: |
227 | | - from ddtrace.internal.remoteconfig.worker import remoteconfig_poller |
228 | | - |
229 | | - remoteconfig_poller.enable() |
230 | | - |
231 | | - if asm_config._asm_enabled or config._remote_config_enabled: |
232 | | - from ddtrace.appsec._remoteconfiguration import enable_appsec_rc |
233 | | - |
234 | | - enable_appsec_rc() |
235 | | - |
236 | 164 | config._ddtrace_bootstrapped = True |
237 | 165 | # Loading status used in tests to detect if the `sitecustomize` has been |
238 | 166 | # properly loaded without exceptions. This must be the last action in the module |
239 | 167 | # when the execution ends with a success. |
240 | 168 | loaded = True |
241 | | - tracer._generate_diagnostic_logs() |
| 169 | + |
| 170 | + for f in preload.post_preload: |
| 171 | + f() |
| 172 | + |
242 | 173 | except Exception: |
243 | 174 | loaded = False |
244 | 175 | log.warning("error configuring Datadog tracing", exc_info=True) |
0 commit comments