Skip to content

Commit

Permalink
feat(node): Add LocalVariables integration to capture local variabl…
Browse files Browse the repository at this point in the history
…es to stack frames (#6478)
  • Loading branch information
timfish committed Jan 9, 2023
1 parent f052394 commit 57c7e7b
Show file tree
Hide file tree
Showing 8 changed files with 4,032 additions and 0 deletions.
@@ -0,0 +1,35 @@
/* eslint-disable no-unused-vars */
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
_experiments: { includeStackLocals: true },
beforeSend: event => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(event));
},
});

process.on('uncaughtException', () => {
// do nothing - this will prevent the Error below from closing this process
});

class Some {
two(name) {
throw new Error('Enough!');
}
}

function one(name) {
const arr = [1, '2', null];
const obj = {
name,
num: 5,
};

const ty = new Some();

ty.two(name);
}

one('some name');
@@ -0,0 +1,34 @@
/* eslint-disable no-unused-vars */
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
beforeSend: event => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(event));
},
});

process.on('uncaughtException', () => {
// do nothing - this will prevent the Error below from closing this process
});

class Some {
two(name) {
throw new Error('Enough!');
}
}

function one(name) {
const arr = [1, '2', null];
const obj = {
name,
num: 5,
};

const ty = new Some();

ty.two(name);
}

one('some name');
@@ -0,0 +1,54 @@
import { Event } from '@sentry/node';
import * as childProcess from 'child_process';
import * as path from 'path';

describe('LocalVariables integration', () => {
test('Should not include local variables by default', done => {
expect.assertions(2);

const testScriptPath = path.resolve(__dirname, 'no-local-variables.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
const event = JSON.parse(stdout) as Event;

const frames = event.exception?.values?.[0].stacktrace?.frames || [];
const lastFrame = frames[frames.length - 1];

expect(lastFrame.vars).toBeUndefined();

const penultimateFrame = frames[frames.length - 2];

expect(penultimateFrame.vars).toBeUndefined();

done();
});
});

test('Should include local variables when enabled', done => {
expect.assertions(4);

const testScriptPath = path.resolve(__dirname, 'local-variables.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
const event = JSON.parse(stdout) as Event;

const frames = event.exception?.values?.[0].stacktrace?.frames || [];
const lastFrame = frames[frames.length - 1];

expect(lastFrame.function).toBe('Some.two');
expect(lastFrame.vars).toEqual({ name: 'some name' });

const penultimateFrame = frames[frames.length - 2];

expect(penultimateFrame.function).toBe('one');
expect(penultimateFrame.vars).toEqual({
name: 'some name',
arr: [1, '2', null],
obj: { name: 'some name', num: 5 },
ty: '<Some>',
});

done();
});
});
});
1 change: 1 addition & 0 deletions packages/node/src/integrations/index.ts
Expand Up @@ -7,3 +7,4 @@ export { Modules } from './modules';
export { ContextLines } from './contextlines';
export { Context } from './context';
export { RequestData } from './requestdata';
export { LocalVariables } from './localvariables';

0 comments on commit 57c7e7b

Please sign in to comment.