-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add helpers to get module metadata from injected code #8438
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b005d08
feat(core): Add functions to get module metadata from injected code
timfish a6755b5
Fix linting
timfish 382f593
We don't need to two globals!
timfish f1b24fa
Merge branch 'develop' into feat/module-metadata
timfish 8707a8c
PR review changes
timfish fde5865
Try catching!
timfish 0748186
`module_metadata` type
timfish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,87 @@ | ||
import type { Event, StackFrame, StackParser } from '@sentry/types'; | ||
import { GLOBAL_OBJ } from '@sentry/utils'; | ||
|
||
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, 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) { | ||
// Save the metadata for this filename | ||
GLOBAL_OBJ.__MODULE_METADATA_PARSED__ = GLOBAL_OBJ.__MODULE_METADATA_PARSED__ || {}; | ||
GLOBAL_OBJ.__MODULE_METADATA_PARSED__[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); | ||
|
||
const metadataObj = GLOBAL_OBJ.__MODULE_METADATA_PARSED__ || {}; | ||
|
||
return metadataObj[url]; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
}); | ||
} |
This file contains hidden or 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,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, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.