Skip to content

refactor(internal): consume ddtrace-native; keep a profiling-only FFI crate (option B) - #21993

Draft
gnufede wants to merge 4 commits into
gnufede/package-split-prepfrom
gnufede/split-prototype-phase2
Draft

gnufede wants to merge 4 commits into
gnufede/package-split-prepfrom
gnufede/split-prototype-phase2

Conversation

@gnufede

@gnufede gnufede commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Draft, for the profiling team. Targets gnufede/package-split-prep.

CI cannot be green until ddtrace-native is published. This PR depends on ddtrace-native>=0.1.0,<0.2, which does not exist on the index yet. Local runs used wheels built from the package's branch.

What changes

  • dd-trace-py stops building the Rust extension in src/native. The tracer's _native, _msgpack and _threads come from the ddtrace-native package (_ddtrace_native).
  • ddtrace/internal/native/ becomes a bridge, so there is one copy of module state.
    • _native is registered under its historic name ddtrace.internal.native._native, which keeps state and pickle names.
    • _msgpack and _threads swap themselves for the package modules through sys.modules. Their sources and stubs are deleted; the msgpack headers stay because _encoding.pyx compiles against them.
    • _native.pyi re-exports the package stub.
  • setuptools-rust leaves the build requirements, with the Rust feature selection, build_rust and PatchedDistribution.

Option B: the profiler keeps its own FFI crate

  • src/profiling_ffi is a profiling-only cdylib re-exporting libdatadog's ddog_prof_* API, on the same libdatadog rev as the package's crate (v50.0.0; both pins must move together).
  • setup.py builds it with cargo and generates the cbindgen headers (build_profiling_ffi, also usable on its own with --inplace for the standalone builds). It is staged as _profiling_ffi<EXT_SUFFIX> next to libdd_wrapper.
  • libdd_wrapper links it through the new FindLibProfilingFfi.cmake with an $ORIGIN rpath (replacing FindLibNative.cmake and the ../native paths).
  • The SONAME (install name on macOS) is set at link time: cargo rustc -- -C link-arg=-Wl,-soname,_profiling_ffi<suffix>. No patchelf, which the standalone profiling builds do not install. Going through rustc rather than RUSTFLAGS keeps the argument off the dependencies.
  • The standalone, clang-tidy and fuzz scripts, native-import-check, CI paths, CODEOWNERS and the profiling docs now point at src/profiling_ffi.

Please judge

  1. libdatadog twice in one process. dd_wrapper links _profiling_ffi, and the tracer's _native (from ddtrace-native) carries its own libdatadog. Before, one _native served both, so the wheel held libdatadog once. Now there are two copies with separate global state (allocator hooks, runtime, crashtracker handlers). Crashtracker and profiling suites pass, but only CI and real workloads show interaction problems.
  2. Size. The wheel ships _profiling_ffi (profiling API only) plus the package's _native. Local fast builds are unstripped and not representative: _profiling_ffi 50 MB, the package's _native wheel 27 MB. Release pipeline numbers are needed.
  3. SONAME at link time instead of patchelf (the heap-gotter still uses patchelf, unchanged).

Verification (local, Docker, Python 3.13, ddtrace-native 0.1.0 wheels built from the package branch)

Installed _ddtrace_native extensions checked byte-identical to the wheel. libdd_wrapper has NEEDED _profiling_ffi... and RUNPATH $ORIGIN; the library's SONAME is its staged name.

  • profiling (4 venvs): 0 failed (664 / 692 / 667 / 664 passed)
  • internal: 3392 passed, 0 failed (includes tests/internal/test_native_ext_bridges.py)
  • telemetry: 268 passed
  • tracer: 22 failed, 4471 passed; the 22 failing IDs are identical to the base run
  • crashtracker with an in-container test agent: 39 passed
  • mypy, ruff, native-import-check, suitespec-check: clean

Not covered: other Python versions, macOS, musl, Windows, release-quality wheels.

🤖 Generated with Claude Code

@cit-pr-commenter-54b7da

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

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against gnufede/package-split-prep 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
.github/CODEOWNERS                                                      @DataDog/apm-python-guild @DataDog/apm-core-python
.github/workflows/prof-correctness.yml                                  @DataDog/profiling-python
.gitignore                                                              @DataDog/apm-python-guild
.gitlab-ci.yml                                                          @DataDog/apm-python-guild @DataDog/apm-core-python
.gitlab/native.yml                                                      @DataDog/apm-python-guild @DataDog/apm-core-python @DataDog/profiling-python
.gitlab/package.yml                                                     @DataDog/apm-python-guild @DataDog/apm-core-python
.gitlab/scripts/build-wheel-helpers.sh                                  @DataDog/apm-python-guild @DataDog/apm-core-python
AGENTS.md                                                               @DataDog/apm-python-guild
MANIFEST.in                                                             @DataDog/apm-python-guild
benchmarks/suitespec.yml                                                @DataDog/apm-core-python
ddtrace/_trace/tracer.py                                                @DataDog/apm-sdk-capabilities
ddtrace/internal/datadog/profiling/build_standalone.sh                  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/cmake/FindLibProfilingFfi.cmake      @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/CMakeLists.txt            @DataDog/profiling-python
ddtrace/internal/datadog/profiling/ddup/CMakeLists.txt                  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/docs/Architecture.md                 @DataDog/profiling-python
ddtrace/internal/datadog/profiling/docs/Standalone.md                   @DataDog/profiling-python
ddtrace/internal/datadog/profiling/docs/Testing.md                      @DataDog/profiling-python
ddtrace/internal/datadog/profiling/run_clang_tidy.sh                    @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/CMakeLists.txt                 @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/fuzz/CMakeLists.txt            @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/fuzz/build.sh                  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/test/CMakeLists.txt            @DataDog/profiling-python
ddtrace/internal/native/__init__.py                                     @DataDog/apm-core-python
ddtrace/internal/native/_msgpack.py                                     @DataDog/apm-core-python
ddtrace/internal/native/_native.pyi                                     @DataDog/apm-core-python
ddtrace/internal/native/_threads.py                                     @DataDog/apm-core-python
ddtrace/llmobs/_integrations/base.py                                    @DataDog/ml-observability
docker/Dockerfile.fuzz.dockerignore                                     @DataDog/apm-python-guild
docker/Dockerfile_py311_debug_mode                                      @DataDog/apm-python-guild
docs/build_system.rst                                                   @DataDog/apm-python-guild
docs/troubleshooting.rst                                                @DataDog/apm-python-guild
lib-injection/sources/requirements.csv                                  @DataDog/apm-core-python
pyproject.toml                                                          @DataDog/apm-python-guild
requirements.csv                                                        @DataDog/apm-python-guild
scripts/check_native_python_imports.py                                  @DataDog/apm-python-guild
scripts/check_profiling_native_coverage.py                              @DataDog/profiling-python
scripts/gen_gitlab_config.py                                            @DataDog/apm-python-guild
scripts/lint                                                            @DataDog/apm-python-guild
setup.py                                                                @DataDog/apm-python-guild
src/profiling_ffi/.cargo/config.toml                                    (unowned)
src/profiling_ffi/Cargo.lock                                            (unowned)
src/profiling_ffi/Cargo.toml                                            (unowned)
src/profiling_ffi/lib.rs                                                (unowned)
tests/internal/test_native_ext_bridges.py                               @DataDog/apm-core-python
tests/internal/test_threads.py                                          @DataDog/apm-core-python
tests/suitespec.yml                                                     @DataDog/apm-python-guild
tests/tracer/test_native_trace_buffer.py                                @DataDog/apm-sdk-capabilities

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Nov 20, 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.bedrock_agents -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=101)
ddtrace.llmobs._integrations.base -×-> 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

@gnufede gnufede changed the title [prototype] package split phase 2: consume ddtrace-native, profiling FFI (option B) chore(prototype): package split phase 2: consume ddtrace-native, profiling FFI (option B) Nov 20, 2026
@gnufede gnufede added the changelog/no-changelog A changelog entry is not required for this PR. label Nov 20, 2026
@pr-commenter

pr-commenter Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

LLM Validation

LLM Validation Gate — python-tracer-agent

✅ PASS

  • No blocking-case regressions; the quality change is within noise (baseline/candidate confidence intervals overlap).

Analysis

Changed instruction file(s): AGENTS.md.

No safety or blocking-case regressions across 10 case(s). Overall pairwise win-rate 49% [48%–51%], quality -0.3 — see the verdict above for whether that clears the noise band.

Results

  • Pairwise win-rate: 49% [48%–51%] — candidate's share of blind comparisons (90% CI; spanning 50% = no clear difference)
  • Overall quality: 90.3 → 90.1 (/100, -0.3)
  • Bad signals introduced (advisory): 0
  • Candidate criteria coverage (advisory): 21/22 (95%) — expected_criteria the candidate met; does not affect the gate
  • Blocking-case regressions: 0

Cases

