-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(console): Re-patch console in AWS Lambda runtimes #20337
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 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
88c6f55
add tests
s1gr1d d1cfd60
fix(console): Re-patch console in AWS Lambda runtimes
s1gr1d 487e210
increase size limit
s1gr1d dc97744
add test against infinite recursion
s1gr1d d4d89f6
fix recursion
s1gr1d e7709a1
refactor
s1gr1d 8a1fc4d
add test case
s1gr1d 249d85d
refactor
s1gr1d e3e3636
exprot consoleIntegration from node-core
s1gr1d 2821b31
fix formatting
s1gr1d a93810e
Merge branch 'develop' into sig/console-aws-lambda-fix
s1gr1d aef0ddd
Merge branch 'develop' into sig/console-aws-lambda-fix
s1gr1d 6b3ee74
add consoleintegration type
s1gr1d 3268548
add new tests
s1gr1d ef9eaa6
improve implementation
s1gr1d 125c6db
Merge branch 'develop' into sig/console-aws-lambda-fix
s1gr1d 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
200 changes: 200 additions & 0 deletions
200
packages/core/test/lib/instrument/console-lambda.test.ts
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,200 @@ | ||
| // Set LAMBDA_TASK_ROOT before any imports so instrumentConsole uses patchWithDefineProperty | ||
| process.env.LAMBDA_TASK_ROOT = '/var/task'; | ||
|
|
||
| import { afterAll, describe, expect, it, vi } from 'vitest'; | ||
| import { addConsoleInstrumentationHandler } from '../../../src/instrument/console'; | ||
| import type { WrappedFunction } from '../../../src/types-hoist/wrappedfunction'; | ||
| import { consoleSandbox, originalConsoleMethods } from '../../../src/utils/debug-logger'; | ||
| import { markFunctionWrapped } from '../../../src/utils/object'; | ||
| import { GLOBAL_OBJ } from '../../../src/utils/worldwide'; | ||
|
|
||
| afterAll(() => { | ||
| delete process.env.LAMBDA_TASK_ROOT; | ||
| }); | ||
|
|
||
| describe('addConsoleInstrumentationHandler in Lambda (patchWithDefineProperty)', () => { | ||
| it('calls registered handler when console.log is called', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| GLOBAL_OBJ.console.log('test'); | ||
|
|
||
| expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['test'], level: 'log' })); | ||
| }); | ||
|
|
||
| describe('external replacement (e.g. Lambda runtime overwriting console)', () => { | ||
| it('keeps firing the handler after console.log is replaced externally', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| GLOBAL_OBJ.console.log = vi.fn(); | ||
| handler.mockClear(); | ||
|
|
||
| GLOBAL_OBJ.console.log('after replacement'); | ||
|
|
||
| expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['after replacement'], level: 'log' })); | ||
| }); | ||
|
|
||
| it('calls the external replacement as the underlying method', () => { | ||
| addConsoleInstrumentationHandler(vi.fn()); | ||
|
|
||
| const lambdaLogger = vi.fn(); | ||
| GLOBAL_OBJ.console.log = lambdaLogger; | ||
|
|
||
| GLOBAL_OBJ.console.log('hello'); | ||
|
|
||
| expect(lambdaLogger).toHaveBeenCalledWith('hello'); | ||
| }); | ||
|
|
||
| it('always delegates to the latest replacement', () => { | ||
| addConsoleInstrumentationHandler(vi.fn()); | ||
|
|
||
| const first = vi.fn(); | ||
| const second = vi.fn(); | ||
|
|
||
| GLOBAL_OBJ.console.log = first; | ||
| GLOBAL_OBJ.console.log = second; | ||
|
|
||
| GLOBAL_OBJ.console.log('latest'); | ||
|
|
||
| expect(first).not.toHaveBeenCalled(); | ||
| expect(second).toHaveBeenCalledWith('latest'); | ||
| }); | ||
|
|
||
| it('does not mutate originalConsoleMethods (kept safe for consoleSandbox)', () => { | ||
| addConsoleInstrumentationHandler(vi.fn()); | ||
|
|
||
| const nativeLog = originalConsoleMethods.log; | ||
| GLOBAL_OBJ.console.log = vi.fn(); | ||
|
|
||
| expect(originalConsoleMethods.log).toBe(nativeLog); | ||
| }); | ||
| }); | ||
|
|
||
| describe('__sentry_original__ detection', () => { | ||
| it('accepts a function with __sentry_original__ without re-wrapping', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| const otherWrapper = vi.fn(); | ||
| markFunctionWrapped(otherWrapper as unknown as WrappedFunction, vi.fn() as unknown as WrappedFunction); | ||
|
|
||
| GLOBAL_OBJ.console.log = otherWrapper; | ||
|
|
||
| expect(GLOBAL_OBJ.console.log).toBe(otherWrapper); | ||
| }); | ||
|
|
||
| it('does not fire our handler when a __sentry_original__ wrapper is installed', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| const otherWrapper = vi.fn(); | ||
| markFunctionWrapped(otherWrapper as unknown as WrappedFunction, vi.fn() as unknown as WrappedFunction); | ||
|
|
||
| GLOBAL_OBJ.console.log = otherWrapper; | ||
| handler.mockClear(); | ||
|
|
||
| GLOBAL_OBJ.console.log('via other wrapper'); | ||
|
|
||
| expect(handler).not.toHaveBeenCalled(); | ||
| expect(otherWrapper).toHaveBeenCalledWith('via other wrapper'); | ||
| }); | ||
|
|
||
| it('re-wraps a plain function without __sentry_original__', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| GLOBAL_OBJ.console.log = vi.fn(); | ||
| handler.mockClear(); | ||
|
|
||
| GLOBAL_OBJ.console.log('plain'); | ||
|
|
||
| expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['plain'], level: 'log' })); | ||
| }); | ||
| }); | ||
|
|
||
| describe('consoleSandbox interaction', () => { | ||
| it('does not fire the handler inside consoleSandbox', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
| handler.mockClear(); | ||
|
|
||
| consoleSandbox(() => { | ||
| GLOBAL_OBJ.console.log('sandbox message'); | ||
| }); | ||
|
|
||
| expect(handler).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('resumes firing the handler after consoleSandbox returns', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| consoleSandbox(() => { | ||
| GLOBAL_OBJ.console.log('inside sandbox'); | ||
| }); | ||
| handler.mockClear(); | ||
|
|
||
| GLOBAL_OBJ.console.log('after sandbox'); | ||
|
|
||
| expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['after sandbox'], level: 'log' })); | ||
| expect(handler).not.toHaveBeenCalledWith(expect.objectContaining({ args: ['inside sandbox'], level: 'log' })); | ||
| }); | ||
|
|
||
| it('does not fire the handler inside consoleSandbox after a Lambda-style replacement', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| GLOBAL_OBJ.console.log = vi.fn(); | ||
| handler.mockClear(); | ||
|
|
||
| consoleSandbox(() => { | ||
| GLOBAL_OBJ.console.log('sandbox after lambda'); | ||
| }); | ||
|
|
||
| expect(handler).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('third-party capture-and-call wrapping', () => { | ||
| it('does not cause infinite recursion when a third party wraps console with the capture pattern', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
| handler.mockClear(); | ||
|
|
||
| // This is the extremely common pattern used by logging libraries, test frameworks, etc: | ||
| // const prevLog = console.log; | ||
| // console.log = (...args) => { prevLog(...args); doSomethingElse(); } | ||
| const prevLog = GLOBAL_OBJ.console.log; | ||
| const thirdPartyExtra = vi.fn(); | ||
| GLOBAL_OBJ.console.log = (...args: any[]) => { | ||
| prevLog(...args); | ||
| thirdPartyExtra(...args); | ||
| }; | ||
|
|
||
| // With the bug, this causes "Maximum call stack size exceeded" | ||
| expect(() => GLOBAL_OBJ.console.log('should not overflow')).not.toThrow(); | ||
|
|
||
| expect(thirdPartyExtra).toHaveBeenCalledWith('should not overflow'); | ||
| expect(handler).toHaveBeenCalledTimes(1); | ||
| expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['should not overflow'], level: 'log' })); | ||
| }); | ||
|
|
||
| it('consoleSandbox still bypasses the handler after third-party wrapping', () => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| const prevLog = GLOBAL_OBJ.console.log; | ||
| GLOBAL_OBJ.console.log = (...args: any[]) => { | ||
| prevLog(...args); | ||
| }; | ||
| handler.mockClear(); | ||
|
|
||
| consoleSandbox(() => { | ||
| GLOBAL_OBJ.console.log('should bypass'); | ||
| }); | ||
|
|
||
| expect(handler).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
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,22 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { addConsoleInstrumentationHandler } from '../../../src/instrument/console'; | ||
| import { GLOBAL_OBJ } from '../../../src/utils/worldwide'; | ||
|
|
||
| describe('addConsoleInstrumentationHandler', () => { | ||
| it.each(['log', 'warn', 'error', 'debug', 'info'] as const)( | ||
| 'calls registered handler when console.%s is called', | ||
| level => { | ||
| const handler = vi.fn(); | ||
| addConsoleInstrumentationHandler(handler); | ||
|
|
||
| GLOBAL_OBJ.console[level]('test message'); | ||
|
|
||
| expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['test message'], level })); | ||
| }, | ||
| ); | ||
|
|
||
| it('calls through to the underlying console method without throwing', () => { | ||
| addConsoleInstrumentationHandler(vi.fn()); | ||
| expect(() => GLOBAL_OBJ.console.log('hello')).not.toThrow(); | ||
| }); | ||
| }); |
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.