Repository navigation
chore(profiling): add conditional span link cleanup - #19377
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit into
Conversation
🎉 All green!🧪 All tests passed 🔗 Commit SHA: 9bc3860 | Docs | Datadog PR Page | Give us feedback! |
59f73eb to
9bc3860
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9bc3860420
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
/merge |
|
View all feedbacks in Devflow UI.
The expected merge time in
mergequeue build completed successfully, but the github api returned an error while merging the pr DetailsError: PUT https://api.github.com/repos/DataDog/dd-trace-py/pulls/19377/merge: 403 Merging stacked PRs via this API is not supported. Use the web interface instead. [] (Request ID: BE2C:4AF1B:327C96:B14B38:6A6A5CDC) FullStacktrace: |
|
/merge -f --reason "it passed all the CI in the merge branch but seems like our mergequeue doesn't support merging one PR from a stacked PRs" |
|
View all feedbacks in Devflow UI.
The expected merge time in
mergequeue build completed successfully, but the github api returned an error while merging the pr DetailsError: PUT https://api.github.com/repos/DataDog/dd-trace-py/pulls/19377/merge: 403 Merging stacked PRs via this API is not supported. Use the web interface instead. [] (Request ID: F34F:1ACD37:361855:BE463D:6A6A5D71) FullStacktrace: |
|
/merge -f --reason "it passed all the CI in the merge branch but seems like our mergequeue doesn't support merging one PR from a stack of PRs, retrying after unstacking" |
|
View all feedbacks in Devflow UI.
The expected merge time in
Warning This change was merged without running any pre merge CI checks Reason: it passed all the CI in the merge branch but seems like our mergequeue doesn't support merging one PR from a stack of PRs, retrying after unstacking |
Codeowners resolved as |
Circular import analysis
|
## 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 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
The stack profiler stores trace metadata in a native map keyed by Python thread ID. Later attribution fixes need two distinct cleanup operations:
This PR adds those internal primitives without changing when span links are published or removed:
The expected-ID check and erase occur under the existing
ThreadSpanLinksmutex, 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
ThreadSpanLinkssuite:3 passed.git diff --check.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