-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add helpers to get module metadata from injected code (#8438
- Loading branch information
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type { Event, StackParser } from '@sentry/types'; | ||
import { GLOBAL_OBJ } from '@sentry/utils'; | ||
|
||
/** Keys are source filename/url, values are metadata objects. */ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const filenameMetadataMap = new Map<string, any>(); | ||
/** Set of stack strings that have already been parsed. */ | ||
const parsedStacks = new Set<string>(); | ||
|
||
function ensureMetadataStacksAreParsed(parser: StackParser): void { | ||
if (!GLOBAL_OBJ._sentryModuleMetadata) { | ||
return; | ||
} | ||
|
||
for (const stack of Object.keys(GLOBAL_OBJ._sentryModuleMetadata)) { | ||
const metadata = GLOBAL_OBJ._sentryModuleMetadata[stack]; | ||
|
||
if (parsedStacks.has(stack)) { | ||
continue; | ||
} | ||
|
||
// Ensure this stack doesn't get parsed again | ||
parsedStacks.add(stack); | ||
|
||
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) { | ||
// Save the metadata for this filename | ||
filenameMetadataMap.set(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. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function getMetadataForUrl(parser: StackParser, filename: string): any | undefined { | ||
ensureMetadataStacksAreParsed(parser); | ||
return filenameMetadataMap.get(filename); | ||
} | ||
|
||
/** | ||
* 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 { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
event.exception!.values!.forEach(exception => { | ||
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; | ||
} | ||
} | ||
}); | ||
} catch (_) { | ||
// To save bundle size we're just try catching here instead of checking for the existence of all the different objects. | ||
} | ||
} | ||
|
||
/** | ||
* Strips metadata from stack frames. | ||
*/ | ||
export function stripMetadataFromStackFrames(event: Event): void { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
event.exception!.values!.forEach(exception => { | ||
if (!exception.stacktrace) { | ||
return; | ||
} | ||
|
||
for (const frame of exception.stacktrace.frames || []) { | ||
delete frame.module_metadata; | ||
} | ||
}); | ||
} catch (_) { | ||
// To save bundle size we're just try catching here instead of checking for the existence of all the different objects. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type { Event } from '@sentry/types'; | ||
import { createStackParser, GLOBAL_OBJ, nodeStackLineParser } from '@sentry/utils'; | ||
|
||
import { addMetadataToStackFrames, getMetadataForUrl, stripMetadataFromStackFrames } from '../../src/metadata'; | ||
|
||
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._sentryModuleMetadata = GLOBAL_OBJ._sentryModuleMetadata || {}; | ||
GLOBAL_OBJ._sentryModuleMetadata[stack] = { team: 'frontend' }; | ||
}); | ||
|
||
it('is parsed', () => { | ||
const metadata = getMetadataForUrl(parser, __filename); | ||
|
||
expect(metadata).toEqual({ team: 'frontend' }); | ||
}); | ||
|
||
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', | ||
}, | ||
}, | ||
]); | ||
|
||
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, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters