Skip to content
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

fix(ci_visibility): set up Test Optimization logging before pytest log capture [backport 3.1] #13063

Open
wants to merge 1 commit into
base: 3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ddtrace/contrib/internal/pytest/_plugin_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,18 @@ def _disable_ci_visibility():
log.debug("encountered error during disable_ci_visibility", exc_info=True)


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_load_initial_conftests(early_config, parser, args):
"""Perform early initialization of the Test Optimization plugin.

This has to happen early enough that `sys.stderr` has not been redirected by pytest, so that logging is configured
properly. Setting the hook with `tryfirst=True` and `hookwrapper=True` achieves that.
"""
_pytest_load_initial_conftests_pre_yield(early_config, parser, args)
yield


def _pytest_load_initial_conftests_pre_yield(early_config, parser, args):
"""Performs the bare-minimum to determine whether or ModuleCodeCollector should be enabled

ModuleCodeCollector has a tangible impact on the time it takes to load modules, so it should only be installed if
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/internal/ci_visibility/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def take_over_logger_stream_handler(remove_ddtrace_stream_handlers=True):
if remove_ddtrace_stream_handlers:
log.debug("CIVisibility removing DDTrace logger handler")
ddtrace_logger = logging.getLogger("ddtrace")
for handler in ddtrace_logger.handlers:
for handler in list(ddtrace_logger.handlers):
ddtrace_logger.removeHandler(handler)
else:
log.warning("Keeping DDTrace logger handler, double logging is likely")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
CI Visibility: This fix resolves an issue where an ``I/O operation on closed file`` error was raised while printing
``ddtrace`` logs during a pytest session, due to a conflict with pytest's log capture.