Skip to content

feat(logging): auto enable log injection for structured loggers #13570

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

Merged
merged 1 commit into from
Jun 15, 2025

Conversation

mabdinur
Copy link
Contributor

@mabdinur mabdinur commented Jun 2, 2025

  • Uses the _logs_injection global configuration to determine whether trace info should be injected into a log message. This simplifies remote configuration logic, and provides one central flag that can modify all logging integrations. With this approach we no longer need to unpatch logging integrations.
  • Ensures structured logging can be enabled/disabled via remote config. Currently remote config only supports configuring the builtin python logging library.
  • Updates loguru, logbook, structlog, and logging tests to validate the expected behavior of setting DD_LOGS_ENABLED to True, False, and leaving this config unset.

Next steps

  • There is a ton of duplication between ddtrace logging integrations and tests. We should clean this up in a follow up PR.
  • ddtrace logging support was designed to support python2 and python3. Since we only support ddtrace>3.7 we can revamp our logging intstrumentation.

Checklist

  • 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
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • 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 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

@mabdinur mabdinur requested review from a team as code owners June 2, 2025 21:21
Copy link
Contributor

github-actions bot commented Jun 2, 2025

CODEOWNERS have been resolved as:

releasenotes/notes/enable-structored-logging-by-default-b6ad86e55b3582be.yaml  @DataDog/apm-python
.github/workflows/system-tests.yml                                      @DataDog/python-guild @DataDog/apm-core-python
ddtrace/_logger.py                                                      @DataDog/apm-core-python
ddtrace/_monkey.py                                                      @DataDog/apm-core-python
ddtrace/_trace/product.py                                               @DataDog/apm-sdk-api-python
ddtrace/bootstrap/sitecustomize.py                                      @DataDog/apm-core-python
ddtrace/contrib/_logbook.py                                             @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/_loguru.py                                              @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/_structlog.py                                           @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/logbook/patch.py                               @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/logging/patch.py                               @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/loguru/patch.py                                @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/structlog/patch.py                             @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/settings/_config.py                                             @DataDog/apm-core-python
docs/configuration.rst                                                  @DataDog/python-guild
tests/commands/test_runner.py                                           @DataDog/apm-core-python
tests/contrib/logbook/test_logbook_logging.py                           @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/logging/test_logging.py                                   @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/loguru/test_loguru_logging.py                             @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/structlog/test_structlog_logging.py                       @DataDog/apm-core-python @DataDog/apm-idm-python
tests/integration/test_debug.py                                         @DataDog/apm-core-python
tests/integration/test_settings.py                                      @DataDog/apm-core-python
tests/internal/test_settings.py                                         @DataDog/apm-core-python
tests/telemetry/test_writer.py                                          @DataDog/apm-python
tests/tracer/test_settings.py                                           @DataDog/apm-sdk-api-python
tests/utils.py                                                          @DataDog/python-guild

@pr-commenter
Copy link

pr-commenter bot commented Jun 2, 2025

Benchmarks

Benchmark execution time: 2025-06-15 04:09:32

Comparing candidate commit 15b3ffb in PR branch munir/struct-log-by-default with baseline commit 98e661a in branch main.

Found 0 performance improvements and 1 performance regressions! Performance is the same for 560 metrics, 3 unstable metrics.

scenario:iastaspectsospath-ospathsplit_aspect

  • 🟥 execution_time [+814.958ns; +899.586ns] or [+16.855%; +18.605%]

@mabdinur mabdinur force-pushed the munir/struct-log-by-default branch from 8fd7ca9 to e52877c Compare June 2, 2025 23:20
@mabdinur mabdinur changed the title feat(logging): enable support for structored logging feat(logging): auto enable log injection for structured loggers Jun 2, 2025
Copy link
Contributor

@bm1549 bm1549 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this up so quickly! I'm not too familiar with the code itself, but I had some questions stemming from some of the comments as compared to the test cases 🙏

Copy link
Member

@brettlangdon brettlangdon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question, but otherwise lgtm

@mabdinur
Copy link
Contributor Author

mabdinur commented Jun 5, 2025

@mabdinur mabdinur requested a review from wantsui June 6, 2025 14:29
@mabdinur mabdinur force-pushed the munir/struct-log-by-default branch 4 times, most recently from 8441141 to f0e5806 Compare June 9, 2025 14:41
@mabdinur mabdinur enabled auto-merge (squash) June 10, 2025 02:11
@DataDog DataDog deleted a comment from github-actions bot Jun 10, 2025
Copy link
Contributor

github-actions bot commented Jun 10, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 274 ± 3 ms.

The average import time from base is: 275 ± 2 ms.

The import time difference between this PR and base is: -0.7 ± 0.1 ms.

Import time breakdown

The following import paths have appeared:

ddtrace.auto 0.364 ms (0.13%)
ddtrace.bootstrap.sitecustomize 0.364 ms (0.13%)
ddtrace.contrib.internal.logging.constants 0.364 ms (0.13%)

The following import paths have grown:

ddtrace.auto 1.707 ms (0.62%)
ddtrace.bootstrap.sitecustomize 1.556 ms (0.57%)
ddtrace.appsec._common_module_patches 0.662 ms (0.24%)
ddtrace.appsec._asm_request_context 0.662 ms (0.24%)
ddtrace.appsec._utils 0.662 ms (0.24%)
ddtrace._trace.trace_handlers 0.044 ms (0.02%)
ddtrace._trace._inferred_proxy 0.044 ms (0.02%)
ddtrace 0.151 ms (0.06%)
ddtrace._logger 0.151 ms (0.06%)
ddtrace.internal.telemetry 0.120 ms (0.04%)
ddtrace.internal.utils.formats 0.120 ms (0.04%)
ddtrace.internal.constants 0.120 ms (0.04%)
ddtrace.constants 0.120 ms (0.04%)

The following import paths have shrunk:

ddtrace.auto 2.932 ms (1.07%)
ddtrace.bootstrap.sitecustomize 2.123 ms (0.77%)
ddtrace.bootstrap.preload 1.326 ms (0.48%)
ddtrace.internal.remoteconfig.client 0.636 ms (0.23%)
ddtrace.appsec._common_module_patches 0.739 ms (0.27%)
ddtrace.appsec._metrics 0.664 ms (0.24%)
ddtrace.appsec._asm_request_context 0.075 ms (0.03%)
ddtrace._trace.trace_handlers 0.031 ms (0.01%)
ddtrace.contrib.internal.subprocess.constants 0.028 ms (0.01%)
ddtrace 0.809 ms (0.30%)
ddtrace._logger 0.131 ms (0.05%)
ddtrace.internal.telemetry 0.131 ms (0.05%)
ddtrace.internal.utils.formats 0.131 ms (0.05%)
ddtrace.internal.compat 0.131 ms (0.05%)
ddtrace.internal._unpatched 0.032 ms (0.01%)
json 0.032 ms (0.01%)
json.decoder 0.032 ms (0.01%)
re 0.032 ms (0.01%)
enum 0.032 ms (0.01%)
types 0.032 ms (0.01%)

@mabdinur mabdinur force-pushed the munir/struct-log-by-default branch 2 times, most recently from a2e3eec to e1060b7 Compare June 10, 2025 19:41
@mabdinur mabdinur force-pushed the munir/struct-log-by-default branch 2 times, most recently from a95b37d to 6601361 Compare June 12, 2025 22:53
@mabdinur mabdinur force-pushed the munir/struct-log-by-default branch from e5682a2 to ea188af Compare June 12, 2025 23:37
@mabdinur mabdinur force-pushed the munir/struct-log-by-default branch from 3023ee8 to 15b3ffb Compare June 15, 2025 03:19
@mabdinur mabdinur merged commit 68152a2 into main Jun 15, 2025
822 checks passed
@mabdinur mabdinur deleted the munir/struct-log-by-default branch June 15, 2025 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants