Skip to content

Commit 7e5cffe

Browse files
authored
chore(tracer): improve patching debug logs (backport #3568) (#3602)
This is an automatic backport of pull request #3568 done by [Mergify](https://mergify.com). --- <details> <summary>Mergify commands and options</summary> <br /> More conditions and actions can be found in the [documentation](https://docs.mergify.com/). You can also trigger Mergify actions by commenting on this pull request: - `@Mergifyio refresh` will re-evaluate the rules - `@Mergifyio rebase` will rebase this PR on its base branch - `@Mergifyio update` will merge the base branch into this PR - `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can: - look at your merge queues - generate the Mergify configuration with the config editor. Finally, you can contact us on https://mergify.com </details>
1 parent bc0e3c9 commit 7e5cffe

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

ddtrace/_monkey.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .internal.logger import get_logger
1313
from .internal.telemetry import telemetry_writer
1414
from .internal.utils import formats
15+
from .internal.utils.importlib import require_modules
1516
from .settings import _config as config
1617
from .vendor.debtcollector.removals import remove
1718

@@ -117,6 +118,15 @@ class ModuleNotFoundException(PatchException):
117118
pass
118119

119120

121+
class IntegrationNotAvailableException(PatchException):
122+
"""Exception for when an integration is not available.
123+
124+
Raised when the module required for an integration is not available.
125+
"""
126+
127+
pass
128+
129+
120130
def _on_import_factory(module, raise_errors=True):
121131
# type: (str, bool) -> Callable[[Any], None]
122132
"""Factory to create an import hook for the provided module name"""
@@ -223,6 +233,11 @@ def _patch_module(module, raise_errors=True):
223233
if raise_errors:
224234
raise
225235
return False
236+
except IntegrationNotAvailableException as e:
237+
if raise_errors:
238+
raise
239+
log.debug("integration %s not enabled (%s)", module, str(e)) # noqa: G200
240+
return False
226241
except Exception:
227242
if raise_errors:
228243
raise
@@ -267,8 +282,11 @@ def _attempt_patch_module(module):
267282
# if patch() is not available in the module, it means
268283
# that the library is not installed in the environment
269284
if not hasattr(imported_module, "patch"):
270-
raise AttributeError(
271-
"%s.patch is not found. '%s' is not configured for this environment" % (path, module)
285+
required_mods = getattr(imported_module, "required_modules", [])
286+
with require_modules(required_mods) as not_avail_mods:
287+
pass
288+
raise IntegrationNotAvailableException(
289+
"missing required module%s: %s" % ("s" if len(not_avail_mods) > 1 else "", ",".join(not_avail_mods))
272290
)
273291

274292
imported_module.patch()

ddtrace/contrib/algoliasearch/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
from ...internal.utils.importlib import require_modules
2727

2828

29-
with require_modules(["algoliasearch", "algoliasearch.version"]) as missing_modules:
29+
required_modules = ["algoliasearch", "algoliasearch.version"]
30+
31+
with require_modules(required_modules) as missing_modules:
3032
if not missing_modules:
3133
from .patch import patch
3234
from .patch import unpatch
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
other:
3+
- |
4+
internal: improve debug logs for tracer patching

tests/integration/test_integration.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,25 @@ def test_ddtrace_run_startup_logging_injection(ddtrace_run_python_code_in_subpro
777777
assert b"ValueError: Formatting field not found in record: 'dd.service'" not in err
778778

779779

780+
def test_no_module_debug_log(ddtrace_run_python_code_in_subprocess):
781+
env = os.environ.copy()
782+
env.update(
783+
dict(
784+
DD_TRACE_DEBUG="1",
785+
)
786+
)
787+
out, err, _, _ = ddtrace_run_python_code_in_subprocess(
788+
"""
789+
import logging
790+
from ddtrace import patch_all
791+
logging.basicConfig(level=logging.DEBUG)
792+
patch_all()
793+
""",
794+
env=env,
795+
)
796+
assert b"DEBUG:ddtrace._monkey:integration starlette not enabled (missing required module: starlette)" in err
797+
798+
780799
def test_no_warnings():
781800
env = os.environ.copy()
782801
# Have to disable sqlite3 as coverage uses it on process shutdown

0 commit comments

Comments
 (0)