Skip to content

perf(profiling): build code provenance in the background at scheduler start - #20611

Draft
vlad-scherbich wants to merge 4 commits into
mainfrom
vlad/profiling-prewarm-provenance
Draft

vlad-scherbich wants to merge 4 commits into
mainfrom
vlad/profiling-prewarm-provenance

Conversation

@vlad-scherbich

@vlad-scherbich vlad-scherbich commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

ddup.upload builds the code provenance file on its first call. A process that exits before its first periodic upload does that in the shutdown flush, and on a cold cache that includes the installed-packages scan. #20808 made the scan native but did not remove the Python provenance build.

The scheduler now builds the file once on a background thread after its periodic worker starts. start() and stop() do not wait for it. Nothing runs when code provenance is disabled.

Each thread now writes its own temp file. The file lock does not keep out a second thread of the same process, so a flush building at the same time as the background thread could lose the rename and upload without provenance.

Testing

tests/profiling/test_scheduler.py and test_code_provenance.py cover the background build, the disabled case, build failure, a flush during the build, a fork during the build, and two builders at once. The background-build, flush, fork, and two-builder tests fail on main.

Risks

If the process exits before the background build finishes, the shutdown flush still builds the file itself. A fork during a cold build waits for the build to finish.

Additional Notes

None.

@datadog-prod-us1-6

datadog-prod-us1-6 Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

❌ Errors

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

🚦 7 Pipeline jobs failed

Profiling correctness | trigger-downstream — 🔧 Needs a code fix, caused by this PR

View more details · View in GitHub Actions

DDCI - DataDog/dd-trace-py | prof-correctness

View more details · View in DDCI

DataDog/apm-reliability/dd-trace-py | Debian_12_amd64.SID0: [test-app-python]

View more details · View in GitLab

View all 7 failed jobs.

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

Useful? React with 👍 / 👎

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

@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 29, 2026 •

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

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

Show existing violations (showing 5 of 102 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=122)
ddtrace.llmobs._integrations.claude_agent_sdk -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=120)
ddtrace.llmobs._integrations.crewai -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=120)
ddtrace.llmobs._integrations.llama_index -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=120)
ddtrace.llmobs._integrations.langchain -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=120)

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

@cit-pr-commenter-54b7da

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

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against main using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

ddtrace/internal/datadog/profiling/code_provenance.py                   @DataDog/profiling-python
ddtrace/profiling/scheduler.py                                          @DataDog/profiling-python
releasenotes/notes/profiling-code-provenance-background-e2922e84df210e13.yaml  @DataDog/apm-python
tests/profiling/test_code_provenance.py                                 @DataDog/profiling-python
tests/profiling/test_scheduler.py                                       @DataDog/profiling-python

@vlad-scherbich vlad-scherbich added the changelog/no-changelog A changelog entry is not required for this PR. label Sep 29, 2026
@vlad-scherbich
vlad-scherbich marked this pull request as ready for review September 29, 2026 15:49
@vlad-scherbich
vlad-scherbich requested a review from a team as a code owner September 29, 2026 15:49

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8e7658d173

ℹ️ 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".

Comment thread ddtrace/profiling/scheduler.py Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description says this moves the cost to Profiler.start; I haven't read the code but if it is true it is not better. Users can manage their Profiler instance themselves, meaning them calling Profiler.start may block (for a long time, e.g. in dogweb) so this is not better than what we have today and from a user's perspective arguably worse.

Can we avoid doing it on the main thread maybe? (I guess it's hard to "fix" because it has to be a Python thread no matter what?)

@vlad-scherbich vlad-scherbich Sep 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll put this PR back in draft while waiting on testing to come back, which will show concrete deltas.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed after a period of inactivity.
After this much time, it will likely be easier to open a new pull request with the
same changes than to update this one from the base branch. Please comment or reopen
if you think this pull request was closed in error.

@github-actions github-actions Bot closed this Nov 1, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this has been auto-closed; I believe this shouldn't be an issue anymore now that Gab has rewritten the logic in Rust!

The first upload builds the code provenance file when the on-disk cache
is cold, and that build waits for the installed packages scan. A process
that exits before its first periodic upload paid for it in the shutdown
flush. Resolving it synchronously in Scheduler._start_service only moved
the stall onto Profiler.start, which waits for the scheduler to start.

Start the build on a one-shot background thread instead, so neither
start nor the exit flush waits for it when the process lives long enough
for it to finish. ddup.upload stays the consumer and reuses the memoized
path; if it runs first it builds the file itself.
@vlad-scherbich vlad-scherbich reopened this Nov 2, 2026
@vlad-scherbich
vlad-scherbich force-pushed the vlad/profiling-prewarm-provenance branch from d247339 to e104ef9 Compare November 2, 2026 18:50
@pr-commenter

pr-commenter Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-03 19:01:15

Comparing candidate commit b2c02d3 in PR branch vlad/profiling-prewarm-provenance with baseline commit 9a6d791 in branch main.

📊 Benchmarking dashboard

Found 5 performance improvements and 0 performance regressions! Performance is the same for 573 metrics, 10 unstable metrics, 1 flaky benchmarks without significant changes.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

scenario:httppropagationextract-empty_headers

  • 🟩 execution_time [-71.131ns; -58.677ns] or [-9.302%; -7.673%]

scenario:httppropagationextract-wsgi_empty_headers

  • 🟩 execution_time [-70.912ns; -60.140ns] or [-9.242%; -7.838%]

scenario:httppropagationextract-wsgi_invalid_span_id_header

  • 🟩 execution_time [-70.805ns; -61.058ns] or [-9.291%; -8.012%]

scenario:telemetryaddmetric-100-count-metrics-100-times

  • 🟩 execution_time [-1.339ms; -1.123ms] or [-8.389%; -7.036%]

scenario:telemetryaddmetric-100-rate-metrics-100-times

  • 🟩 execution_time [-142.512µs; -121.838µs] or [-8.775%; -7.502%]

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:coreapiscenario-context_with_data_listeners

  • unstable execution_time [-678.164ns; +771.498ns] or [-6.351%; +7.226%]

scenario:coreapiscenario-core_dispatch_1_listener

  • unstable execution_time [-32.109ns; +39.545ns] or [-4.687%; +5.772%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-1751.893ns; +2052.676ns] or [-8.850%; +10.370%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-1805.985ns; +1837.714ns] or [-9.437%; +9.603%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-259.652ns; +490.287ns] or [-6.110%; +11.538%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-190.289ns; +255.843ns] or [-7.119%; +9.572%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-81.458ns; +100.425ns] or [-5.794%; +7.143%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-4744.824ns; +4870.979ns] or [-9.449%; +9.700%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-1100.510ns; +888.615ns] or [-10.344%; +8.353%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • unstable execution_time [-56721.325ns; +55949.324ns] or [-9.652%; +9.520%]

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

Known flaky benchmarks without significant changes:

  • scenario:packagespackageforrootmodulemapping-cache_off

@github-actions github-actions Bot removed the stale label Nov 3, 2026
The file lock only keeps out other processes. With the background build
from the scheduler, a flush in the same process can build the file at the
same time; both wrote the same temp file and the second rename failed, so
that caller got no provenance file. Use one temp file per thread.

Add tests for a flush during the background build and for a fork waiting
on it.
@vlad-scherbich vlad-scherbich changed the title perf(profiling): resolve code provenance at scheduler start perf(profiling): build code provenance in the background at scheduler start Nov 3, 2026
The per-thread cache temp file and the scheduler tests on this branch left those names untyped.
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.

2 participants