|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import type { ConsoleLevel, HandlerDataConsole, WrappedFunction } from '@sentry/core'; |
| 3 | +import { |
| 4 | + CONSOLE_LEVELS, |
| 5 | + GLOBAL_OBJ, |
| 6 | + consoleIntegration as coreConsoleIntegration, |
| 7 | + defineIntegration, |
| 8 | + fill, |
| 9 | + markFunctionWrapped, |
| 10 | + maybeInstrument, |
| 11 | + originalConsoleMethods, |
| 12 | + triggerHandlers, |
| 13 | +} from '@sentry/core'; |
| 14 | + |
| 15 | +interface ConsoleIntegrationOptions { |
| 16 | + levels: ConsoleLevel[]; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Node-specific console integration that captures breadcrumbs and handles |
| 21 | + * the AWS Lambda runtime replacing console methods after our patch. |
| 22 | + * |
| 23 | + * In Lambda, console methods are patched via `Object.defineProperty` so that |
| 24 | + * external replacements (by the Lambda runtime) are absorbed as the delegate |
| 25 | + * while our wrapper stays in place. Outside Lambda, this delegates entirely |
| 26 | + * to the core `consoleIntegration` which uses the simpler `fill`-based patch. |
| 27 | + */ |
| 28 | +export const consoleIntegration = defineIntegration((options: Partial<ConsoleIntegrationOptions> = {}) => { |
| 29 | + return { |
| 30 | + name: 'Console', |
| 31 | + setup(client) { |
| 32 | + if (process.env.LAMBDA_TASK_ROOT) { |
| 33 | + maybeInstrument('console', instrumentConsoleLambda); |
| 34 | + } |
| 35 | + |
| 36 | + // Delegate breadcrumb handling to the core console integration. |
| 37 | + const core = coreConsoleIntegration(options); |
| 38 | + core.setup?.(client); |
| 39 | + }, |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
| 43 | +function instrumentConsoleLambda(): void { |
| 44 | + const consoleObj = GLOBAL_OBJ?.console; |
| 45 | + if (!consoleObj) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + CONSOLE_LEVELS.forEach((level: ConsoleLevel) => { |
| 50 | + if (level in consoleObj) { |
| 51 | + patchWithDefineProperty(consoleObj, level); |
| 52 | + } |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +function patchWithDefineProperty(consoleObj: Console, level: ConsoleLevel): void { |
| 57 | + const nativeMethod = consoleObj[level] as (...args: unknown[]) => void; |
| 58 | + originalConsoleMethods[level] = nativeMethod; |
| 59 | + |
| 60 | + let delegate: Function = nativeMethod; |
| 61 | + let savedDelegate: Function | undefined; |
| 62 | + let isExecuting = false; |
| 63 | + |
| 64 | + const wrapper = function (...args: any[]): void { |
| 65 | + if (isExecuting) { |
| 66 | + // Re-entrant call: a third party captured `wrapper` via the getter and calls it from inside their replacement. We must |
| 67 | + // use `nativeMethod` (not `delegate`) to break the cycle, and we intentionally skip `triggerHandlers` to avoid duplicate |
| 68 | + // breadcrumbs. The outer invocation already triggered the handlers for this console call. |
| 69 | + nativeMethod.apply(consoleObj, args); |
| 70 | + return; |
| 71 | + } |
| 72 | + isExecuting = true; |
| 73 | + try { |
| 74 | + triggerHandlers('console', { args, level } as HandlerDataConsole); |
| 75 | + delegate.apply(consoleObj, args); |
| 76 | + } finally { |
| 77 | + isExecuting = false; |
| 78 | + } |
| 79 | + }; |
| 80 | + markFunctionWrapped(wrapper as unknown as WrappedFunction, nativeMethod as unknown as WrappedFunction); |
| 81 | + |
| 82 | + // consoleSandbox reads originalConsoleMethods[level] to temporarily bypass instrumentation. We replace it with a distinct reference (.bind creates a |
| 83 | + // new function identity) so the setter can tell apart "consoleSandbox bypass" from "external code restoring a native method captured before Sentry init." |
| 84 | + const sandboxBypass = nativeMethod.bind(consoleObj); |
| 85 | + originalConsoleMethods[level] = sandboxBypass; |
| 86 | + |
| 87 | + try { |
| 88 | + let current: any = wrapper; |
| 89 | + |
| 90 | + Object.defineProperty(consoleObj, level, { |
| 91 | + configurable: true, |
| 92 | + enumerable: true, |
| 93 | + get() { |
| 94 | + return current; |
| 95 | + }, |
| 96 | + set(newValue) { |
| 97 | + if (newValue === wrapper) { |
| 98 | + // consoleSandbox restoring the wrapper: recover the saved delegate. |
| 99 | + if (savedDelegate !== undefined) { |
| 100 | + delegate = savedDelegate; |
| 101 | + savedDelegate = undefined; |
| 102 | + } |
| 103 | + current = wrapper; |
| 104 | + } else if (newValue === sandboxBypass) { |
| 105 | + // consoleSandbox entering bypass: save delegate, let getter return sandboxBypass directly so calls skip the wrapper entirely. |
| 106 | + savedDelegate = delegate; |
| 107 | + current = sandboxBypass; |
| 108 | + } else if (typeof newValue === 'function' && !(newValue as WrappedFunction).__sentry_original__) { |
| 109 | + delegate = newValue; |
| 110 | + current = wrapper; |
| 111 | + } else { |
| 112 | + current = newValue; |
| 113 | + } |
| 114 | + }, |
| 115 | + }); |
| 116 | + } catch { |
| 117 | + // Fall back to fill-based patching if defineProperty fails |
| 118 | + fill(consoleObj, level, function (originalConsoleMethod: () => any): Function { |
| 119 | + originalConsoleMethods[level] = originalConsoleMethod; |
| 120 | + |
| 121 | + return function (this: Console, ...args: any[]): void { |
| 122 | + triggerHandlers('console', { args, level } as HandlerDataConsole); |
| 123 | + originalConsoleMethods[level]?.apply(this, args); |
| 124 | + }; |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
0 commit comments