Skip to content

Commit c4fc9a9

Browse files
mabdinurgithub-actions[bot]
authored andcommitted
fix(logging): remove log correlation attributes from ddtrace loggers (#14979)
## Description Fixes ddtrace internal logging errors when trace-log correlation is disabled programmatically. ## Problem ddtrace internal logs were expecting correlation attributes (`dd.service`, etc.) even when log injection was disabled, causing `ValueError: Formatting field not found in record` errors. ## Solution Removed set_log_formatting to prevent mismatches between expected and actual log record attributes. When log correlation is disabled but DD_LOG_FORMAT is set, internal ddtrace logs can raise a ValueError. This also prevents issues when user configured log formatters override log formatters set by ddtrace. Since span start and finish now emit debug logs, including trace attributes in all ddtrace log records is no longer necessary. ## Testing Verified ddtrace internal logs work correctly with correlation disabled. ## Risks None - fixes existing bug. (cherry picked from commit b433817)
1 parent 7451e84 commit c4fc9a9

File tree

4 files changed

+7
-18
lines changed

4 files changed

+7
-18
lines changed

ddtrace/_logger.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ def _add_file_handler(
110110
return ddtrace_file_handler
111111

112112

113-
def set_log_formatting():
114-
# type: () -> None
115-
"""Sets the log format for the ddtrace logger."""
116-
ddtrace_logger = logging.getLogger("ddtrace")
117-
for handler in ddtrace_logger.handlers:
118-
handler.setFormatter(logging.Formatter(DD_LOG_FORMAT))
119-
120-
121113
def get_log_injection_state(raw_config: Optional[str]) -> bool:
122114
"""Returns the current log injection state."""
123115
if raw_config:

ddtrace/contrib/internal/logging/patch.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import ddtrace
77
from ddtrace import config
8-
from ddtrace._logger import set_log_formatting
98
from ddtrace.contrib.internal.trace_utils import unwrap as _u
109
from ddtrace.internal.constants import LOG_ATTR_ENV
1110
from ddtrace.internal.constants import LOG_ATTR_SERVICE
@@ -100,13 +99,6 @@ def patch():
10099
_w(logging.Logger, "makeRecord", _w_makeRecord)
101100
_w(logging.StrFormatStyle, "_format", _w_StrFormatStyle_format)
102101

103-
if config._logs_injection:
104-
# Only set the formatter is DD_LOGS_INJECTION is set to True. We do not want to modify
105-
# unstructured logs if a user has not enabled logs injection.
106-
# Also, the Datadog log format must be set after the logging module has been patched,
107-
# otherwise the formatter will raise an exception.
108-
set_log_formatting()
109-
110102

111103
def unpatch():
112104
if getattr(logging, "_datadog_patch", False):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
logging: Fixed ddtrace internal logging when trace-log correlation is disabled.
5+
Prevents ``ValueError: Formatting field not found in record: 'dd.service'``.

tests/integration/test_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,8 @@ def test_logging_during_tracer_init_succeeds_when_debug_logging_and_logs_injecti
808808
assert out == b"", "an empty program should generate no logs under ddtrace-run"
809809

810810
assert (
811-
b"[dd.service=ddtrace_subprocess_dir dd.env= dd.version= dd.trace_id=0 dd.span_id=0]" in err
812-
), "stderr should contain debug output when DD_TRACE_DEBUG is set"
811+
b"[dd.service=ddtrace_subprocess_dir dd.env= dd.version= dd.trace_id=0 dd.span_id=0]" not in err
812+
), "stderr should not contain debug output when DD_TRACE_DEBUG is set"
813813

814814
assert b"KeyError: 'dd.service'" not in err, "stderr should not contain any exception logs"
815815
assert (

0 commit comments

Comments
 (0)