Skip to content

refactor(testing): decouple test optimization from the tracer and contrib - #22129

Open
gnufede wants to merge 6 commits into
mainfrom
gnufede/package-split-testing
Open

gnufede wants to merge 6 commits into
mainfrom
gnufede/package-split-testing

Conversation

@gnufede

@gnufede gnufede commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Part of replacing #22018 with smaller PRs on main.

  • Route the remaining tracer touchpoints of ddtrace.testing through tracer_api; stdlib logging in the xdist helpers.
  • Move the ci_visibility.utils source helpers into ddtrace/testing/internal/source_info.py.
  • Move the coverage.py logic into ddtrace/testing/internal/coverage_py/; contrib.internal.coverage stays a thin integration, COVERAGE_RUN is read via env.get.
  • tests/testing/internal/test_tracer_isolation.py asserts importing the pytest plugin loads no ddtrace._trace, ddtrace.trace or ddtrace.contrib.

🤖 Generated with Claude Code

gnufede and others added 5 commits November 26, 2026 16:42
The pytest plugin and the BDD plugin imported ddtrace, and the logging
and selenium contrib patches, directly. Move those touchpoints behind
lazy helpers in tracer_api/context.py (get_tracer, enable_log_correlation,
enable_selenium_integration), next to the existing trace filter and
patch-all helpers.

No behavior change. It makes tracer_api the single seam between test
optimization and the tracer, so the plugin can later be extracted into
its own package with the tracer integration injected by dd-trace-py.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
(cherry picked from commit c4a3ca2)
Every other ddtrace.testing module logs through logging.getLogger; the
xdist helpers were the exception, importing ddtrace.internal.logger.
Its extras (product rate limiting, ddtrace.* level lookup) do not apply
to this module's records, so the stdlib logger is equivalent here and
drops one more ddtrace internal from the plugin.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
(cherry picked from commit 9010a00)
With tracer integration off, a pytest session must not make
ddtrace.testing import the tracer, _monkey or the logging/selenium
contrib. sys.modules cannot show this in-tree, since importing
ddtrace.testing runs ddtrace/__init__, so the test records imports by
importer. Positive controls cover tracer integration on and
--ddtrace-patch-all.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
(cherry picked from commit 0e2d7fb)
The pytest plugin imported get_relative_path and
get_source_lines_for_test_method from ci_visibility.utils, which imports
ddtrace, the coverage contrib and the legacy CI visibility package, so
that one import loaded the tracer and 32 ci_visibility modules.

Both helpers only use the standard library and utils.paths. They move,
with the line-counting helper they use, into
ddtrace.testing.internal.source_info, which moves with test
optimization. The legacy CI visibility code imports them from there, and
their tests move to tests/testing.

The plugin still loads the tracer through ddtrace.contrib.internal.coverage,
whose home is not decided yet. The new tests check that source_info
loads no tracer modules and that the plugin no longer loads
ci_visibility.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 163bdb4)
(cherry picked from commit 618a20f)
Every `ddtrace.contrib.*` import runs ddtrace/contrib/__init__.py, which
imports the trace handlers and subscribers, so the pytest plugin loaded
the tracer just to reach the coverage.py integration's helpers. All of
that code is test optimization's: the pytest plugin, the xdist plugin,
tracer_api.coverage, ci_visibility and the unittest integration are its
only users. It is not the line coverage collector (ddtrace-coverage),
which knows nothing about coverage.py.

The coverage instance state, the start/stop/report functions, the pytest-cov
and `coverage run` detection, the LCOV conversion and their constants move
to ddtrace/testing/internal/coverage_py/, with git history, and the users
import them from there. ddtrace/contrib/internal/coverage/patch.py stays as
the thin integration that _monkey and the integration registry expect:
patch(), unpatch(), get_version() and _supported_versions(), delegating to
the moved patch/unpatch. patch(coverage=True), the docs in its __init__ and
coverage.manifest.yaml are unchanged.

The COVERAGE_RUN lookup used the tracer's configuration getter, which
imports the tracer settings. It now reads the same variable from the env
accessor with the same boolean parsing. As the getter also reported it to
configuration telemetry and read stable config, that report is gone.

test_tracer_isolation now asserts that importing the plugin loads no
ddtrace._trace, ddtrace.trace or ddtrace.contrib module. The mypy sections
follow the modules, and the tests that patched the old paths use the new
ones.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
(cherry picked from commit cd7df69de0142ff67e63733d99ff2ad30d399053)
(cherry picked from commit 21dc684)
@gnufede gnufede added the changelog/no-changelog A changelog entry is not required for this PR. label Nov 26, 2026
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against main using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

.github/CODEOWNERS                                                      @DataDog/apm-python-guild @DataDog/apm-core-python
ddtrace/contrib/internal/coverage/patch.py                              @DataDog/ci-app-libraries
ddtrace/contrib/internal/unittest/patch.py                              @DataDog/ci-app-libraries
ddtrace/internal/ci_visibility/api/_base.py                             @DataDog/ci-app-libraries
ddtrace/internal/ci_visibility/api/_coverage_data.py                    @DataDog/ci-app-libraries
ddtrace/internal/ci_visibility/api/_module.py                           @DataDog/ci-app-libraries
ddtrace/internal/ci_visibility/utils.py                                 @DataDog/ci-app-libraries
ddtrace/testing/internal/coverage_py/__init__.py                        @DataDog/ci-app-libraries
ddtrace/testing/internal/coverage_py/constants.py                       @DataDog/ci-app-libraries
ddtrace/testing/internal/coverage_py/data.py                            @DataDog/ci-app-libraries
ddtrace/testing/internal/coverage_py/lcov.py                            @DataDog/ci-app-libraries
ddtrace/testing/internal/coverage_py/patch.py                           @DataDog/ci-app-libraries
ddtrace/testing/internal/coverage_py/utils.py                           @DataDog/ci-app-libraries
ddtrace/testing/internal/pytest/_xdist.py                               @DataDog/ci-app-libraries
ddtrace/testing/internal/pytest/bdd.py                                  @DataDog/ci-app-libraries
ddtrace/testing/internal/pytest/plugin.py                               @DataDog/ci-app-libraries
ddtrace/testing/internal/pytest/xdist.py                                @DataDog/ci-app-libraries
ddtrace/testing/internal/source_info.py                                 @DataDog/ci-app-libraries
ddtrace/testing/internal/tracer_api/context.py                          @DataDog/ci-app-libraries
ddtrace/testing/internal/tracer_api/coverage.py                         @DataDog/ci-app-libraries
mypy.ini                                                                @DataDog/apm-python-guild @DataDog/apm-core-python
tests/ci_visibility/test_utils.py                                       @DataDog/ci-app-libraries
tests/contrib/internal/coverage/test_utils.py                           @DataDog/apm-python-guild @DataDog/apm-idm-python
tests/testing/internal/coverage/conftest.py                             @DataDog/ci-app-libraries
tests/testing/internal/coverage/test_patch.py                           @DataDog/ci-app-libraries
tests/testing/internal/pytest/test_pytest_cov.py                        @DataDog/ci-app-libraries
tests/testing/internal/pytest/test_pytest_coverage_report_upload.py     @DataDog/ci-app-libraries
tests/testing/internal/test_source_info.py                              @DataDog/ci-app-libraries
tests/testing/internal/test_tracer_isolation.py                         @DataDog/ci-app-libraries

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 22 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 22 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=103)
ddtrace.llmobs._integrations.base -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=101)
ddtrace.llmobs._integrations.bedrock_agents -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=101)
ddtrace.internal.span_bus -×-> ddtrace._trace.span  (internal-core -> product:tracing, score=35)
ddtrace.llmobs._llmobs -×-> ddtrace._trace.span  (product:llmobs -> product:tracing, score=33)

To see all violations, download the layers-base.json and layers-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/layers.py compare layers-base.json layers-pr.json

…-libraries

ddtrace/contrib/internal/coverage is already owned by ci-app-libraries,
and its logic now lives in ddtrace.testing, but its tests fell under the
generic tests/contrib/ rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gnufede
gnufede marked this pull request as ready for review November 26, 2026 16:13
@gnufede
gnufede requested review from a team as code owners November 26, 2026 16:13
@gnufede
gnufede requested review from danielsn, juanjux and quinna-h and removed request for a team November 26, 2026 16:13
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T16:20:25.758853Z b9dee5b Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-prod-us1-4 datadog-prod-us1-4 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

Static inspection found that the caller migrations preserve shared coverage state and existing tracer activation behavior. No actionable regression was identified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit b9dee5b

@pr-commenter

pr-commenter Bot commented Nov 26, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-26 16:24:34

Comparing candidate commit b9dee5b in PR branch gnufede/package-split-testing with baseline commit 56a4a53 in branch main.

📊 Benchmarking dashboard

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

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/no-changelog A changelog entry is not required for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant