Skip to content

refactor(internal): move _threads next to the native extension - #22137

Open
gnufede wants to merge 2 commits into
mainfrom
gnufede/package-split-native-threads
Open

gnufede wants to merge 2 commits into
mainfrom
gnufede/package-split-native-threads

Conversation

@gnufede

@gnufede gnufede commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Part of replacing #22018 with smaller PRs on main. Addresses
#22018 (comment).

What

  • Move ddtrace/internal/_threads.cpp / _threads.pyi to ddtrace/internal/native/, the package that
    becomes ddtrace-native. _threads is compiled code with no Python ddtrace imports.
  • The module named itself ddtrace.internal._threads in two places. Both now come from the name it is imported
    under: the module-state lookup uses the module's __name__, and the PeriodicThread type name is built from
    it when the module first runs. No compile-time macro, and the same source works as
    _ddtrace_native._threads. PeriodicThread.__module__ follows the module; nothing pickles these threads.
  • In-tree importers (threads.py, periodic.py, the trace processor, profiling's _threading.pyx, tests,
    docs) use the new path.
  • ddtrace/internal/_threads.py stays as a temporary alias of the new module, so the serverless test
    tests/contrib/aws_lambda/test_runtime_id.py is not touched here. A follow-up reviewed by its owners moves
    that import and removes the alias.

Tests

  • tests/internal/test_threads.py: the binary loads under a bare name in an interpreter that never imports
    ddtrace, as ddtrace-native will.
  • tests/internal/test_periodic.py: one assert reordered (mypy reported it unreachable once the file was touched).

🤖 Generated with Claude Code

_threads is compiled code with no Python ddtrace imports, so it ships in
ddtrace-native, whose package ddtrace.internal.native becomes. Moving it
there now makes the dd-trace-py layout match the split.

The module named itself ddtrace.internal._threads in two places. Both now
come from the name it is imported under: the module-state lookup uses the
module's __name__, and the PeriodicThread type name is built from it when
the module first runs. That needs no compile-time setting, so there is no
string macro to quote through MSVC flags, and the same source works as
_ddtrace_native._threads. PeriodicThread.__module__ follows the module;
nothing pickles these threads.

ddtrace.internal._threads stays as an alias of the new module for now:
the serverless tests still import it from the old path, and a follow-up
reviewed by their owners moves those imports and removes the alias.

The new tests load the binary under a bare name in an interpreter that
never imports ddtrace, as the ddtrace-native package will.

The commit hooks check every touched file, so this also drops an unused
import from _threading.pyx and reorders one assert in test_periodic.py
that mypy reported as unreachable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gnufede gnufede added the changelog/no-changelog A changelog entry is not required for this PR. label Nov 26, 2026
@cit-pr-commenter-54b7da

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.

.agents/skills/analyze-crash/SKILL.md                                   @DataDog/apm-python-guild
benchmarks/suitespec.yml                                                @DataDog/apm-core-python
ddtrace/_trace/processor/__init__.py                                    @DataDog/apm-sdk-capabilities
ddtrace/internal/_threads.py                                            @DataDog/apm-core-python
ddtrace/internal/native/_threads.cpp                                    @DataDog/apm-core-python
ddtrace/internal/native/_threads.pyi                                    @DataDog/apm-core-python
ddtrace/internal/periodic.py                                            @DataDog/apm-core-python
ddtrace/internal/threads.py                                             @DataDog/apm-core-python
ddtrace/profiling/_threading.pyx                                        @DataDog/profiling-python
docs/contributing-architecture.rst                                      @DataDog/apm-python-guild
setup.py                                                                @DataDog/apm-python-guild
tests/internal/test_periodic.py                                         @DataDog/apm-core-python
tests/internal/test_periodic_stress.py                                  @DataDog/apm-core-python
tests/internal/test_threads.py                                          @DataDog/apm-core-python
tests/suitespec.yml                                                     @DataDog/apm-python-guild
tests/tracer/test_writer.py                                             @DataDog/apm-sdk-capabilities

@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

@pr-commenter

pr-commenter Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-26 17:52:03

Comparing candidate commit d9f72f5 in PR branch gnufede/package-split-native-threads with baseline commit 257b955 in branch main.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 612 metrics, 10 unstable metrics, 1 flaky benchmarks without significant changes.

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

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:coreapiscenario-context_with_data_listeners

  • unstable execution_time [-756.435ns; +717.764ns] or [-7.082%; +6.719%]

scenario:coreapiscenario-core_dispatch_1_listener

  • unstable execution_time [-39.500ns; +38.909ns] or [-6.187%; +6.094%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-1898.667ns; +1917.798ns] or [-9.511%; +9.607%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-1910.162ns; +1765.851ns] or [-9.869%; +9.123%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-368.540ns; +384.625ns] or [-8.676%; +9.054%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-225.845ns; +229.757ns] or [-8.491%; +8.638%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-99.024ns; +80.200ns] or [-7.152%; +5.792%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-4703.571ns; +4549.485ns] or [-9.715%; +9.396%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-1054.671ns; +823.422ns] or [-10.271%; +8.019%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • unstable execution_time [-46.719µs; +64.559µs] or [-8.077%; +11.161%]

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

Known flaky benchmarks without significant changes:

  • scenario:packagespackageforrootmodulemapping-cache_off

@gnufede
gnufede marked this pull request as ready for review November 26, 2026 17:24
@gnufede
gnufede requested review from a team as code owners November 26, 2026 17:24
@gnufede
gnufede requested review from juanjux, khanayan123 and taegyunkim and removed request for a team November 26, 2026 17:24
@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-26T17:27:24.327904Z d9f72f5 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-datadog-prod-us1 datadog-datadog-prod-us1 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

The completed static review found no actionable defect in native initialization, compatibility imports, or build registration. Runtime loading and thread behavior remain unverified by execution.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit d9f72f5

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