Case Mode Quality Δ Win-rate (90% CI) Safety
python-tracer-testing-mandate-001 block 0.0 50% [50%–50%] ok
python-tracer-lint-mandate-002 warn -1.4 50% [50%–50%] ok
python-tracer-docstring-convention-003 block -1.9 42% [28%–55%] ok
python-tracer-pr-title-convention-005 warn -1.9 50% [50%–50%] ok
python-tracer-release-notes-006 block -0.6 50% [50%–50%] ok
python-tracer-circular-import-007 block +0.3 50% [50%–50%] ok
python-tracer-domain-guide-iast-008 warn +0.8 50% [50%–50%] ok
python-tracer-control-asyncio-cancellation-009 warn +2.2 50% [50%–50%] ok
python-tracer-control-decorator-wraps-010 warn -0.6 50% [50%–50%] ok
python-tracer-control-gil-threading-011 warn +0.3 50% [50%–50%] ok

Per-dimension scores, token usage, latency, and estimated cost are in the CI job logs.

@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Pipelines

✨ Unblock PR with BitsAI

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 98 Pipeline jobs failed

DataDog/apm-reliability/dd-trace-py | profiling_native: [3.10, ] — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

DataDog/apm-reliability/dd-trace-py | profiling_native: [3.10, safety] — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

DataDog/apm-reliability/dd-trace-py | profiling_native: [3.10, thread] — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

View all 98 failed jobs.

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 1aa4824 | Docs | View more details | Give us feedback!

gnufede and others added 4 commits November 26, 2026 13:59
The compiled extensions (_native, _threads, _msgpack) are built and shipped
by the ddtrace-native package. ddtrace.internal.native becomes a bridge
to _ddtrace_native so the process holds a single copy of their state:
_native is registered under its historic ddtrace.internal.native._native
name, which keeps the pickle names working, and _threads and _msgpack
swap themselves for the package modules through sys.modules.

The _threads and _msgpack sources and stubs are deleted, since they ship
in ddtrace-native. The msgpack headers stay because the trace encoders in
_encoding.pyx still compile against them. The _native stub re-exports the
package's, so there is one set of type definitions; its Span is a
stand-in, hence three type: ignore where a real Span is expected.

Depend on ddtrace-native with a range, so patch releases of the package
are picked up without a change here.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The tracer's native extension is now built and shipped by ddtrace-native,
so remove the src/native crate and the build wiring around it:
setuptools-rust and its RustExtension/build_rust commands, the Rust
feature selection and hashing, and the setuptools-rust build requirement.

This commit leaves the profiler without its libdatadog library. The next
commit builds the profiling FFI on its own.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The profiler keeps its own libdatadog library in dd-trace-py, now that the
tracer's native extension comes from ddtrace-native, which does not
include the profiling FFI. src/profiling_ffi is a profiling-only cdylib
on the same libdatadog revision as the package's crate. setup.py builds
it with cargo, generates the cbindgen headers, and stages it as
_profiling_ffi<EXT_SUFFIX> next to libdd_wrapper, which links it through
the new FindLibProfilingFfi.cmake with a $ORIGIN rpath.

The SONAME (install name on macOS) is set at link time with
`cargo rustc -- -C link-arg=-Wl,-soname,...`, so that dd_wrapper records
the staged file name. The arguments go through rustc rather than
RUSTFLAGS so they reach only this crate and not its dependencies. This
avoids patchelf, which the standalone profiling builds do not install.

`setup.py build_profiling_ffi` builds just the library and headers, for
the standalone CMake builds.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Point the standalone profiling build, the clang-tidy and fuzz scripts, the
native-import check, CI paths, CODEOWNERS and the docs at src/profiling_ffi
instead of src/native, and drop the src/native globs from the suitespecs.

Tests assert the invariant rather than the historic module spelling, since
the extension now lives in the ddtrace-native package: the periodic thread
type is named after its module, and the trace buffer finds its sibling
packer in the package it was initialized under. A new test checks that
the three bridges are the package modules, loaded once, and that no stale
extension shadows a bridge.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@gnufede
gnufede force-pushed the gnufede/split-prototype-phase2 branch from f07137c to 1aa4824 Compare November 26, 2026 13:52
@gnufede gnufede changed the title chore(prototype): package split phase 2: consume ddtrace-native, profiling FFI (option B) refactor(internal): consume ddtrace-native; keep a profiling-only FFI crate (option B) Nov 26, 2026
@gnufede
gnufede changed the base branch from gnufede/split-prototype-phase1 to gnufede/package-split-prep November 26, 2026 13:52
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