Skip to content

feat: Add source mapping to CPU (wall time) profiler - #4095

Merged
amychisholm03 merged 4 commits into
newrelic:mainfrom
amychisholm03:NR-4083/source-mapping
Jul 9, 2026
Merged

feat: Add source mapping to CPU (wall time) profiler#4095
amychisholm03 merged 4 commits into
newrelic:mainfrom
amychisholm03:NR-4083/source-mapping

Conversation

@amychisholm03

@amychisholm03 amychisholm03 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds source code mapping* support for our current CpuProfiler. When enabled via the profiling.source_mapping.enabled config option, a pprof.SourceMapper is built once during agent startup by scanning the application root (process.cwd()) for source maps. It's cached on the ProfilingManager and passed into the CpuProfiler's constructor when the profilers are registered. It builds it once at startup, rather than on each profiling cycle, to keep the profiling start/stop path synchronous and avoids re-scanning on every collection. Caching it on the manager also lets the same SourceMapper be shared with the HeapProfiler when we add heap source mapping later.

If the SourceMapper fails to build (or no source maps are found), the profiler falls back to its original behavior and reports the compiled file/line/code information.

*Source code mapping refers to mapping the profiling sample to the pre-compiled code e.g. TypeScript, pre-compiled JavaScript. The goal is that users will be able to more easily correlate the node in the flamegraph with the function information (file location, code line number, etc.) in their written code instead of the compiled version.

How to Test

npm run integration
npm run unit

# or more specifically
node --test test/integration/profiling/source-mapping.test.js
node --test test/unit/aggregators/profiling-aggregator.test.js
node --test test/unit/lib/profiling/profilers/cpu.test.js
node --test test/unit/lib/profiling/index.test.js

With the example app: newrelic/newrelic-node-examples#378

Related Issues

Closes #4083

@amychisholm03
amychisholm03 force-pushed the NR-4083/source-mapping branch 2 times, most recently from f3cf356 to 269100d Compare July 6, 2026 20:39
@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.40%. Comparing base (b22c25b) to head (8ab2ce5).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4095      +/-   ##
==========================================
- Coverage   97.48%   97.40%   -0.08%     
==========================================
  Files         517      517              
  Lines       62214    62270      +56     
  Branches        1        1              
==========================================
+ Hits        60647    60654       +7     
- Misses       1567     1616      +49     
Flag Coverage Δ
integration-tests-cjs-22.x 73.70% <95.08%> (+0.07%) ⬆️
integration-tests-cjs-24.x 74.28% <95.08%> (+0.09%) ⬆️
integration-tests-cjs-26.x 74.28% <95.08%> (+0.09%) ⬆️
integration-tests-esm-22.x 54.21% <38.46%> (-0.02%) ⬇️
integration-tests-esm-24.x 55.40% <38.46%> (-0.02%) ⬇️
integration-tests-esm-26.x 55.40% <38.46%> (-0.02%) ⬇️
unit-tests-22.x 89.06% <100.00%> (+0.01%) ⬆️
unit-tests-24.x 89.00% <100.00%> (+0.01%) ⬆️
unit-tests-26.x 89.00% <100.00%> (+0.01%) ⬆️
versioned-tests-22.x 97.40% <100.00%> (-0.08%) ⬇️
versioned-tests-24.x 97.40% <100.00%> (-0.08%) ⬇️
versioned-tests-26.x 97.40% <100.00%> (-0.08%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 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.

@amychisholm03
amychisholm03 marked this pull request as ready for review July 6, 2026 20:58
@amychisholm03
amychisholm03 force-pushed the NR-4083/source-mapping branch from 269100d to b81bc00 Compare July 7, 2026 15:20
@amychisholm03
amychisholm03 requested a review from bizob2828 July 8, 2026 16:32

@bizob2828 bizob2828 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we could land this in the current state based on my comments

Comment thread lib/aggregators/profiling-aggregator.js Outdated
Comment thread lib/profiling/index.js Outdated
Comment thread test/unit/agent/agent.test.js Outdated
Comment thread lib/profiling/index.js Outdated
@amychisholm03
amychisholm03 force-pushed the NR-4083/source-mapping branch 4 times, most recently from 4a59a16 to 4c35d38 Compare July 8, 2026 20:37
@amychisholm03
amychisholm03 requested a review from bizob2828 July 8, 2026 20:50

@bizob2828 bizob2828 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

tried using newrelic/newrelic-node-examples#378 but i don't see any samples that have user code in it. it's all either the agent or core node code. either way take a look at my suggestions

Comment thread lib/agent.js Outdated
@amychisholm03
amychisholm03 requested a review from bizob2828 July 9, 2026 15:34
@amychisholm03
amychisholm03 force-pushed the NR-4083/source-mapping branch from 39b9abc to 50cb745 Compare July 9, 2026 17:22

@bizob2828 bizob2828 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

a few tests suggestions/questions but the code looks great!

Comment thread test/unit/agent/agent.test.js Outdated
started = true
})

await new Promise((resolve) => setImmediate(resolve))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what are these for?

Comment thread test/unit/lib/profiling/profilers/cpu.test.js Outdated
@amychisholm03
amychisholm03 requested a review from bizob2828 July 9, 2026 19:41
@amychisholm03
amychisholm03 merged commit f6123bc into newrelic:main Jul 9, 2026
100 of 102 checks passed
@github-project-automation github-project-automation Bot moved this from Needs PR Review to Done: Issues recently completed in Node.js Engineering Board Jul 9, 2026
@github-actions github-actions Bot mentioned this pull request Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done: Issues recently completed

Development

Successfully merging this pull request may close these issues.

Add source map support to CpuProfiler so flamegraph frames map to original source

3 participants