|
| 1 | +import { getCurrentHub } from '@sentry/core'; |
| 2 | +import { Event, EventProcessor, Integration } from '@sentry/types'; |
| 3 | +import { addContextToFrame } from '@sentry/utils'; |
| 4 | +import { readFile } from 'fs'; |
| 5 | +import { LRUMap } from 'lru_map'; |
| 6 | + |
| 7 | +import { NodeClient } from '../client'; |
| 8 | + |
| 9 | +const FILE_CONTENT_CACHE = new LRUMap<string, string | null>(100); |
| 10 | +const DEFAULT_LINES_OF_CONTEXT = 7; |
| 11 | + |
| 12 | +// TODO: Replace with promisify when minimum supported node >= v8 |
| 13 | +function readTextFileAsync(path: string): Promise<string> { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + readFile(path, 'utf8', (err, data) => { |
| 16 | + if (err) reject(err); |
| 17 | + else resolve(data); |
| 18 | + }); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Resets the file cache. Exists for testing purposes. |
| 24 | + * @hidden |
| 25 | + */ |
| 26 | +export function resetFileContentCache(): void { |
| 27 | + FILE_CONTENT_CACHE.clear(); |
| 28 | +} |
| 29 | + |
| 30 | +interface ContextLinesOptions { |
| 31 | + /** |
| 32 | + * Sets the number of context lines for each frame when loading a file. |
| 33 | + * Defaults to 7. |
| 34 | + * |
| 35 | + * Set to 0 to disable loading and inclusion of source files. |
| 36 | + **/ |
| 37 | + frameContextLines?: number; |
| 38 | +} |
| 39 | + |
| 40 | +/** Add node modules / packages to the event */ |
| 41 | +export class ContextLines implements Integration { |
| 42 | + /** |
| 43 | + * @inheritDoc |
| 44 | + */ |
| 45 | + public static id: string = 'ContextLines'; |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritDoc |
| 49 | + */ |
| 50 | + public name: string = ContextLines.id; |
| 51 | + |
| 52 | + public constructor(private readonly _options: ContextLinesOptions = {}) {} |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritDoc |
| 56 | + */ |
| 57 | + public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void { |
| 58 | + // This is only here to copy frameContextLines from init options if it hasn't |
| 59 | + // been set via this integrations constructor. |
| 60 | + // |
| 61 | + // TODO: Remove on next major! |
| 62 | + if (this._options.frameContextLines === undefined) { |
| 63 | + const initOptions = getCurrentHub().getClient<NodeClient>()?.getOptions(); |
| 64 | + // eslint-disable-next-line deprecation/deprecation |
| 65 | + this._options.frameContextLines = initOptions?.frameContextLines; |
| 66 | + } |
| 67 | + |
| 68 | + const contextLines = |
| 69 | + this._options.frameContextLines !== undefined ? this._options.frameContextLines : DEFAULT_LINES_OF_CONTEXT; |
| 70 | + |
| 71 | + addGlobalEventProcessor(event => this.addSourceContext(event, contextLines)); |
| 72 | + } |
| 73 | + |
| 74 | + /** Processes an event and adds context lines */ |
| 75 | + public async addSourceContext(event: Event, contextLines: number): Promise<Event> { |
| 76 | + const frames = event.exception?.values?.[0].stacktrace?.frames; |
| 77 | + |
| 78 | + if (frames && contextLines > 0) { |
| 79 | + const filenames: Set<string> = new Set(); |
| 80 | + |
| 81 | + for (const frame of frames) { |
| 82 | + if (frame.filename) { |
| 83 | + filenames.add(frame.filename); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + const sourceFiles = await readSourceFiles(filenames); |
| 88 | + |
| 89 | + for (const frame of frames) { |
| 90 | + if (frame.filename && sourceFiles[frame.filename]) { |
| 91 | + try { |
| 92 | + const lines = (sourceFiles[frame.filename] as string).split('\n'); |
| 93 | + addContextToFrame(lines, frame, contextLines); |
| 94 | + } catch (e) { |
| 95 | + // anomaly, being defensive in case |
| 96 | + // unlikely to ever happen in practice but can definitely happen in theory |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return event; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * This function reads file contents and caches them in a global LRU cache. |
| 108 | + * |
| 109 | + * @param filenames Array of filepaths to read content from. |
| 110 | + */ |
| 111 | +async function readSourceFiles(filenames: Set<string>): Promise<Record<string, string | null>> { |
| 112 | + const sourceFiles: Record<string, string | null> = {}; |
| 113 | + |
| 114 | + for (const filename of filenames) { |
| 115 | + const cachedFile = FILE_CONTENT_CACHE.get(filename); |
| 116 | + // We have a cache hit |
| 117 | + if (cachedFile !== undefined) { |
| 118 | + // If stored value is null, it means that we already tried, but couldn't read the content of the file. Skip. |
| 119 | + if (cachedFile === null) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + // Otherwise content is there, so reuse cached value. |
| 124 | + sourceFiles[filename] = cachedFile; |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + let content: string | null = null; |
| 129 | + try { |
| 130 | + content = await readTextFileAsync(filename); |
| 131 | + } catch (_) { |
| 132 | + // |
| 133 | + } |
| 134 | + |
| 135 | + FILE_CONTENT_CACHE.set(filename, content); |
| 136 | + sourceFiles[filename] = content; |
| 137 | + } |
| 138 | + |
| 139 | + return sourceFiles; |
| 140 | +} |
0 commit comments