Skip to content
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

fix(library): catch exceptions raised while enabling ddtrace integrations #11759

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
16 changes: 9 additions & 7 deletions ddtrace/_monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,22 @@ def on_import(hook):
path = "%s.%s" % (prefix, module)
try:
imported_module = importlib.import_module(path)
imported_module.patch()
if hasattr(imported_module, "patch_submodules"):
imported_module.patch_submodules(patch_indicator)
except Exception as e:
if raise_errors:
raise
error_msg = "failed to import ddtrace module %r when patching on import" % (path,)
log.error(error_msg, exc_info=True)
telemetry.telemetry_writer.add_integration(module, False, PATCH_MODULES.get(module) is True, error_msg)
log.error(
"failed to enable ddtrace support for %s: %s",
module,
str(e),
)
telemetry.telemetry_writer.add_integration(module, False, PATCH_MODULES.get(module) is True, str(e))
telemetry.telemetry_writer.add_count_metric(
"tracers", "integration_errors", 1, (("integration_name", module), ("error_type", type(e).__name__))
)
else:
imported_module.patch()
if hasattr(imported_module, "get_versions"):
versions = imported_module.get_versions()
for name, v in versions.items():
Expand All @@ -194,9 +199,6 @@ def on_import(hook):
module, True, PATCH_MODULES.get(module) is True, "", version=version
)

if hasattr(imported_module, "patch_submodules"):
imported_module.patch_submodules(patch_indicator)

return on_import


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Integrations: Improved error handling for exceptions raised during the startup of ddtrace integrations. This reduces the likelihood of the ddtrace library raising unhandled exceptions.
mabdinur marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 2 additions & 4 deletions tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,12 @@ def test_handled_integration_error(test_agent_session, run_python_code_in_subpro
_, stderr, status, _ = run_python_code_in_subprocess(code, env=env)

assert status == 0, stderr
expected_stderr = b"failed to import"
assert expected_stderr in stderr
assert b"failed to enable ddtrace support for sqlite3" in stderr

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

# Get metric containing the integration error
Expand Down
Loading