Skip to content

feat(profiling): Start profilers synchronously within tracer initialization#5906

Merged
szegedi merged 5 commits intomasterfrom
szegedi/profiler-sync-start
Mar 23, 2026
Merged

feat(profiling): Start profilers synchronously within tracer initialization#5906
szegedi merged 5 commits intomasterfrom
szegedi/profiler-sync-start

Conversation

@szegedi
Copy link
Copy Markdown
Contributor

@szegedi szegedi commented Jun 17, 2025

What does this PR do?

Start profilers synchronously within tracer initialization.

Motivation

Profiler was started asynchronously because of source map loading – it uses async file IO. There are drawbacks to starting the profiler asynchronously though. A minor issue is that it won't start capturing CPU samples for the root program. A larger issue is that any long-lived allocations made after tracer init in the root program also won't ever be shown in the heap live size snapshots. We've had customers complaining about this.

Additional Notes

  • Source maps are still loaded asynchronously. Since they aren't needed until the first time profiles are emitted (65s into the runtime) they should have plenty of time to get loaded.
  • We moved initialization of compression functions (thus, loading of zlib etc.) also out of the synchronous startup path as they, too, are only required first time profiles are emitted.

Jira: PROF-14055

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 17, 2025

Codecov Report

❌ Patch coverage is 78.37838% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.09%. Comparing base (b6d42d8) to head (adfb0e9).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
packages/dd-trace/src/profiling/profiler.js 75.75% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5906      +/-   ##
==========================================
- Coverage   80.45%   80.09%   -0.37%     
==========================================
  Files         748      744       -4     
  Lines       32405    32174     -231     
