Skip to content

Commit d402354

Browse files
committed
changes from review
1 parent b847980 commit d402354

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

packages/node/src/integrations/localvariables.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { getCurrentHub } from '@sentry/core';
2-
import { Event, EventProcessor, Integration, StackFrame, StackParser } from '@sentry/types';
2+
import { Event, EventProcessor, Exception, Integration, StackFrame, StackParser } from '@sentry/types';
33
import { Debugger, InspectorNotification, Runtime, Session } from 'inspector';
44
import { LRUMap } from 'lru_map';
55

6-
import { NodeClient } from '../client';
7-
86
/**
97
* Promise API is available as `Experimental` and in Node 19 only.
108
*
@@ -15,7 +13,7 @@ import { NodeClient } from '../client';
1513
* https://nodejs.org/docs/latest-v14.x/api/inspector.html
1614
*/
1715
class AsyncSession extends Session {
18-
public async getProperties(objectId: string): Promise<Runtime.PropertyDescriptor[]> {
16+
public getProperties(objectId: string): Promise<Runtime.PropertyDescriptor[]> {
1917
return new Promise((resolve, reject) => {
2018
this.post(
2119
'Runtime.getProperties',
@@ -59,7 +57,8 @@ function hashFrames(frames: StackFrame[] | undefined): string | undefined {
5957
return;
6058
}
6159

62-
return frames.reduce((acc, frame) => `${acc},${frame.function},${frame.lineno},${frame.colno}`, '');
60+
// Only hash the 10 most recent frames (ie. the last 10)
61+
return frames.slice(-10).reduce((acc, frame) => `${acc},${frame.function},${frame.lineno},${frame.colno}`, '');
6362
}
6463

6564
interface FrameVariables {
@@ -76,14 +75,14 @@ export class LocalVariables implements Integration {
7675
public readonly name: string = LocalVariables.id;
7776

7877
private readonly _session: AsyncSession = new AsyncSession();
79-
private readonly _cachedFrames: LRUMap<string, Promise<FrameVariables[]>> = new LRUMap(50);
78+
private readonly _cachedFrames: LRUMap<string, Promise<FrameVariables[]>> = new LRUMap(20);
8079
private _stackParser: StackParser | undefined;
8180

8281
/**
8382
* @inheritDoc
8483
*/
8584
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void {
86-
const options = getCurrentHub().getClient<NodeClient>()?.getOptions();
85+
const options = getCurrentHub().getClient()?.getOptions();
8786

8887
if (options?._experiments?.includeStackLocals) {
8988
this._stackParser = options.stackParser;
@@ -130,7 +129,7 @@ export class LocalVariables implements Integration {
130129
const framePromises = callFrames.map(async ({ scopeChain, functionName, this: obj }) => {
131130
const localScope = scopeChain.find(scope => scope.type === 'local');
132131

133-
const fn = obj.className !== 'global' ? `${obj.className}.${functionName}` : functionName;
132+
const fn = obj.className === 'global' ? functionName : `${obj.className}.${functionName}`;
134133

135134
if (localScope?.object.objectId === undefined) {
136135
return { function: fn };
@@ -190,47 +189,58 @@ export class LocalVariables implements Integration {
190189
}
191190

192191
/**
193-
* Adds local variables to the exception stack trace frames.
192+
* Adds local variables event stack frames.
194193
*/
195194
private async _addLocalVariables(event: Event): Promise<Event> {
196-
const hash = hashFrames(event?.exception?.values?.[0]?.stacktrace?.frames);
195+
for (const exception of event?.exception?.values || []) {
196+
await this._addLocalVariablesToException(exception);
197+
}
198+
199+
return event;
200+
}
201+
202+
/**
203+
* Adds local variables to the exception stack frames.
204+
*/
205+
private async _addLocalVariablesToException(exception: Exception): Promise<void> {
206+
const hash = hashFrames(exception?.stacktrace?.frames);
197207

198208
if (hash === undefined) {
199-
return event;
209+
return;
200210
}
201211

202212
// Check if we have local variables for an exception that matches the hash
203213
const cachedFrames = await this._cachedFrames.get(hash);
204214

205215
if (cachedFrames === undefined) {
206-
return event;
216+
return;
207217
}
208218

209-
const frameCount = event?.exception?.values?.[0]?.stacktrace?.frames?.length || 0;
219+
await this._cachedFrames.delete(hash);
220+
221+
const frameCount = exception.stacktrace?.frames?.length || 0;
210222

211223
for (let i = 0; i < frameCount; i++) {
212224
// Sentry frames are in reverse order
213225
const frameIndex = frameCount - i - 1;
214226

215227
// Drop out if we run out of frames to match up
216-
if (!event?.exception?.values?.[0]?.stacktrace?.frames?.[frameIndex] || !cachedFrames[i]) {
228+
if (!exception?.stacktrace?.frames?.[frameIndex] || !cachedFrames[i]) {
217229
break;
218230
}
219231

220232
if (
221233
// We need to have vars to add
222234
cachedFrames[i].vars === undefined ||
223235
// We're not interested in frames that are not in_app because the vars are not relevant
224-
event.exception.values[0].stacktrace.frames[frameIndex].in_app === false ||
236+
exception.stacktrace.frames[frameIndex].in_app === false ||
225237
// The function names need to match
226-
!functionNamesMatch(event.exception.values[0].stacktrace.frames[frameIndex].function, cachedFrames[i].function)
238+
!functionNamesMatch(exception.stacktrace.frames[frameIndex].function, cachedFrames[i].function)
227239
) {
228240
continue;
229241
}
230242

231-
event.exception.values[0].stacktrace.frames[frameIndex].vars = cachedFrames[i].vars;
243+
exception.stacktrace.frames[frameIndex].vars = cachedFrames[i].vars;
232244
}
233-
234-
return event;
235245
}
236246
}

0 commit comments

Comments
 (0)