Skip to content

Commit 15a89f5

Browse files
mabdinurgithub-actions[bot]
authored andcommitted
fix(library): catch exceptions raised while enabling ddtrace integrations (#11759)
## Description - Improves the error message generated when `ddtrace` failed to patch/enable an integration. - Ensure patching modules and sub-modules are wrapped in a try-except. The ddtrace library should not crash an application if an integration can not be patched. ## Motivation Prevent issues like this: #11603 ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) (cherry picked from commit 79069a3)
1 parent 9924f37 commit 15a89f5

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

ddtrace/_monkey.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,22 @@ def on_import(hook):
171171
path = "%s.%s" % (prefix, module)
172172
try:
173173
imported_module = importlib.import_module(path)
174+
imported_module.patch()
175+
if hasattr(imported_module, "patch_submodules"):
176+
imported_module.patch_submodules(patch_indicator)
174177
except Exception as e:
175178
if raise_errors:
176179
raise
177-
error_msg = "failed to import ddtrace module %r when patching on import" % (path,)
178-
log.error(error_msg, exc_info=True)
179-
telemetry.telemetry_writer.add_integration(module, False, PATCH_MODULES.get(module) is True, error_msg)
180+
log.error(
181+
"failed to enable ddtrace support for %s: %s",
182+
module,
183+
str(e),
184+
)
185+
telemetry.telemetry_writer.add_integration(module, False, PATCH_MODULES.get(module) is True, str(e))
180186
telemetry.telemetry_writer.add_count_metric(
181187
"tracers", "integration_errors", 1, (("integration_name", module), ("error_type", type(e).__name__))
182188
)
183189
else:
184-
imported_module.patch()
185190
if hasattr(imported_module, "get_versions"):
186191
versions = imported_module.get_versions()
187192
for name, v in versions.items():
@@ -194,9 +199,6 @@ def on_import(hook):
194199
module, True, PATCH_MODULES.get(module) is True, "", version=version
195200
)
196201

197-
if hasattr(imported_module, "patch_submodules"):
198-
imported_module.patch_submodules(patch_indicator)
199-
200202
return on_import
201203

202204

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Integrations: Improved error handling for exceptions raised during the startup of ddtrace integrations. This reduces the likelihood of the ddtrace library raising unhandled exceptions.

tests/telemetry/test_telemetry.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,12 @@ def test_handled_integration_error(test_agent_session, run_python_code_in_subpro
243243
_, stderr, status, _ = run_python_code_in_subprocess(code, env=env)
244244

245245
assert status == 0, stderr
246-
expected_stderr = b"failed to import"
247-
assert expected_stderr in stderr
246+
assert b"failed to enable ddtrace support for sqlite3" in stderr
248247

249248
integrations_events = test_agent_session.get_events("app-integrations-change", subprocess=True)
250249
assert len(integrations_events) == 1
251250
assert (
252-
integrations_events[0]["payload"]["integrations"][0]["error"]
253-
== "failed to import ddtrace module 'ddtrace.contrib.sqlite3' when patching on import"
251+
integrations_events[0]["payload"]["integrations"][0]["error"] == "module 'sqlite3' has no attribute 'connect'"
254252
)
255253

256254
# Get metric containing the integration error

0 commit comments

Comments
 (0)