Skip to content

fix(profiling): clear finished span links - #19341

Merged
taegyunkim merged 0 commit into
taegyunkim/profiling-logical-span-linksfrom
taegyunkim/profiling-clear-finished-endpoint
Aug 5, 2026
Merged

taegyunkim merged 0 commit into
taegyunkim/profiling-logical-span-linksfrom
taegyunkim/profiling-clear-finished-endpoint

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR is stacked on #19420.

Fixes stale stack-profiler attribution after a trace finishes and adds the shared activation and lifecycle layer used by the gevent and asyncio follow-ups.

Activation routing

  • Registers tracing activation and span-finish listeners only after the stack collector starts successfully, then removes both during shutdown.
  • Routes each activation to the highest-priority logical provider that identifies a native-renderable target. If none does, attribution is stored against the physical thread.
  • Uses domain-qualified targets for physical threads, asyncio tasks, and gevent greenlets.
  • Stores generation-scoped copied span metadata in a profiler ContextVar, allowing newly created logical contexts to inherit attribution without retaining a Span.
  • Uses the safe native ContextVar setter before Python 3.12.

Cleanup and process lifecycle

ThreadSpanLinks owns the forward and reverse indexes under one native mutex. Finishing a span atomically removes every current target whose span or local root derives from it. If a target has already advanced to another span, the reverse index no longer associates it with the finished span, so cleanup preserves the newer mapping.

Profiler enable, disable, restart, and post-fork hooks clear native mappings and invalidate generation-scoped ContextVar state. No physical or logical span attribution crosses a fork. Only a real activation in the child can create a new link.

Observability and resource bounds

  • Emits low-cardinality profiler.span_links.dropped telemetry counters for provider failures and integration-supplied conservative rejection reasons.
  • Reports profiler.span_links gauges by target domain at profiler flush time.
  • Relies on integration lifecycle cleanup rather than arbitrary eviction, which could silently remove valid attribution.

Testing

Added coverage for:

  • provider priority, provider failure, and physical-thread fallback;
  • generation-scoped inherited ContextVar state;
  • safe ContextVar writes before Python 3.12;
  • domain-scoped logical detach and clear operations;
  • atomic finished-span cleanup and local-root invalidation;
  • post-fork invalidation of physical and logical state;
  • span-link mapping gauges;
  • synchronous traced work followed by untraced work;
  • nested child-to-parent restoration;
  • cleanup on reused thread-pool workers.

Validation completed:

  • Python 3.11 focused shared lifecycle tests in normal and fast-copy modes: 10 passed in each mode
  • Python 3.11 full profiling suite in normal and fast-copy modes: 326 passed, 86 skipped, 2 xpassed in each mode
  • downstream focused tests on Python 3.9 and 3.12
  • scripts/lint
  • scripts/lint cformat
  • scripts/lint profiling-native-check
  • git diff --check

Risks

  • Profiling-enabled tracing activations perform a small provider scan followed by an expected O(1) native mapping update.
  • Finished-span cleanup visits only targets indexed by the finished span or local root, not every thread, task, or greenlet.
  • Telemetry failures and provider failures are contained so profiler attribution cannot break application tracing.
  • The rollback is to disable stack profiling or revert this stacked change. Missing logical attribution remains preferable to cross-attribution.

Additional Notes

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

Codeowners resolved as

ddtrace/internal/datadog/profiling/stack/__init__.py                    @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/__init__.pyi                   @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/_stack.pyi                     @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/include/thread_span_links.hpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/stack.cpp                  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/thread_span_links.cpp      @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/test/test_thread_span_links.cpp  @DataDog/profiling-python
ddtrace/profiling/collector/stack.py                                    @DataDog/profiling-python
releasenotes/notes/profiling-clear-finished-endpoint-e75147a1799ab41a.yaml  @DataDog/apm-python
tests/profiling/collector/pprof_utils.py                                @DataDog/profiling-python
tests/profiling/collector/test_stack_endpoint_labels.py                 @DataDog/profiling-python
tests/profiling/collector/test_stack_span_links.py                      @DataDog/profiling-python

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 28, 2026 •

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 -> ddtrace.trace -> ddtrace._trace.tracer -> ddtrace.internal.debug -> ddtrace.internal.runtime.runtime_metrics -> ddtrace

@taegyunkim taegyunkim added the Profiling Continous Profling label Jul 28, 2026
@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

⚠️ Warnings

🚦 14 Pipeline jobs failed

System Tests | tracer-release / End-to-end #8 / fastapi 8   View in Datadog   GitHub Actions

🔧 Fix in code. This looks caused by changes in this PR. 4 failed tests due to assertion errors: Telemetry was not received in tests/debugger/test_debugger_telemetry.py.

🧪 4 Tests failed · 🎯 related to the job error above

🎯 tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry.test_telemetry_co[fastapi] from system_tests_suite   View in Datadog
AssertionError: Telemetry was not received
assert None
 +  where None = <tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry object at 0x7f255cb39bb0>.telemetry

self = <tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry object at 0x7f255cb39bb0>

    @slow
    def test_telemetry_co(self):
>       self._assert(required_telemetry=["code_origin_for_spans_enabled"])

...
🎯 tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry.test_telemetry_di[fastapi] from system_tests_suite   View in Datadog
AssertionError: Telemetry was not received
assert None
 +  where None = <tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry object at 0x7f255cb39c40>.telemetry

self = <tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry object at 0x7f255cb39c40>

    def test_telemetry_di(self):
>       self._assert(required_telemetry=["dynamic_instrumentation_enabled"])

tests/debugger/test_debugger_telemetry.py:75: 
...
🎯 tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry.test_telemetry_er[fastapi] from system_tests_suite   View in Datadog
AssertionError: Telemetry was not received
assert None
 +  where None = <tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry object at 0x7f255cb39d00>.telemetry

self = <tests.debugger.test_debugger_telemetry.Test_Debugger_Telemetry object at 0x7f255cb39d00>

    @slow
    def test_telemetry_er(self):
>       self._assert(required_telemetry=["exception_replay_enabled"])

...
View all failed tests

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v113741238-d2b8243-manylinux2014_x86_64, 1]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v126532274-233089d-musllinux_1_2_x86_64, 1]   View in Datadog   GitLab

View all 14 failed jobs.

📋 Copy prompt for your agent
CI on my pull request is failing. Help me find and fix the root cause of each failing job below — they were flagged as caused by changes in this PR, so focus on the diff. For each job, explain the failure and propose a fix.

Branch: taegyunkim/profiling-clear-finished-endpoint

System Tests | tracer-release / End-to-end #8 / fastapi 8
Commit: c31cb81c63d15f0a2d50f6230e9d1bbee8b27895
Error (code / test):
4 failed tests due to assertion errors: Telemetry was not received in tests/debugger/test_debugger_telemetry.py.
CI job: https://github.com/DataDog/dd-trace-py/actions/runs/31029296038/job/92387714933

ℹ️ Info

No other issues found (see more)

❄️ No new flaky tests detected

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: e121905 | Docs | Datadog PR Page | Give us feedback!

@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-clear-finished-endpoint branch from 7629ec0 to e9d510e Compare July 29, 2026 17:30
@taegyunkim
taegyunkim changed the base branch from main to taegyunkim/profiling-gevent-span-links July 29, 2026 17:32
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-clear-finished-endpoint branch from e9d510e to c5a8f30 Compare July 29, 2026 17:38
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-gevent-span-links branch 2 times, most recently from 3eefb90 to 1694041 Compare July 29, 2026 18:23
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-clear-finished-endpoint branch 3 times, most recently from 4015c0e to 9a412ec Compare July 30, 2026 20:56
@taegyunkim
taegyunkim changed the base branch from taegyunkim/profiling-gevent-span-links to taegyunkim/profiling-logical-span-links July 30, 2026 20:56
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-clear-finished-endpoint branch 2 times, most recently from 6b72027 to 658892c Compare July 30, 2026 21:31
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-logical-span-links branch from dab384f to afadd01 Compare August 5, 2026 17:01
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-clear-finished-endpoint branch from 658892c to c500e46 Compare August 5, 2026 17:01
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-logical-span-links branch from afadd01 to 1153d8b Compare August 5, 2026 17:13
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-clear-finished-endpoint branch from c500e46 to 615d186 Compare August 5, 2026 17:13
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-logical-span-links branch from 1153d8b to ca6945c Compare August 5, 2026 17:16
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-clear-finished-endpoint branch 2 times, most recently from e121905 to b47705e Compare August 5, 2026 17:56
@taegyunkim
taegyunkim merged commit b47705e into taegyunkim/profiling-logical-span-links Aug 5, 2026
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-logical-span-links branch from ca6945c to e50fe98 Compare August 5, 2026 17:56
@taegyunkim
taegyunkim deleted the taegyunkim/profiling-clear-finished-endpoint branch August 5, 2026 17:56
@taegyunkim
taegyunkim restored the taegyunkim/profiling-clear-finished-endpoint branch August 5, 2026 17:57
@taegyunkim

Copy link
Copy Markdown
Contributor Author

The stack was reordered so physical-thread cleanup lands first. GitHub marked this PR merged when its head became an ancestor of the rewritten base branch. The physical cleanup now continues in draft PR #19529, followed by common logical helpers in #19420.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Profiling Continous Profling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant