Skip to content

Commit 0efd33a

Browse files
authored
fix(lib-injection): log only to stderr when in debug mode [backport 1.20] (#7165)
The stderr logs are noisy and not actionable when running many Python scripts on a system. It is also possible that the injection breaks scripts and applications that rely on specific stderr output. The logs can still be printed to stderr when using `DD_TRACE_DEBUG=1`. It was considered to write the logs to [system logs](https://docs.python.org/3/library/syslog.html) but opted against until we can better evaluate it as an approach. There are some risks like permission issues, noise and usability that need to be explored a little bit more before going ahead with it. For now we just opt into stderr logs with `DD_TRACE_DEBUG=1`. Public docs PR: DataDog/documentation#19944 #7061 - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] 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) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that.
1 parent 496676b commit 0efd33a

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

docs/spelling_wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ sqlite
207207
stacktrace
208208
starlette
209209
statsd
210+
stderr
211+
stdout
210212
stringable
211213
subclass
212214
subdirectory

lib-injection/sitecustomize.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
import os
66
import sys
7+
import time
78

89

910
debug_mode = os.environ.get("DD_TRACE_DEBUG", "").lower() in ("true", "1", "t")
@@ -28,9 +29,10 @@ def _log(msg, *args, level="info"):
2829
This function is provided instead of built-in Python logging since we can't rely on any logger
2930
being configured.
3031
"""
31-
if not debug_mode and level == "debug":
32-
return
33-
print("%s:datadog.autoinstrumentation(pid: %d): " % (level.upper(), os.getpid()) + msg % args, file=sys.stderr)
32+
if debug_mode:
33+
asctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
34+
msg = "[%s] [%s] datadog.autoinstrumentation(pid: %d): " % (asctime, level.upper(), os.getpid()) + msg % args
35+
print(msg, file=sys.stderr)
3436

3537

3638
try:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
lib-injection: changes the log output to opt-in. Logging to stderr could interfere with applications. Logs can still be sent to stderr using `DD_TRACE_DEBUG=1`.

0 commit comments

Comments
 (0)