Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"license": "Apache-2.0",
"dependencies": {
"node-gyp-build": "^4.8.4",
"pprof-format": "^2.3.2",
"pprof-format": "^2.4.0",
"source-map": "^0.8.0"
},
"devDependencies": {
Expand Down
35 changes: 34 additions & 1 deletion ts/src/heap-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ import {
DEFAULT_COLUMN_NUMBERS,
serializeHeapProfile,
} from './profile-serializer';
import {NormalizedProfile, normalizeHeapProfile} from './profile-normalizer';
import {
HeapProfileNormalizerOptions,
NormalizedProfile,
normalizeHeapProfile,
ProfileVisitor,
visitHeapProfile,
VisitResult,
} from './profile-normalizer';
import {SourceMapper} from './sourcemapper/sourcemapper';
import {
AllocationProfileNode,
Expand Down Expand Up @@ -129,6 +136,32 @@ export function normalizedProfile(
});
}

/**
* Collects a profile and passes it to the visitor, normalized, instead of
* serializing it as pprof, so the caller chooses the output (e.g. a visitor of
* an OtelProfilesBuilder). Throws if heap profiler is not enabled.
*/
export function visitProfile(
visitor: ProfileVisitor,
options: Pick<
HeapProfileNormalizerOptions,
'ignoreSamplesPath' | 'sourceMapper' | 'generateLabels'
> = {},
): VisitResult {
return collectProfile(root =>
visitHeapProfile(
addExternalMemoryNode(root, startedWithAllocations),
visitor,
{
...options,
startTimeNanos: BigInt(Date.now()) * 1_000_000n,
intervalBytes: heapIntervalBytes,
allocations: startedWithAllocations,
},
),
);
}

function collectProfile<T>(
processProfile: (
root: AllocationProfileNode | AllocationProfileNodeWithStats,
Expand Down
2 changes: 2 additions & 0 deletions ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const time = {
start: timeProfiler.start,
stop: timeProfiler.stop,
stopNormalized: timeProfiler.stopNormalized,
stopAndVisit: timeProfiler.stopAndVisit,
profileV2: timeProfiler.profileV2,
getContext: timeProfiler.getContext,
setContext: timeProfiler.setContext,
Expand All @@ -75,6 +76,7 @@ export const heap = {
profile: heapProfiler.profile,
profileV2: heapProfiler.profileV2,
normalizedProfile: heapProfiler.normalizedProfile,
visitProfile: heapProfiler.visitProfile,
convertProfile: heapProfiler.convertProfile,
v8Profile: heapProfiler.v8Profile,
monitorOutOfMemory: heapProfiler.monitorOutOfMemory,
Expand Down
Loading
Loading