-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ref(node): Refactor node source fetching into integration #3729
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
AbhiPrasad
merged 45 commits into
getsentry:master
from
timfish:feat/separate-source-reading
Feb 23, 2022
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
31935f0
node stackwalk for Electron
timfish 781312e
Merge branch 'master' into feat/node-stackwalk-electron
timfish f143c10
Re-order params
timfish c890809
Merge branch 'master' into feat/node-stackwalk-electron
timfish 8b30a9a
Move source loading to seperate file
timfish f1e3f31
First try
timfish 6a03a2c
Improve logic
timfish 7c164dd
Revert tslib change
timfish f362589
jsdoc
timfish da256ee
Merge branch 'master' into feat/separate-source-reading
timfish 04db3fb
Merge branch 'master' into feat/separate-source-reading
timfish 3324578
With integration
timfish 443ea06
oops
timfish dcd287d
Merge branch 'master' into feat/separate-source-reading
timfish 84155de
Make this a non-breaking change by keeping NodeOptions.frameContextLines
timfish 285a643
Merge branch 'master' into feat/separate-source-reading
timfish 6a8a48b
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
timfish c4f48ad
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
timfish 7f5793a
Merge branch 'master' into feat/separate-source-reading
timfish ac4318e
Correctly handle zero lines of context
timfish 82573f4
Merge remote-tracking branch 'origin/feat/separate-source-reading' in…
timfish 86e2d4e
Make async
timfish 2ff7efd
Merge branch 'master' into feat/separate-source-reading
timfish 012eff3
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
timfish 647999d
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
timfish 2fe44fc
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
timfish af9b1d8
No longer a breaking change
timfish 270e13e
Fix linting
timfish 76d8998
Merge branch 'master' into feat/separate-source-reading
timfish ddb8406
Add docs and `@deprecated`
timfish 83f859b
Disable warning for internal usage of deprecated field
timfish 8bc20b0
Revert promise changes
timfish 18c5780
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
timfish 2266ac5
Minor improve
timfish bb37b6c
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
timfish 03cb320
Fix nextjs test
timfish 965d48f
revert
timfish e08bcbb
Fix nextjs test
timfish 7cd79af
Merge branch 'feat/separate-source-reading' of https://github.com/tim…
timfish f278e8b
Code review changes!
timfish a7d61df
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
timfish ef7d423
Abhi code review
timfish a172765
Revert promisify
timfish bd85ce9
Add TODO comment
timfish d7952ea
Merge remote-tracking branch 'upstream/master' into feat/separate-sou…
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { BaseClient } from '@sentry/core'; | ||
import { RewriteFrames } from '@sentry/integrations'; | ||
import * as SentryNode from '@sentry/node'; | ||
import { getCurrentHub, NodeClient } from '@sentry/node'; | ||
|
@@ -17,7 +16,6 @@ const global = getGlobalObject(); | |
(global as typeof global & { __rewriteFramesDistDir__: string }).__rewriteFramesDistDir__ = '.next'; | ||
|
||
const nodeInit = jest.spyOn(SentryNode, 'init'); | ||
const captureEvent = jest.spyOn(BaseClient.prototype, 'captureEvent'); | ||
const logError = jest.spyOn(logger, 'error'); | ||
|
||
describe('Server init()', () => { | ||
|
@@ -91,7 +89,7 @@ describe('Server init()', () => { | |
expect(currentScope._tags.vercel).toBeUndefined(); | ||
}); | ||
|
||
it('adds 404 transaction filter', () => { | ||
it('adds 404 transaction filter', async () => { | ||
init({ | ||
dsn: 'https://[email protected]/12312012', | ||
tracesSampleRate: 1.0, | ||
|
@@ -102,8 +100,10 @@ describe('Server init()', () => { | |
const transaction = hub.startTransaction({ name: '/404' }); | ||
transaction.finish(); | ||
|
||
// We need to flush because the event processor pipeline is async whereas transaction.finish() is sync. | ||
await SentryNode.flush(); | ||
|
||
expect(sendEvent).not.toHaveBeenCalled(); | ||
expect(captureEvent.mock.results[0].value).toBeUndefined(); | ||
expect(logError).toHaveBeenCalledWith(new SentryError('An event processor returned null, will not send event.')); | ||
}); | ||
|
||
|
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
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,140 @@ | ||
import { getCurrentHub } from '@sentry/core'; | ||
import { Event, EventProcessor, Integration } from '@sentry/types'; | ||
import { addContextToFrame } from '@sentry/utils'; | ||
import { readFile } from 'fs'; | ||
import { LRUMap } from 'lru_map'; | ||
|
||
import { NodeClient } from '../client'; | ||
|
||
const FILE_CONTENT_CACHE = new LRUMap<string, string | null>(100); | ||
const DEFAULT_LINES_OF_CONTEXT = 7; | ||
|
||
// TODO: Replace with promisify when minimum supported node >= v8 | ||
function readTextFileAsync(path: string): Promise<string> { | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new Promise((resolve, reject) => { | ||
readFile(path, 'utf8', (err, data) => { | ||
if (err) reject(err); | ||
else resolve(data); | ||
}); | ||
}); | ||
} | ||
AbhiPrasad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Resets the file cache. Exists for testing purposes. | ||
* @hidden | ||
*/ | ||
export function resetFileContentCache(): void { | ||
FILE_CONTENT_CACHE.clear(); | ||
} | ||
|
||
interface ContextLinesOptions { | ||
/** | ||
* Sets the number of context lines for each frame when loading a file. | ||
* Defaults to 7. | ||
* | ||
* Set to 0 to disable loading and inclusion of source files. | ||
**/ | ||
frameContextLines?: number; | ||
} | ||
|
||
/** Add node modules / packages to the event */ | ||
export class ContextLines implements Integration { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'ContextLines'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public name: string = ContextLines.id; | ||
|
||
public constructor(private readonly _options: ContextLinesOptions = {}) {} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void { | ||
// This is only here to copy frameContextLines from init options if it hasn't | ||
// been set via this integrations constructor. | ||
// | ||
// TODO: Remove on next major! | ||
if (this._options.frameContextLines === undefined) { | ||
const initOptions = getCurrentHub().getClient<NodeClient>()?.getOptions(); | ||
// eslint-disable-next-line deprecation/deprecation | ||
this._options.frameContextLines = initOptions?.frameContextLines; | ||
} | ||
|
||
const contextLines = | ||
this._options.frameContextLines !== undefined ? this._options.frameContextLines : DEFAULT_LINES_OF_CONTEXT; | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
addGlobalEventProcessor(event => this.addSourceContext(event, contextLines)); | ||
} | ||
|
||
/** Processes an event and adds context lines */ | ||
public async addSourceContext(event: Event, contextLines: number): Promise<Event> { | ||
const frames = event.exception?.values?.[0].stacktrace?.frames; | ||
|
||
if (frames && contextLines > 0) { | ||
const filenames: Set<string> = new Set(); | ||
|
||
for (const frame of frames) { | ||
if (frame.filename) { | ||
filenames.add(frame.filename); | ||
} | ||
} | ||
|
||
const sourceFiles = await readSourceFiles(filenames); | ||
|
||
for (const frame of frames) { | ||
if (frame.filename && sourceFiles[frame.filename]) { | ||
try { | ||
const lines = (sourceFiles[frame.filename] as string).split('\n'); | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
addContextToFrame(lines, frame, contextLines); | ||
} catch (e) { | ||
// anomaly, being defensive in case | ||
// unlikely to ever happen in practice but can definitely happen in theory | ||
} | ||
} | ||
} | ||
} | ||
|
||
return event; | ||
} | ||
} | ||
|
||
/** | ||
* This function reads file contents and caches them in a global LRU cache. | ||
* | ||
* @param filenames Array of filepaths to read content from. | ||
*/ | ||
async function readSourceFiles(filenames: Set<string>): Promise<Record<string, string | null>> { | ||
const sourceFiles: Record<string, string | null> = {}; | ||
|
||
for (const filename of filenames) { | ||
const cachedFile = FILE_CONTENT_CACHE.get(filename); | ||
// We have a cache hit | ||
if (cachedFile !== undefined) { | ||
// If stored value is null, it means that we already tried, but couldn't read the content of the file. Skip. | ||
if (cachedFile === null) { | ||
continue; | ||
} | ||
|
||
// Otherwise content is there, so reuse cached value. | ||
sourceFiles[filename] = cachedFile; | ||
continue; | ||
} | ||
|
||
let content: string | null = null; | ||
try { | ||
content = await readTextFileAsync(filename); | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (_) { | ||
// | ||
} | ||
|
||
FILE_CONTENT_CACHE.set(filename, content); | ||
sourceFiles[filename] = content; | ||
} | ||
|
||
return sourceFiles; | ||
} |
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
Oops, something went wrong.
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.