Skip to content

Commit 68d1e96

Browse files
fix(logging): fix issue with dd.* properties not getting injected onto logging records [backport 3.10] (#13929)
Backport 265f660 from #13924 to 3.10. Resolves: #13892 ## 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) Co-authored-by: brettlangdon <[email protected]>
1 parent 2c2d3bf commit 68d1e96

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

ddtrace/contrib/internal/logging/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, trace_id: int, span_id: int, service: str, version: str, env:
5454
def _w_makeRecord(func, instance, args, kwargs):
5555
# Get the LogRecord instance for this log
5656
record = func(*args, **kwargs)
57-
if config._logs_injection != LogInjectionState.ENABLED:
57+
if config._logs_injection == LogInjectionState.DISABLED:
5858
# log injection is opt-in for non-structured logging
5959
return record
6060
record.__dict__.update(ddtrace.tracer.get_log_correlation_context())
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
logging: Fix issue when ``dd.*`` properties were not injected onto logging records unless ``DD_LOGS_ENABLED=true`` env var was set (default value is ``structured``).
5+
This issue causes problems for non-structured loggers which set their own format string instead of having ``ddtrace`` set the logging format string for you.

tests/contrib/logging/test_logging.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from io import StringIO
22
import logging
3+
import os
34

45
import pytest
56
import wrapt
@@ -299,3 +300,29 @@ def test_log_strformat_style_format(self):
299300
assert not hasattr(record, "dd")
300301
assert getattr(record, LOG_ATTR_TRACE_ID) == "{:032x}".format(span.trace_id)
301302
assert getattr(record, LOG_ATTR_SPAN_ID) == str(span.span_id)
303+
304+
305+
@pytest.mark.parametrize("dd_logs_enabled", ["true", "false", "structured"])
306+
def test_manual_log_formatter_injection(dd_logs_enabled: str, run_python_code_in_subprocess):
307+
code = """
308+
import ddtrace.auto
309+
310+
import logging
311+
312+
format_string = (
313+
"%(message)s - dd.service=%(dd.service)s dd.version=%(dd.version)s dd.env=%(dd.env)s"
314+
" dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s"
315+
)
316+
317+
log = logging.getLogger()
318+
logging.basicConfig(level=logging.INFO, format=format_string)
319+
log.info("Hello!")
320+
"""
321+
322+
env = os.environ.copy()
323+
env["DD_LOGS_ENABLED"] = dd_logs_enabled
324+
stdout, stderr, status, _ = run_python_code_in_subprocess(code, env=env)
325+
assert status == 0, stderr
326+
327+
assert stdout == b"", stderr
328+
assert stderr == b"Hello! - dd.service=ddtrace_subprocess_dir dd.version= dd.env= dd.trace_id=0 dd.span_id=0\n"

0 commit comments

Comments
 (0)