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): Add helpers to get module metadata from injected code #8438

Merged
merged 7 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export { prepareEvent } from './utils/prepareEvent';
export { createCheckInEnvelope } from './checkin';
export { hasTracingEnabled } from './utils/hasTracingEnabled';
export { DEFAULT_ENVIRONMENT } from './constants';
export { getMetadataForUrl, addMetadataToStackFrames, stripMetadataFromStackFrames } from './metadata';
timfish marked this conversation as resolved.
Show resolved Hide resolved

import * as Integrations from './integrations';

Expand Down
89 changes: 89 additions & 0 deletions packages/core/src/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { Event, StackParser } from '@sentry/types';
import { GLOBAL_OBJ } from '@sentry/utils';

/**
* Keys are source filenames/urls, values are metadata objects.
*/
let filenameMetadataMap: Record<string, object> | undefined;
timfish marked this conversation as resolved.
Show resolved Hide resolved

function ensureMetadataStacksAreParsed(parser: StackParser): void {
if (!GLOBAL_OBJ.__MODULE_METADATA__) {
return;
}

for (const stack of Object.keys(GLOBAL_OBJ.__MODULE_METADATA__)) {
const metadata = GLOBAL_OBJ.__MODULE_METADATA__[stack];

// If this stack has already been parsed, we can skip it
if (metadata == false) {
continue;
}

// Ensure this stack doesn't get parsed again
GLOBAL_OBJ.__MODULE_METADATA__[stack] = false;

const frames = parser(stack);

// Go through the frames starting from the top of the stack and find the first one with a filename
for (const frame of frames.reverse()) {
if (frame.filename) {
filenameMetadataMap = filenameMetadataMap || {};
// Save the metadata for this filename
filenameMetadataMap[frame.filename] = metadata;
break;
}
}
}
}

/**
* Retrieve metadata for a specific JavaScript file URL.
*
* Metadata is injected by the Sentry bundler plugins using the `_experiments.moduleMetadata` config option.
*/
export function getMetadataForUrl(parser: StackParser, url: string): object | undefined {
ensureMetadataStacksAreParsed(parser);
return filenameMetadataMap ? filenameMetadataMap[url] : undefined;
}

/**
* Adds metadata to stack frames.
*
* Metadata is injected by the Sentry bundler plugins using the `_experiments.moduleMetadata` config option.
*/
export function addMetadataToStackFrames(parser: StackParser, event: Event): void {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
event.exception!.values!.forEach(exception => {
timfish marked this conversation as resolved.
Show resolved Hide resolved
if (!exception.stacktrace) {
return;
}

for (const frame of exception.stacktrace.frames || []) {
if (!frame.filename) {
continue;
}

const metadata = getMetadataForUrl(parser, frame.filename);

if (metadata) {
frame.module_metadata = metadata;
}
}
});
}

/**
* Strips metadata from stack frames.
*/
export function stripMetadataFromStackFrames(event: Event): void {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
event.exception!.values!.forEach(exception => {
timfish marked this conversation as resolved.
Show resolved Hide resolved
if (!exception.stacktrace) {
return;
}

for (const frame of exception.stacktrace.frames || []) {
delete frame.module_metadata;
}
});
}
102 changes: 102 additions & 0 deletions packages/core/test/lib/metadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import type { Event } from '@sentry/types';
import { createStackParser, GLOBAL_OBJ, nodeStackLineParser } from '@sentry/utils';

import { addMetadataToStackFrames, getMetadataForUrl, stripMetadataFromStackFrames } from '../../src';

const parser = createStackParser(nodeStackLineParser());

const stack = new Error().stack || '';

const event: Event = {
exception: {
values: [
{
stacktrace: {
frames: [
{
filename: '<anonymous>',
function: 'new Promise',
},
{
filename: '/tmp/utils.js',
function: 'Promise.then.completed',
lineno: 391,
colno: 28,
},
{
filename: __filename,
function: 'Object.<anonymous>',
lineno: 9,
colno: 19,
},
],
},
},
],
},
};

describe('Metadata', () => {
beforeEach(() => {
GLOBAL_OBJ.__MODULE_METADATA__ = GLOBAL_OBJ.__MODULE_METADATA__ || {};
GLOBAL_OBJ.__MODULE_METADATA__[stack] = { team: 'frontend' };
});

it('is parsed', () => {
const metadata = getMetadataForUrl(parser, __filename);

expect(metadata).toEqual({ team: 'frontend' });
// should now be false so it doesn't get parsed again
expect(GLOBAL_OBJ.__MODULE_METADATA__?.[stack]).toBe(false);
});

it('is added and stripped from stack frames', () => {
addMetadataToStackFrames(parser, event);

expect(event.exception?.values?.[0].stacktrace?.frames).toEqual([
{
filename: '<anonymous>',
function: 'new Promise',
},
{
filename: '/tmp/utils.js',
function: 'Promise.then.completed',
lineno: 391,
colno: 28,
},
{
filename: __filename,
function: 'Object.<anonymous>',
lineno: 9,
colno: 19,
module_metadata: {
team: 'frontend',
},
},
]);

// should now be false so it doesn't get parsed again
expect(GLOBAL_OBJ.__MODULE_METADATA__?.[stack]).toBe(false);

stripMetadataFromStackFrames(event);

expect(event.exception?.values?.[0].stacktrace?.frames).toEqual([
{
filename: '<anonymous>',
function: 'new Promise',
},
{
filename: '/tmp/utils.js',
function: 'Promise.then.completed',
lineno: 391,
colno: 28,
},
{
filename: __filename,
function: 'Object.<anonymous>',
lineno: 9,
colno: 19,
},
]);
});
});
1 change: 1 addition & 0 deletions packages/types/src/stackframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface StackFrame {
addr_mode?: string;
vars?: { [key: string]: any };
debug_id?: string;
module_metadata?: object;
timfish marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 7 additions & 0 deletions packages/utils/src/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export interface InternalGlobal {
[key: string]: Function;
};
};
/**
* Raw module metadata that is injected by bundler plugins.
*
* Keys are `error.stack` strings, values are metadata objects.
* Once the stack has been parsed, the value is set to `false` so they do not get parsed multiple times.
*/
__MODULE_METADATA__?: Record<string, object | false>;
timfish marked this conversation as resolved.
Show resolved Hide resolved
}
timfish marked this conversation as resolved.
Show resolved Hide resolved

// The code below for 'isGlobalObj' and 'GLOBAL_OBJ' was copied from core-js before modification
Expand Down