Repository navigation
fix(profiling): preserve propagated span metadata - #19378
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
Conversation
59f73eb to
9bc3860
Compare
d059b96 to
7a0d380
Compare
🎉 All green!🧪 All tests passed 🔗 Commit SHA: a12926e | Docs | Datadog PR Page | Give us feedback! |
## Description The stack profiler stores trace metadata in a native map keyed by Python thread ID. Later attribution fixes need two distinct cleanup operations: - remove an entry only when it still contains an expected span ID; - clear the current thread entry unconditionally when its execution context is known to be invalid. This PR adds those internal primitives without changing when span links are published or removed: ```text unlink_span(expected_span_id) -> compare and erase atomically clear_span() -> erase the current thread entry ``` The expected-ID check and erase occur under the existing `ThreadSpanLinks` mutex, preventing an older execution context from deleting a newer span link on the same physical thread. Python-facing mutations continue to release the GIL before waiting for the profiler mutex, preserving the thread-pool deadlock protection from #14852. This is the first prerequisite in the PROF-14213 span-attribution stack. It has no user-visible behavior until called by the later cleanup PRs. ## Testing - Standalone native `ThreadSpanLinks` suite: `3 passed`. - The new regression verifies that a non-matching expected span ID preserves the entry and a matching ID removes it. - clang-format 18. - `git diff --check`. - The cumulative stacked tip passed the full Python 3.14 gevent profiling suite: `398 passed, 13 skipped, 2 xpassed`. ## Risks Low. This adds internal methods and bindings but does not call them from existing attribution paths. Both operations use the existing mutex and expected O(1) map lookup/erase behavior. ## Additional Notes - Next stacked PR: #19378 - No release note is needed because this PR does not change customer-visible behavior. - Jira: [PROF-14213](https://datadoghq.atlassian.net/browse/PROF-14213) [PROF-14213]: https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
gyuheon0h
left a comment
There was a problem hiding this comment.
LGTM I assume unlink_span is currently dead code because we are using it in the stacked PR?
Circular import analysis
|
@gyuheon0h correct, but it's actually from this PR's previous PR #19377, and it's now merged. This PR is rebased to latest main, and should have a smaller set of diffs. |
|
/merge |
|
View all feedbacks in Devflow UI.
This pull request is not mergeable according to GitHub. Common reasons include pending required checks, missing approvals, or merge conflicts — but it could also be blocked by other repository rules or settings.
The expected merge time in
|
Codeowners resolved as |
## Description The stack profiler stores trace metadata in a native map keyed by Python thread ID. Later attribution fixes need two distinct cleanup operations: - remove an entry only when it still contains an expected span ID; - clear the current thread entry unconditionally when its execution context is known to be invalid. This PR adds those internal primitives without changing when span links are published or removed: ```text unlink_span(expected_span_id) -> compare and erase atomically clear_span() -> erase the current thread entry ``` The expected-ID check and erase occur under the existing `ThreadSpanLinks` mutex, preventing an older execution context from deleting a newer span link on the same physical thread. Python-facing mutations continue to release the GIL before waiting for the profiler mutex, preserving the thread-pool deadlock protection from #14852. This is the first prerequisite in the PROF-14213 span-attribution stack. It has no user-visible behavior until called by the later cleanup PRs. ## Testing - Standalone native `ThreadSpanLinks` suite: `3 passed`. - The new regression verifies that a non-matching expected span ID preserves the entry and a matching ID removes it. - clang-format 18. - `git diff --check`. - The cumulative stacked tip passed the full Python 3.14 gevent profiling suite: `398 passed, 13 skipped, 2 xpassed`. ## Risks Low. This adds internal methods and bindings but does not call them from existing attribution paths. Both operations use the existing mutex and expected O(1) map lookup/erase behavior. ## Additional Notes - Next stacked PR: #19378 - No release note is needed because this PR does not change customer-visible behavior. - Jira: [PROF-14213](https://datadoghq.atlassian.net/browse/PROF-14213) [PROF-14213]: https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
## Description
This PR is stacked on the native cleanup primitives prerequisite.
### Problem
When tracing crosses an execution boundary without sharing the live parent `Span`, ddtrace passes a detached `ddtrace.trace.Context` containing the trace ID and current parent span ID. For example, the futures integration copies this Context when submitting work to a thread-pool worker, and that active Context can later be inherited by a child greenlet.
A child span created from this detached Context has the correct parent span ID but no live parent `Span` object. Copied profiler metadata on the Context carries the original local-root span ID and span type needed for endpoint correlation.
The profiler previously copied that local-root metadata into `threading.local()` storage during Context activation. That works for a simple thread-pool worker, but gevent greenlets share a physical thread. One greenlet could therefore overwrite the detached Context metadata needed by another greenlet.
### Fix
For a span created with `child_of=Context`, read the copied profiler metadata directly from `span._parent_context` instead of physical-thread-local side state.
Conceptually:
```text
detached tracing Context:
current span ID: 200
copied local-root ID: 100
copied span type: web
child span:
span ID: 300
profiling local root: 100
```
The Context remains the source of truth across both thread and greenlet execution boundaries. If profiler metadata is absent, the Context's span ID remains the fallback local-root ID.
## Testing
Fresh Python 3.14 gevent profiling environment:
- Context without profiler metadata falls back to its span ID;
- Context with profiler metadata preserves the copied local-root ID and span type;
- a greenlet child span retains the local root copied from its detached parent Context.
Result: `3 passed`.
Also validated with targeted formatting, `git diff --check`, and the cumulative full Python 3.14 profiling suite at the final stack tip: `398 passed, 13 skipped, 2 xpassed`.
## Risks
Low. The change removes physical-thread-local side state and reads scalar/string metadata already attached to the detached Context. It does not retain a parent Span or alter tracing propagation.
## Additional Notes
- Previous PR: #19377.
- Next stacked PR: #19379.
- A customer-facing profiling fix release note is included.
- Jira: [PROF-14213](https://datadoghq.atlassian.net/browse/PROF-14213)
[PROF-14213]: https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
Description
This PR is stacked on the native cleanup primitives prerequisite.
Problem
When tracing crosses an execution boundary without sharing the live parent
Span, ddtrace passes a detachedddtrace.trace.Contextcontaining the trace ID and current parent span ID. For example, the futures integration copies this Context when submitting work to a thread-pool worker, and that active Context can later be inherited by a child greenlet.A child span created from this detached Context has the correct parent span ID but no live parent
Spanobject. Copied profiler metadata on the Context carries the original local-root span ID and span type needed for endpoint correlation.The profiler previously copied that local-root metadata into
threading.local()storage during Context activation. That works for a simple thread-pool worker, but gevent greenlets share a physical thread. One greenlet could therefore overwrite the detached Context metadata needed by another greenlet.Fix
For a span created with
child_of=Context, read the copied profiler metadata directly fromspan._parent_contextinstead of physical-thread-local side state.Conceptually:
The Context remains the source of truth across both thread and greenlet execution boundaries. If profiler metadata is absent, the Context's span ID remains the fallback local-root ID.
Testing
Fresh Python 3.14 gevent profiling environment:
Result:
3 passed.Also validated with targeted formatting,
git diff --check, and the cumulative full Python 3.14 profiling suite at the final stack tip:398 passed, 13 skipped, 2 xpassed.Risks
Low. The change removes physical-thread-local side state and reads scalar/string metadata already attached to the detached Context. It does not retain a parent Span or alter tracing propagation.
Additional Notes