Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Cache processed stacks for debug IDs #7825

Merged
merged 2 commits into from
Apr 14, 2023
Merged
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
23 changes: 21 additions & 2 deletions packages/core/src/utils/prepareEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ClientOptions, Event, EventHint, StackParser } from '@sentry/types';
import type { ClientOptions, Event, EventHint, StackFrame, StackParser } from '@sentry/types';
import { dateTimestampInSeconds, GLOBAL_OBJ, normalize, resolvedSyncPromise, truncate, uuid4 } from '@sentry/utils';

import { DEFAULT_ENVIRONMENT } from '../constants';
Expand Down Expand Up @@ -114,6 +114,8 @@ function applyClientOptions(event: Event, options: ClientOptions): void {
}
}

const debugIdStackParserCache = new WeakMap<StackParser, Map<string, StackFrame[]>>();

/**
* Applies debug metadata images to the event in order to apply source maps by looking up their debug ID.
*/
Expand All @@ -124,9 +126,26 @@ export function applyDebugMetadata(event: Event, stackParser: StackParser): void
return;
}

let debugIdStackFramesCache: Map<string, StackFrame[]>;
const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser);
if (cachedDebugIdStackFrameCache) {
debugIdStackFramesCache = cachedDebugIdStackFrameCache;
} else {
debugIdStackFramesCache = new Map<string, StackFrame[]>();
debugIdStackParserCache.set(stackParser, debugIdStackFramesCache);
}

// Build a map of filename -> debug_id
const filenameDebugIdMap = Object.keys(debugIdMap).reduce<Record<string, string>>((acc, debugIdStackTrace) => {
const parsedStack = stackParser(debugIdStackTrace);
let parsedStack: StackFrame[];
const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace);
if (cachedParsedStack) {
parsedStack = cachedParsedStack;
} else {
parsedStack = stackParser(debugIdStackTrace);
debugIdStackFramesCache.set(debugIdStackTrace, parsedStack);
}

for (let i = parsedStack.length - 1; i >= 0; i--) {
const stackFrame = parsedStack[i];
if (stackFrame.filename) {
Expand Down