Skip to content

refactor(profiling): centralize span link lifecycle - #20347

Closed
taegyunkim wants to merge 1 commit into
mainfrom
taegyunkim/profiling-span-link-lifecycle
Closed

taegyunkim wants to merge 1 commit into
mainfrom
taegyunkim/profiling-span-link-lifecycle

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

Stacked directly on main.

Centralize profiler span-link routing and lifecycle handling in a runtime-independent Python module. The stack collector now normalizes tracing contexts once, starts and stops span linking with the collector, republishes an already-active span, invalidates inherited state across restarts and forks, and removes every native link derived from a finished span.

This PR also adds the native reset_span_links() lifecycle operation. It intentionally keeps only physical-thread attribution. Asyncio task storage and task-specific helpers are introduced with their first consumer in #19736, while greenlet storage and greenlet-specific helpers are introduced in #19420.

With no runtime-specific provider registered, activations continue to use the existing physical-thread native API. The coordinator remains importable when the optional native stack extension is unavailable.

Testing

  • scripts/lint checks on the complete rewritten stack
  • scripts/run-tests --venv 1ed51fe -- tests/profiling/collector/test_stack_span_links.py tests/profiling/collector/test_stack_threadpool.py -k 'stack_span_links or link_span_plain_context or link_span_context'
    • 8 passed, 1 skipped, 442 deselected on Python 3.12
  • Benchmark discovery found no matching benchmark suites for these profiling files

Risks

The collector's listener, restart, and fork lifecycle is consolidated. Tests cover context deactivation, inherited context validation, finished spans, fork reset, current-span republishing, and propagated context metadata.

No runtime-specific provider is registered in this PR, so asyncio tasks, AnyIO workers, and gevent greenlets do not change attribution behavior yet.

Additional Notes

No release note is needed because this is an internal refactor with no customer-visible behavior change. The PR keeps the changelog/no-changelog label.

Review this stack in order:

Order PR Purpose
1 This PR Centralize Python routing and lifecycle handling
2 #19736 Add task-specific native storage and attribute asyncio task samples
3 #19737 Propagate attribution into AnyIO worker threads
4 #19420 Add greenlet-specific native storage and attribute gevent samples

The former standalone native infrastructure PR #20346 is superseded by the runtime-specific PRs above.

@taegyunkim taegyunkim added the changelog/no-changelog A changelog entry is not required for this PR. label Sep 15, 2026
@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against taegyunkim/profiling-native-span-links using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

ddtrace/profiling/_span_links.py                                        @DataDog/profiling-python
ddtrace/profiling/collector/stack.py                                    @DataDog/profiling-python
tests/profiling/collector/test_stack_span_links.py                      @DataDog/profiling-python
tests/profiling/collector/test_stack_threadpool.py                      @DataDog/profiling-python

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Circular import analysis

⚠️ Existing circular imports

There are 1 circular imports that already exist on the base branch and have not been changed by this PR.

ddtrace.errortracking._handled_exceptions.bytecode_injector -> ddtrace.errortracking._handled_exceptions.callbacks -> ddtrace.errortracking._handled_exceptions.collector -> ddtrace.errortracking._handled_exceptions.bytecode_reporting -> ddtrace.errortracking._handled_exceptions.bytecode_injector

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 218 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 218 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=133)
ddtrace.llmobs._integrations.vertexai -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=131)
ddtrace.appsec._listeners -×-> ddtrace.trace  (product:appsec -> product:tracing, score=131)
ddtrace.llmobs._integrations.base -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=131)
ddtrace.debugging._debugger -×-> ddtrace.trace  (product:debugging -> product:tracing, score=131)

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

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Pipelines  Tests

❌ Errors

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

🚦 1 Pipeline job failed

DataDog/apm-reliability/dd-trace-py | download win_arm64 wheels

View more details · View in GitLab

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

🔄 Datadog retried 1 test - 1 passed on retry View in Datadog

Useful? React with 👍 / 👎

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

@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-span-link-lifecycle branch from fabbeef to d92c58d Compare September 16, 2026 16:20
@taegyunkim
taegyunkim changed the base branch from taegyunkim/profiling-native-span-links to main September 16, 2026 16:21
@taegyunkim

Copy link
Copy Markdown
Contributor Author

Closing in favor of #19736, which now bases directly on main and includes this centralization refactor as its first commit. Splitting the refactor into its own PR meant reviewing a ContextVar write with no in-tree reader; folding it into #19736 lets the write and read sides be reviewed together as a complete loop. Read #19736's first commit for the refactor in isolation.

@taegyunkim taegyunkim closed this Sep 16, 2026
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