==========================================
- Hits        26072    25770     -302     
- Misses       6333     6404      +71     
Flag Coverage Δ
aiguard-macos 39.15% <25.00%> (-0.20%) ⬇️
aiguard-ubuntu 39.27% <25.00%> (-0.20%) ⬇️
aiguard-windows 38.97% <25.00%> (-0.20%) ⬇️
apm-capabilities-tracing-macos 48.99% <8.10%> (+0.06%) ⬆️
apm-capabilities-tracing-ubuntu ?
apm-capabilities-tracing-windows 48.76% <8.10%> (+0.06%) ⬆️
apm-integrations-child-process 38.49% <25.00%> (-0.16%) ⬇️
apm-integrations-couchbase-18 37.33% <25.00%> (-0.20%) ⬇️
apm-integrations-couchbase-eol 37.84% <25.00%> (-0.16%) ⬇️
apm-integrations-oracledb ?
appsec-express 55.17% <25.00%> (-0.17%) ⬇️
appsec-fastify 51.51% <25.00%> (-0.17%) ⬇️
appsec-graphql 51.68% <25.00%> (-0.18%) ⬇️
appsec-kafka 44.29% <25.00%> (-0.19%) ⬇️
appsec-ldapjs 43.92% <25.00%> (-0.18%) ⬇️
appsec-lodash 43.54% <25.00%> (-0.18%) ⬇️
appsec-macos 58.18% <25.00%> (-0.11%) ⬇️
appsec-mongodb-core 48.68% <25.00%> (-0.19%) ⬇️
appsec-mongoose 49.34% <25.00%> (-0.19%) ⬇️
appsec-mysql ?
appsec-node-serialize 43.10% <25.00%> (-0.18%) ⬇️
appsec-passport 47.53% <25.00%> (-0.20%) ⬇️
appsec-postgres ?
appsec-sourcing 42.53% <25.00%> (-0.17%) ⬇️
appsec-template 43.26% <25.00%> (-0.18%) ⬇️
appsec-ubuntu ?
appsec-windows 58.01% <25.00%> (-0.13%) ⬇️
instrumentations-instrumentation-bluebird 32.27% <25.00%> (-0.17%) ⬇️
instrumentations-instrumentation-body-parser 40.43% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-child_process 37.84% <25.00%> (-0.10%) ⬇️
instrumentations-instrumentation-cookie-parser 34.23% <25.00%> (-0.16%) ⬇️
instrumentations-instrumentation-express 34.55% <25.00%> (-0.17%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 34.36% <25.00%> (-0.16%) ⬇️
instrumentations-instrumentation-express-session 40.07% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-fs 31.89% <25.00%> (-0.17%) ⬇️
instrumentations-instrumentation-generic-pool 29.46% <ø> (-0.07%) ⬇️
instrumentations-instrumentation-http 39.71% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-knex 32.27% <25.00%> (-0.17%) ⬇️
instrumentations-instrumentation-mongoose 33.39% <25.00%> (-0.16%) ⬇️
instrumentations-instrumentation-multer 40.18% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-mysql2 38.20% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-passport 43.96% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-passport-http 43.64% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-passport-local 44.17% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-pg 37.64% <25.00%> (-0.19%) ⬇️
instrumentations-instrumentation-promise 32.20% <25.00%> (-0.17%) ⬇️
instrumentations-instrumentation-promise-js 32.21% <25.00%> (-0.17%) ⬇️
instrumentations-instrumentation-q 32.25% <25.00%> (-0.17%) ⬇️
instrumentations-instrumentation-url 32.17% <25.00%> (-0.17%) ⬇️
instrumentations-instrumentation-when 32.22% <25.00%> (-0.17%) ⬇️
llmobs-ai 42.16% <25.00%> (-0.20%) ⬇️
llmobs-anthropic 40.21% <25.00%> (-0.19%) ⬇️
llmobs-bedrock 39.15% <25.00%> (-0.18%) ⬇️
llmobs-google-genai 39.69% <25.00%> (-0.20%) ⬇️
llmobs-langchain 39.94% <25.00%> (-0.28%) ⬇️
llmobs-openai 43.88% <25.00%> (-0.19%) ⬇️
llmobs-vertex-ai 39.94% <25.00%> (-0.20%) ⬇️
platform-core 31.47% <ø> (ø)
platform-esbuild 34.42% <ø> (ø)
platform-instrumentations-misc 48.41% <ø> (ø)
platform-shimmer 37.56% <ø> (ø)
platform-unit-guardrails 32.89% <ø> (ø)
plugins-azure-durable-functions 25.74% <ø> (ø)
plugins-azure-event-hubs 25.90% <ø> (ø)
plugins-azure-service-bus 25.26% <ø> (ø)
plugins-bullmq 44.17% <25.00%> (-0.12%) ⬇️
plugins-cassandra 37.83% <25.00%> (-0.18%) ⬇️
plugins-cookie 26.96% <ø> (ø)
plugins-cookie-parser 26.75% <ø> (ø)
plugins-crypto 26.73% <ø> (ø)
plugins-dd-trace-api 38.22% <75.00%> (-0.20%) ⬇️
plugins-express-mongo-sanitize 26.89% <ø> (ø)
plugins-express-session 26.70% <ø> (ø)
plugins-fastify 42.15% <25.00%> (-0.19%) ⬇️
plugins-fetch 38.28% <25.00%> (-0.18%) ⬇️
plugins-fs 38.55% <25.00%> (-0.20%) ⬇️
plugins-generic-pool 25.94% <ø> (ø)
plugins-google-cloud-pubsub 45.34% <25.00%> (-0.22%) ⬇️
plugins-grpc 40.82% <25.00%> (-0.19%) ⬇️
plugins-handlebars 26.94% <ø> (ø)
plugins-hapi 40.07% <25.00%> (-0.19%) ⬇️
plugins-hono 40.32% <25.00%> (-0.19%) ⬇️
plugins-ioredis 38.36% <25.00%> (-0.19%) ⬇️
plugins-knex 26.57% <ø> (ø)
plugins-langgraph 38.38% <25.00%> (-0.19%) ⬇️
plugins-ldapjs 24.43% <ø> (ø)
plugins-light-my-request 26.30% <ø> (ø)
plugins-limitd-client 32.55% <25.00%> (-0.17%) ⬇️
plugins-lodash 26.03% <ø> (ø)
plugins-mariadb 39.40% <25.00%> (-0.20%) ⬇️
plugins-memcached 38.07% <25.00%> (-0.19%) ⬇️
plugins-microgateway-core 39.14% <25.00%> (-0.19%) ⬇️
plugins-moleculer 40.43% <25.00%> (-0.21%) ⬇️
plugins-mongodb 39.08% <25.00%> (-0.30%) ⬇️
plugins-mongodb-core 38.91% <25.00%> (-0.19%) ⬇️
plugins-mongoose 38.79% <25.00%> (-0.18%) ⬇️
plugins-multer 26.70% <ø> (ø)
plugins-mysql 39.10% <25.00%> (-0.20%) ⬇️
plugins-mysql2 39.20% <25.00%> (-0.19%) ⬇️
plugins-node-serialize 27.00% <ø> (ø)
plugins-opensearch 37.52% <25.00%> (-0.18%) ⬇️
plugins-passport-http 26.76% <ø> (ø)
plugins-postgres 35.50% <25.00%> (-0.17%) ⬇️
plugins-process 26.73% <ø> (ø)
plugins-pug 26.96% <ø> (ø)
plugins-redis ?
plugins-router 43.00% <25.00%> (-0.20%) ⬇️
plugins-sequelize 25.55% <ø> (ø)
plugins-test-and-upstream-amqp10 38.41% <25.00%> (-0.19%) ⬇️
plugins-test-and-upstream-amqplib 43.79% <25.00%> (-0.22%) ⬇️
plugins-test-and-upstream-apollo 39.05% <25.00%> (-0.17%) ⬇️
plugins-test-and-upstream-avsc 38.52% <25.00%> (-0.20%) ⬇️
plugins-test-and-upstream-bunyan 33.80% <25.00%> (-0.18%) ⬇️
plugins-test-and-upstream-connect 40.72% <25.00%> (-0.20%) ⬇️
plugins-test-and-upstream-graphql 40.00% <25.00%> (-0.20%) ⬇️
plugins-test-and-upstream-koa ?
plugins-test-and-upstream-protobufjs 38.74% <25.00%> (-0.20%) ⬇️
plugins-test-and-upstream-rhea 43.96% <25.00%> (-0.22%) ⬇️
plugins-undici 39.04% <25.00%> (-0.18%) ⬇️
plugins-url 26.73% <ø> (ø)
plugins-valkey 38.03% <25.00%> (-0.19%) ⬇️
plugins-vm 26.73% <ø> (ø)
plugins-winston 33.99% <25.00%> (-0.17%) ⬇️
plugins-ws 41.80% <25.00%> (-0.20%) ⬇️
profiling-macos 40.45% <25.00%> (-0.20%) ⬇️
profiling-ubuntu 40.58% <25.00%> (-0.20%) ⬇️
profiling-windows 42.15% <70.27%> (-0.17%) ⬇️
serverless-azure-functions-client 25.62% <ø> (ø)
serverless-azure-functions-eventhubs ?
serverless-azure-functions-servicebus ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 17, 2025

Overall package size

Self size: 5.04 MB
Deduped: 5.88 MB
No deduping: 5.88 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.0 | 81.15 kB | 815.98 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-datadog-prod-us1

This comment has been minimized.

@szegedi szegedi force-pushed the szegedi/profiler-sync-start branch from 7b483ee to f503427 Compare June 25, 2025 13:03
@pr-commenter
Copy link
Copy Markdown

pr-commenter Bot commented Jun 25, 2025

Benchmarks

Benchmark execution time: 2026-03-23 13:57:48

Comparing candidate commit adfb0e9 in PR branch szegedi/profiler-sync-start with baseline commit b6d42d8 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 228 metrics, 32 unstable metrics.

@szegedi szegedi force-pushed the szegedi/profiler-sync-start branch from f503427 to ff3352a Compare March 19, 2026 06:27
szegedi and others added 2 commits March 19, 2026 07:30
Replace `await SourceMapper.create()` with a synchronous `new SourceMapper()`
constructor call followed by a fire-and-forget `loadDirectory()`. The mapper
is handed to profilers immediately (with an initially empty `infoMap`);
`#sourceMapCount` is updated in the background `.then()` callback once the
filesystem scan completes.

This removes the only `await` from `_start()`, making it — and the entire
profiler start path — synchronous. `start()` now uses try/catch instead of
`.catch()` chaining. `proxy.js` is updated accordingly: `_profilerStarted`
is stored as a plain boolean and `profilerStarted()` wraps it in
`Promise.resolve()` for backwards compatibility with existing callers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Move zlib/compression setup out of the synchronous startup path into a
lazy `#getCompressionFn()` private method, initialized on first call.
`zlib` and `util.promisify` are now required inside that method rather
than at module load time, keeping the startup path free of I/O-adjacent
module loading.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@szegedi szegedi force-pushed the szegedi/profiler-sync-start branch from ff3352a to 12f9f2f Compare March 19, 2026 06:30
szegedi and others added 2 commits March 19, 2026 07:30
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
With the synchronous profiler startup change, source map loading
(DD_PROFILING_SOURCE_MAP defaults to true) now happens concurrently
with profiling instead of before it. This causes source map directory
scan events (dir.read, promises.opendir, etc.) to appear in the
timeline profile without span IDs, failing the previous assertion
that all events of a given type must have span IDs.

Fix by skipping events without span IDs in gatherTimelineEvents
instead of asserting they must be present. User-initiated operations
(wrapped in tracer.trace()) still have span IDs and are still
collected; the count check continues to verify they are all captured.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@szegedi szegedi changed the title Start profilers synchronously within tracer initialization feat(profiling): Start profilers synchronously within tracer initialization Mar 19, 2026
@szegedi szegedi marked this pull request as ready for review March 19, 2026 13:51
@szegedi szegedi requested a review from a team as a code owner March 19, 2026 13:51
Copy link
Copy Markdown
Contributor

@IlyasShabi IlyasShabi left a comment

Choose a reason for hiding this comment

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

LGTM

@szegedi szegedi requested a review from IlyasShabi March 19, 2026 18:38
Comment thread packages/dd-trace/src/profiling/profiler.js Outdated
Copy link
Copy Markdown
Contributor

@IlyasShabi IlyasShabi left a comment

Choose a reason for hiding this comment

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

🚢

@szegedi szegedi enabled auto-merge (squash) March 23, 2026 13:46
@szegedi szegedi merged commit f429a85 into master Mar 23, 2026
787 checks passed
@szegedi szegedi deleted the szegedi/profiler-sync-start branch March 23, 2026 14:02
dd-octo-sts Bot pushed a commit that referenced this pull request Mar 24, 2026
…zation (#5906)

* feat(profiler): make profiler startup synchronous

Replace `await SourceMapper.create()` with a synchronous `new SourceMapper()`
constructor call followed by a fire-and-forget `loadDirectory()`. The mapper
is handed to profilers immediately (with an initially empty `infoMap`);
`#sourceMapCount` is updated in the background `.then()` callback once the
filesystem scan completes.

This removes the only `await` from `_start()`, making it — and the entire
profiler start path — synchronous. `start()` now uses try/catch instead of
`.catch()` chaining. `proxy.js` is updated accordingly: `_profilerStarted`
is stored as a plain boolean and `profilerStarted()` wraps it in
`Promise.resolve()` for backwards compatibility with existing callers.

We also move zlib/compression setup out of the synchronous startup path into a
lazy `#getCompressionFn()` private method, initialized on first call.
`zlib` and `util.promisify` are now required inside that method rather
than at module load time, keeping the startup path free of I/O-adjacent
module loading.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dd-octo-sts dd-octo-sts Bot mentioned this pull request Mar 24, 2026
tlhunter pushed a commit that referenced this pull request Mar 26, 2026
…zation (#5906)

* feat(profiler): make profiler startup synchronous

Replace `await SourceMapper.create()` with a synchronous `new SourceMapper()`
constructor call followed by a fire-and-forget `loadDirectory()`. The mapper
is handed to profilers immediately (with an initially empty `infoMap`);
`#sourceMapCount` is updated in the background `.then()` callback once the
filesystem scan completes.

This removes the only `await` from `_start()`, making it — and the entire
profiler start path — synchronous. `start()` now uses try/catch instead of
`.catch()` chaining. `proxy.js` is updated accordingly: `_profilerStarted`
is stored as a plain boolean and `profilerStarted()` wraps it in
`Promise.resolve()` for backwards compatibility with existing callers.

We also move zlib/compression setup out of the synchronous startup path into a
lazy `#getCompressionFn()` private method, initialized on first call.
`zlib` and `util.promisify` are now required inside that method rather
than at module load time, keeping the startup path free of I/O-adjacent
module loading.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants