Skip to content

Commit c9552a4

Browse files
authored
ref(node): Refactor LocalVariables integration to avoid setupOnce (#9897)
Slowly getting rid of `getCurrentHub()`...
1 parent 91a6b4e commit c9552a4

File tree

3 files changed

+51
-50
lines changed

3 files changed

+51
-50
lines changed

packages/core/src/baseclient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
115115
/** Number of calls being processed */
116116
protected _numProcessing: number;
117117

118+
protected _eventProcessors: EventProcessor[];
119+
118120
/** Holds flushable */
119121
private _outcomes: { [key: string]: number };
120122

121123
// eslint-disable-next-line @typescript-eslint/ban-types
122124
private _hooks: Record<string, Function[]>;
123125

124-
private _eventProcessors: EventProcessor[];
125-
126126
/**
127127
* Initializes this client instance.
128128
*

packages/node/src/integrations/localvariables.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import type { Event, EventProcessor, Exception, Hub, Integration, StackFrame, StackParser } from '@sentry/types';
33
import { LRUMap, logger } from '@sentry/utils';
44
import type { Debugger, InspectorNotification, Runtime, Session } from 'inspector';
5+
import type { NodeClient } from '../client';
56

67
import { NODE_VERSION } from '../nodeVersion';
7-
import type { NodeClientOptions } from '../types';
88

99
type Variables = Record<string, unknown>;
1010
type OnPauseEvent = InspectorNotification<Debugger.PausedEventDataType>;
@@ -332,6 +332,7 @@ export class LocalVariables implements Integration {
332332

333333
private readonly _cachedFrames: LRUMap<string, FrameVariables[]> = new LRUMap(20);
334334
private _rateLimiter: RateLimitIncrement | undefined;
335+
private _shouldProcessEvent = false;
335336

336337
public constructor(
337338
private readonly _options: Options = {},
@@ -341,16 +342,15 @@ export class LocalVariables implements Integration {
341342
/**
342343
* @inheritDoc
343344
*/
344-
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
345-
this._setup(addGlobalEventProcessor, getCurrentHub().getClient()?.getOptions());
345+
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, _getCurrentHub: () => Hub): void {
346+
// noop
346347
}
347348

348-
/** Setup in a way that's easier to call from tests */
349-
private _setup(
350-
addGlobalEventProcessor: (callback: EventProcessor) => void,
351-
clientOptions: NodeClientOptions | undefined,
352-
): void {
353-
if (this._session && clientOptions?.includeLocalVariables) {
349+
/** @inheritdoc */
350+
public setup(client: NodeClient): void {
351+
const clientOptions = client.getOptions();
352+
353+
if (this._session && clientOptions.includeLocalVariables) {
354354
// Only setup this integration if the Node version is >= v18
355355
// https://github.com/getsentry/sentry-javascript/issues/7697
356356
const unsupportedNodeVersion = (NODE_VERSION.major || 0) < 18;
@@ -386,10 +386,19 @@ export class LocalVariables implements Integration {
386386
);
387387
}
388388

389-
addGlobalEventProcessor(async event => this._addLocalVariables(event));
389+
this._shouldProcessEvent = true;
390390
}
391391
}
392392

393+
/** @inheritdoc */
394+
public processEvent(event: Event): Event {
395+
if (this._shouldProcessEvent) {
396+
return this._addLocalVariables(event);
397+
}
398+
399+
return event;
400+
}
401+
393402
/**
394403
* Handle the pause event
395404
*/

packages/node/test/integrations/localvariables.test.ts

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ClientOptions, EventProcessor } from '@sentry/types';
22
import type { LRUMap } from '@sentry/utils';
33
import type { Debugger, InspectorNotification } from 'inspector';
44

5-
import { defaultStackParser } from '../../src';
5+
import { NodeClient, defaultStackParser } from '../../src';
66
import type { DebugSession, FrameVariables } from '../../src/integrations/localvariables';
77
import { LocalVariables, createCallbackList, createRateLimiter } from '../../src/integrations/localvariables';
88
import { NODE_VERSION } from '../../src/nodeVersion';
@@ -52,7 +52,6 @@ class MockDebugSession implements DebugSession {
5252

5353
interface LocalVariablesPrivate {
5454
_cachedFrames: LRUMap<string, FrameVariables[]>;
55-
_setup(addGlobalEventProcessor: (callback: EventProcessor) => void, clientOptions: ClientOptions): void;
5655
}
5756

5857
const exceptionEvent = {
@@ -154,8 +153,6 @@ const exceptionEvent100Frames = {
154153

155154
describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
156155
it('Adds local variables to stack frames', async () => {
157-
expect.assertions(7);
158-
159156
const session = new MockDebugSession({
160157
'-6224981551105448869.1.2': { name: 'tim' },
161158
'-6224981551105448869.1.6': { arr: [1, 2, 3] },
@@ -164,13 +161,14 @@ describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
164161
const options = getDefaultNodeClientOptions({
165162
stackParser: defaultStackParser,
166163
includeLocalVariables: true,
164+
integrations: [localVariables],
167165
});
168166

169-
let eventProcessor: EventProcessor | undefined;
167+
const client = new NodeClient(options);
168+
client.setupIntegrations(true);
170169

171-
(localVariables as unknown as LocalVariablesPrivate)._setup(callback => {
172-
eventProcessor = callback;
173-
}, options);
170+
const eventProcessors = client['_eventProcessors'];
171+
const eventProcessor = eventProcessors.find(processor => processor.id === 'LocalVariables');
174172

175173
expect(eventProcessor).toBeDefined();
176174

@@ -189,7 +187,7 @@ describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
189187
{ function: 'one', vars: { arr: [1, 2, 3] } },
190188
]);
191189

192-
const event = await eventProcessor?.(
190+
const event = await eventProcessor!(
193191
{
194192
event_id: '9cbf882ade9a415986632ac4e16918eb',
195193
platform: 'node',
@@ -249,22 +247,16 @@ describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
249247
});
250248

251249
it('Only considers the first 5 frames', async () => {
252-
expect.assertions(4);
253-
254250
const session = new MockDebugSession({});
255251
const localVariables = new LocalVariables({}, session);
256252
const options = getDefaultNodeClientOptions({
257253
stackParser: defaultStackParser,
258254
includeLocalVariables: true,
255+
integrations: [localVariables],
259256
});
260257

261-
let eventProcessor: EventProcessor | undefined;
262-
263-
(localVariables as unknown as LocalVariablesPrivate)._setup(callback => {
264-
eventProcessor = callback;
265-
}, options);
266-
267-
expect(eventProcessor).toBeDefined();
258+
const client = new NodeClient(options);
259+
client.setupIntegrations(true);
268260

269261
await session.runPause(exceptionEvent100Frames);
270262

@@ -280,16 +272,16 @@ describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
280272
});
281273

282274
it('Should not lookup variables for non-exception reasons', async () => {
283-
expect.assertions(1);
284-
285275
const session = new MockDebugSession({}, { getLocalVariables: true });
286276
const localVariables = new LocalVariables({}, session);
287277
const options = getDefaultNodeClientOptions({
288278
stackParser: defaultStackParser,
289279
includeLocalVariables: true,
280+
integrations: [localVariables],
290281
});
291282

292-
(localVariables as unknown as LocalVariablesPrivate)._setup(_ => {}, options);
283+
const client = new NodeClient(options);
284+
client.setupIntegrations(true);
293285

294286
const nonExceptionEvent = {
295287
method: exceptionEvent.method,
@@ -302,43 +294,41 @@ describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
302294
});
303295

304296
it('Should not initialize when disabled', async () => {
305-
expect.assertions(1);
306-
307297
const session = new MockDebugSession({}, { configureAndConnect: true });
308298
const localVariables = new LocalVariables({}, session);
309299
const options = getDefaultNodeClientOptions({
310300
stackParser: defaultStackParser,
301+
integrations: [localVariables],
311302
});
312303

313-
let eventProcessor: EventProcessor | undefined;
304+
const client = new NodeClient(options);
305+
client.setupIntegrations(true);
314306

315-
(localVariables as unknown as LocalVariablesPrivate)._setup(callback => {
316-
eventProcessor = callback;
317-
}, options);
307+
const eventProcessors = client['_eventProcessors'];
308+
const eventProcessor = eventProcessors.find(processor => processor.id === 'LocalVariables');
318309

319-
expect(eventProcessor).toBeUndefined();
310+
expect(eventProcessor).toBeDefined();
311+
expect(localVariables['_shouldProcessEvent']).toBe(false);
320312
});
321313

322314
it('Should not initialize when inspector not loaded', async () => {
323-
expect.assertions(1);
324-
325315
const localVariables = new LocalVariables({}, undefined);
326316
const options = getDefaultNodeClientOptions({
327317
stackParser: defaultStackParser,
318+
integrations: [localVariables],
328319
});
329320

330-
let eventProcessor: EventProcessor | undefined;
321+
const client = new NodeClient(options);
322+
client.setupIntegrations(true);
331323

332-
(localVariables as unknown as LocalVariablesPrivate)._setup(callback => {
333-
eventProcessor = callback;
334-
}, options);
324+
const eventProcessors = client['_eventProcessors'];
325+
const eventProcessor = eventProcessors.find(processor => processor.id === 'LocalVariables');
335326

336-
expect(eventProcessor).toBeUndefined();
327+
expect(eventProcessor).toBeDefined();
328+
expect(localVariables['_shouldProcessEvent']).toBe(false);
337329
});
338330

339331
it('Should cache identical uncaught exception events', async () => {
340-
expect.assertions(1);
341-
342332
const session = new MockDebugSession({
343333
'-6224981551105448869.1.2': { name: 'tim' },
344334
'-6224981551105448869.1.6': { arr: [1, 2, 3] },
@@ -347,9 +337,11 @@ describeIf((NODE_VERSION.major || 0) >= 18)('LocalVariables', () => {
347337
const options = getDefaultNodeClientOptions({
348338
stackParser: defaultStackParser,
349339
includeLocalVariables: true,
340+
integrations: [localVariables],
350341
});
351342

352-
(localVariables as unknown as LocalVariablesPrivate)._setup(_ => {}, options);
343+
const client = new NodeClient(options);
344+
client.setupIntegrations(true);
353345

354346
await session.runPause(exceptionEvent);
355347
await session.runPause(exceptionEvent);

0 commit comments

Comments
 (0)