Repository navigation
Conversation
The generic msgpack Packer and packb import nothing from ddtrace, but they lived in _encoding next to the trace encoders, which import the constants, the logger, span limits and agent settings and stay with the tracer. Moving the packer into its own Cython module, next to the Rust extension in ddtrace.internal.native, lets it ship verbatim with the ddtrace-native package, and lets the extension import it as a sibling instead of reaching into ddtrace.internal._encoding. The packer is unchanged. The msgpack headers move with it, and _encoding, whose trace encoders also compile against them, adds that directory to its include path. The v0.4 encoder, data streams, the CI visibility encoder and the testing tracer API import packb from the new module. The type parameters in test_subtype.py only satisfy mypy, which checks the files a commit touches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The native TraceBuffer imported ddtrace.internal._encoding on first use to pack meta_struct values. The extension is moving to a standalone package that must not import any ddtrace module. Now that the packer lives next to the extension, the extension imports it as a sibling: at init it records the package it was imported into (from its own module name) and imports <package>._msgpack. That resolves to ddtrace.internal.native in dd-trace-py and to _ddtrace_native in the ddtrace-native package, with no ddtrace path in the crate and no registration call from Python to keep in sync. TraceBuffer.__init__ does the import, so write() imports nothing and keeps working in sandboxes that block imports after startup. If the import fails, write() drops each meta_struct entry with a single warning, because it must never raise into user code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The native extension is moving to a standalone package that must not import any ddtrace module. The last reach-in, the packb import in span/wire.rs, now goes to the sibling _msgpack; this check keeps new ones from coming back. It scans src/native for py.import / PyModule::import calls on a ddtrace module, ignoring comments and plain string literals such as the pyclass module attribute and event names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codeowners resolved asResolved from the full PR diff against |
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 4 Pipeline jobs failed
ℹ️ InfoNo other issues found (see more)🧪 All tests passed Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: 597e3f6 | Docs | View more details | Give us feedback! |
Dependency direction analysis
|
BenchmarksBenchmark execution time: 2026-12-04 18:57:04 Comparing candidate commit 597e3f6 in PR branch Found 0 performance improvements and 1 performance regressions! Performance is the same for 618 metrics, 10 unstable metrics, 1 flaky benchmarks without significant changes.
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 02829510ae
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
More details
No reportable defect was identified in the msgpack extraction or sibling-import changes. Suite-selection changes and runtime/wheel-build behavior remain unvalidated.
🤖 Bits Code Review · Commit 0282951
ed258ee to
c0eaba5
Compare
There was a problem hiding this comment.
More details
The native buffer derives the sibling packer’s location from its own package and resolves it during construction. The completed static review found no actionable defect; native builds, runtime tests, and wheel contents remain unverified.
🤖 Bits Code Review · Commit c0eaba5
c0eaba5 to
63c5b93
Compare
There was a problem hiding this comment.
More details
The specialist found no actionable defects in the packer extraction, sibling imports, build configuration, or consumers. Serialization logic is preserved, but runtime behavior was not tested.
🤖 Bits Code Review · Commit 8ab8610
a6a2452 to
12b66b6
Compare
8ab8610 to
d470175
Compare
There was a problem hiding this comment.
More details
The packer extraction preserves serialization logic, and Rust resolves the packer relative to the extension’s package. Confidence is limited to static inspection; tests and benchmarks were not executed.
🤖 Bits Code Review · Commit d470175
There was a problem hiding this comment.
The PR extracts native._msgpack and moves struct-tag encoding into Python, removing Rust’s packer imports. One issue remains: Ray’s direct writes bypass encoding and silently lose struct tags, while the same bypass leaves meta-struct benchmarks measuring empty storage; no earlier findings needed resolution, and validation remains static.
🤖 Bits Code Review · Commit 2312e81
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2312e814af
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
More details
The PR extracts native._msgpack and removes Rust’s Python packer imports, with struct tags encoded at writer boundaries. This fixes the earlier Ray data loss and empty meta-struct benchmark workload; no findings remain open, with validation limited to static inspection.
🤖 Bits Code Review · Commit 6c3688e
Flagging ddtrace-named string literals was an indirect proxy: the real dependency is the Python import call itself. Banning pyo3's import APIs outright (suggested in review) is stricter -- it catches module names computed at runtime -- and smaller: no pyo3 `module = "..."` exemption and no literal-matching utilities are needed, replacing ~465 lines of rule, snapshot and tests with ~120. The one remaining import in src/native (wire.rs packb) is grandfathered with a suppression; the packer split (#22136) removes it. The literal suppressions in context_provider.rs and wire.rs are no longer needed and are reverted to main's version.
6c3688e to
e208e6e
Compare
There was a problem hiding this comment.
More details
Writer-time struct-tag encoding covers direct native-buffer writes and preserves edits made by trace processors. Runtime behavior and performance were not exercised.
🤖 Bits Code Review · Commit e208e6e
e208e6e to
0fef1ae
Compare
There was a problem hiding this comment.
More details
The native writer encodes live struct tags before serialization, covering direct writer callers despite the omission described in the PR text. The completed static review identified no reportable defect.
🤖 Bits Code Review · Commit 0fef1ae
3a28078 to
a92e6bc
Compare
There was a problem hiding this comment.
More details
The reviewed head encodes struct tags at writer and encoder boundaries, addressing the aggregator-bypass risk described in the PR text. Runtime behavior and wheel packaging remain unverified.
🤖 Bits Code Review · Commit a92e6bc
## Description Split out of #22136: fail CI if Rust code under `src/native` imports a Python module. - A new ast-grep rule `native-no-python-imports` (`.sg/rules/`) flags any `.import` or `::import` call in `src/native/**/*.rs` (four call patterns: `$RECV.import($$$ARGS)`, `$TYPE::import($$$ARGS)` and their turbofish forms). That covers the method form (`py.import(...)`, `PyOnceLock::import`, the deprecated `GILOnceCell` alias) and every associated-function spelling (`Python::import`, `pyo3::Python::import`, `<Python>::import`, `PyModule::import`, `pyo3::types::PyModule::import`, aliased types), with or without turbofish generics. pyo3 0.30 has no other import entry point (`import_bound` was removed in 0.23). Only calls match: a Rust module or field named `import` that is not called (`self.import.dispatch(...)`, `use crate::encoding::import`) is not flagged. - Why: the native extension is heading to a standalone package that must not depend on `ddtrace`. Python registers the callables native needs instead (e.g. `Context._register_tracestate_helpers`). - The ban is on the import call, not on the module name, so names built at runtime are caught too. String literals that only mention `ddtrace` (event names, log messages, pyo3 `module = "..."` class metadata) are not imports and are not flagged, so no exemptions are needed for them. - The only existing import, `py.import("ddtrace.internal._encoding")` in `src/native/span/wire.rs`, carries an inline `// ast-grep-ignore: native-no-python-imports` suppression. #22136 (stacked on this PR) removes that code path and the suppression with it, leaving `src/native` with no Python imports. - The check rides the existing ast-grep rules CI job: no new script, lint subcommand or CI wiring. `scripts/lint sg` now passes `--error=unused-suppression`, so a suppression whose rule stops firing fails CI and the suppression list can only shrink. ## Customer impact None: developer and CI tooling only. `changelog/no-changelog`. ## Testing - `scripts/lint sg` clean on the real tree (exit 0), with `--error=unused-suppression`. - `scripts/lint sg-test`: all 29 ast-grep test suites pass, including the new rule's test and snapshot (method and associated-function forms, fully qualified, `<Python>::import`, aliased and turbofish calls, a runtime-computed name, a multiline method chain; ddtrace-named literals, pyo3 `module =` metadata and Rust modules or fields named `import` stay valid). - Inline suppressions are not applied by `ast-grep test`, so the suppression path is covered by `scripts/lint sg` against the `wire.rs` site. - #22136 restacked on this branch: `scripts/lint sg` and `sg-test` pass at its head, which has no suppressions left under `src/native`. ## Risks Low. The rule is scoped to `src/native/**/*.rs`. A future legitimate import needs an explicit inline suppression with a justification, which reviewers see in the diff. ## Additional Notes This replaces the earlier version of this PR, which flagged string literals naming `ddtrace` modules (with shared `ddtrace-named-literal` / `pyo3-module-arg` utils and a pyo3 exemption). Banning the import call is stricter (it also catches computed names) and smaller: the PR went from +475/−3 to +172/−1, most of it the generated rule-test snapshot. Co-authored-by: federico.mon <federico.mon@datadoghq.com>
There was a problem hiding this comment.
More details
Writer-side struct-tag encoding covers direct-write callers, addressing the Ray omission described in the PR’s risk section. Runtime behavior and packaging of the extracted Cython module remain unverified.
🤖 Bits Code Review · Commit a92e6bc
a92e6bc to
206bc17
Compare
There was a problem hiding this comment.
More details
The native wire-encoder comparison remains incomplete because the exact base-version wire.rs blob was unavailable. Runtime behavior and performance were not exercised.
🤖 Bits Code Review · Commit 206bc17
| if let Some(o) = &self._trace_id_py { | ||
| visit.call(o)?; | ||
| } | ||
| if let Some(d) = &self.meta_struct { |
There was a problem hiding this comment.
we don't have to traverse and visit the PyBackedStrings ?
580cc3d to
597e3f6
Compare
There was a problem hiding this comment.
More details
The pinned revision retains Rust-side meta_struct encoding; the bytes-only migration described in the PR text is outside this diff. Static review identified no actionable defect, while runtime and release-wheel behavior remain unverified.
🤖 Bits Code Review · Commit 597e3f6
Part of replacing #22018 with smaller PRs on
main. Addresses#22018 (comment) and
#22018 (comment).
What
The native span stops calling back into Python to encode
meta_struct. It stores bytes only, which is what theagent spec says
meta_structis (map<string, bytes>). The generic msgpack packer moves next to the nativeextension, so it can ship in
ddtrace-nativelater.Packer/packbmoves out ofddtrace/internal/_encoding.pyxinto anew Cython module,
ddtrace/internal/native/_msgpack.pyx, together with thepack.h/pack_template.h/sysdep.hheaders. The packer is unchanged._encodingstill compiles against the headers (extra includedir) and re-imports
packb. The v0.4 encoder, data streams, the CI Visibility encoder and the testing tracerAPI import it from the new module.
meta_struct.SpanDatastoresmeta_structas a Rust-owned list of(str, bytes).Its methods are
_set_encoded_struct_tag/_get_encoded_struct_tag/_has_encoded_struct_tags/_get_encoded_struct_tags, and the setter raisesTypeErrorfor anything that isn'tstr -> bytes.wire.rscopies the stored bytes into the wire map, so the Rust crate no longer imports any Python packer(the
ddtrace.internal._encodingimport and its drop logging are gone). The GC traversal visits themeta_structkeys like the other string fields; the values are Rust-owned bytes._encoding.pyxwrites thestored bytes as msgpack
bin, and the wire layout is unchanged.Span. The nested, mutable dicts that products read and edit (LLMObs,AppSec, IAST, AI Guard, stack traces, CI Visibility coverage) live in a lazy
_meta_structslot onSpan,which keeps
_set_struct_tag/_get_struct_tag/_remove_struct_tag/_has_meta_structs/_get_meta_structs. No product caller changes.has run (the LLMObs processor also removes its key), so nothing is encoded before the writer. The Cython
encoders pack
Span._packed_struct_tags()straight into the payload, so no span keeps a second, encoded copy.NativeTraceBuffer.write()stores the encoding in the native span withSpan._encode_struct_tags()justbefore handing it to Rust, because the native span is all the Rust encoder reads. Encoding in
write()rather than in
SpanAggregatorcovers every caller, including the ones that write spans themselves (Ray'sspan manager, benchmarks). An entry that fails to encode is dropped, the first drop in the process logs one
warning, and the application thread never sees the error.
SpanDatadeclares_encode_struct_tags()as a no-op thatSpanoverrides, so the writer calls it without acast, and spans with no struct tags return before any work.
native-no-python-importssuppression inwire.rs(from chore(native): fail on python imported from src/native #22161).src/nativehas noast-grep-ignoreleft.Tests
tests/tracer/test_encoders.py: the encoder reads the live dicts when the writer takes the span, and anentry that fails to encode is dropped with one warning while the rest of the span is still encoded.
tests/tracer/test_processors.py: the aggregator hands the writer the struct-tag state after the lasttrace processor (in-place edits, additions and removals).
tests/tracer/test_native_trace_buffer.py:write()encodes the struct tags, the wire output matches theCython encoder, and a native span with no encoded struct tags sends none.
tests/tracer/test_span_data.py: the native encoded round-trip and theTypeErrorfor non-bytes keys andvalues. The live struct-tag tests moved to
tests/tracer/test_span.py, with a check that a reference cyclethrough
Span._meta_structis collected.tests/vendor/msgpack/*import from the new module.the review round: 660 passed, 1 failed, same cause). All 22 are subprocess tests that assert empty
stderr and hit
localhost:8126with no agent running; none touchmeta_struct. Earlier runs: CI Visibility651 passed, AppSec 182 passed, LLMObs 494 passed / 8 failed (pytest-asyncio environment).
scripts/lintclean. CI is the first run on other Python versions.
benchmarked separately.
Plan: products encode their own values
Agreed with @brettlangdon: the end state is that every product encodes its own
meta_structvalues withpackbwhen its span finishes, and the core never encodes product data. This PR does not force that move.It keeps two paths, so each product switches on its own timeline:
span._set_struct_tag(key, dict)span._set_encoded_struct_tag(key, packb(value))span._get_struct_tag(key)(live dict)span._get_encoded_struct_tag(key)(bytes)product changes.
_set_encoded_struct_tag(today only the native trace buffer does), one rule for a key set through both paths, and
_has_meta_structs()sees both. Until it lands, products stay on today's path.(11 call sites in 9 modules). A key moves when nothing edits it after the span finishes, because an encoded
value can no longer change; the LLMObs processor, which removes its key in a trace processor, moves last.
Span._meta_struct,_packed_struct_tags/_encode_struct_tags, the encode step inthe writers and the five transitional stubs in
_native.pyi.Risks
_native.pyistill declares the five live struct-tag methods onSpanData, because AppSec, IAST andsecurity type their spans as
SpanData(refactor(appsec): use native span data interfaces #20898). OnlySpanimplements them; the stub comment says so.They go away in step 4.
trace processors. A product that moves to the target path gives that up for its key.
Notes
changelog/no-changelog.🤖 Generated with Claude Code