|
| 1 | +import googleTranslateRecursionNoiseFilterIntegration, {isGoogleTranslateRecursionNoise} from '@libs/telemetry/integrations/googleTranslateRecursionNoiseFilter'; |
| 2 | +import {googleTranslateRecursionNoiseFilterIntegration as webGoogleTranslateRecursionNoiseFilterIntegration} from '@libs/telemetry/integrations/index.web'; |
| 3 | + |
| 4 | +import type {ErrorEvent, StackFrame} from '@sentry/core'; |
| 5 | + |
| 6 | +import {buildErrorEvent, sentryClientStub, THIRD_PARTY_TAGS} from '../utils/SentryEventTestUtils'; |
| 7 | + |
| 8 | +const WEBKIT_MESSAGE = 'Maximum call stack size exceeded.'; |
| 9 | +const V8_MESSAGE = 'Maximum call stack size exceeded'; |
| 10 | + |
| 11 | +type Position = [lineno: number, colno: number]; |
| 12 | + |
| 13 | +function frame(filename: string, [lineno, colno]: Position): StackFrame { |
| 14 | + return {filename, lineno, colno}; |
| 15 | +} |
| 16 | + |
| 17 | +function callers(filename: string, positions: Position[]): StackFrame[] { |
| 18 | + return positions.map((position) => frame(filename, position)); |
| 19 | +} |
| 20 | + |
| 21 | +/** `count` frames alternating between two positions, `innermost` last. */ |
| 22 | +function cycle(filename: string, innermost: Position, other: Position, count: number): StackFrame[] { |
| 23 | + return Array.from({length: count}, (_, index) => frame(filename, (count - 1 - index) % 2 === 0 ? innermost : other)); |
| 24 | +} |
| 25 | + |
| 26 | +function buildStackOverflowEvent(frames: StackFrame[], tags: ErrorEvent['tags'] = THIRD_PARTY_TAGS, value = WEBKIT_MESSAGE): ErrorEvent { |
| 27 | + return buildErrorEvent([{type: 'RangeError', value, stacktrace: {frames}}], tags); |
| 28 | +} |
| 29 | + |
| 30 | +/** APP-M6P, Chrome iOS. */ |
| 31 | +const APP_M6P_FRAMES: StackFrame[] = [ |
| 32 | + ...callers('app:///home', [ |
| 33 | + [190, 70], |
| 34 | + [197, 363], |
| 35 | + [190, 41], |
| 36 | + [198, 237], |
| 37 | + [226, 382], |
| 38 | + ]), |
| 39 | + ...cycle('app:///home', [226, 63], [226, 408], 45), |
| 40 | +]; |
| 41 | + |
| 42 | +/** APP-KQF: the cycle sits under a different route than its callers. */ |
| 43 | +const APP_KQF_FRAMES: StackFrame[] = [ |
| 44 | + ...callers('app:///home', [ |
| 45 | + [190, 70], |
| 46 | + [197, 363], |
| 47 | + [190, 41], |
| 48 | + [198, 237], |
| 49 | + ]), |
| 50 | + frame('app:///transition', [226, 382]), |
| 51 | + ...cycle('app:///transition', [226, 63], [226, 408], 45), |
| 52 | +]; |
| 53 | + |
| 54 | +/** APP-M27: a page URL with no basename. */ |
| 55 | +const APP_M27_FRAMES: StackFrame[] = APP_M6P_FRAMES.map((entry) => ({...entry, filename: 'app:///'})); |
| 56 | + |
| 57 | +/** APP-MTE, desktop Chrome: the script reported under its own URL. */ |
| 58 | +const APP_MTE_FRAMES: StackFrame[] = [ |
| 59 | + ...callers('app:///m=el_main', [ |
| 60 | + [197, 128], |
| 61 | + [190, 39], |
| 62 | + [198, 235], |
| 63 | + [226, 380], |
| 64 | + ]), |
| 65 | + ...cycle('app:///m=el_main', [226, 406], [226, 61], 46), |
| 66 | +]; |
| 67 | + |
| 68 | +/** APP-KFE, an older Translate build: the loop sits on line 228, not 226. */ |
| 69 | +const APP_KFE_FRAMES: StackFrame[] = [ |
| 70 | + ...callers('app:///', [ |
| 71 | + [190, 70], |
| 72 | + [197, 363], |
| 73 | + [190, 41], |
| 74 | + [198, 237], |
| 75 | + [228, 382], |
| 76 | + ]), |
| 77 | + ...cycle('app:///', [228, 63], [228, 408], 45), |
| 78 | +]; |
| 79 | + |
| 80 | +describe('googleTranslateRecursionNoiseFilter', () => { |
| 81 | + it.each([ |
| 82 | + ['APP-M6P', APP_M6P_FRAMES, WEBKIT_MESSAGE], |
| 83 | + ['APP-KQF', APP_KQF_FRAMES, WEBKIT_MESSAGE], |
| 84 | + ['APP-M27', APP_M27_FRAMES, WEBKIT_MESSAGE], |
| 85 | + ['APP-MTE', APP_MTE_FRAMES, V8_MESSAGE], |
| 86 | + ['APP-KFE', APP_KFE_FRAMES, WEBKIT_MESSAGE], |
| 87 | + ])('drops %s', (_, frames, message) => { |
| 88 | + // Given the exact frames Sentry recorded for one Translate build, because each build moves the loop and renames its functions |
| 89 | + const event = buildStackOverflowEvent(frames, THIRD_PARTY_TAGS, message); |
| 90 | + |
| 91 | + // When the predicate runs on the recorded shape |
| 92 | + // Then it is dropped, because the filter must recognize every Translate build seen so far without pinning any one of them |
| 93 | + expect(isGoogleTranslateRecursionNoise(event)).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it.each([ |
| 97 | + ['an untagged event', buildStackOverflowEvent(APP_M6P_FRAMES, {})], |
| 98 | + ['a different error type', buildErrorEvent([{type: 'TypeError', value: WEBKIT_MESSAGE, stacktrace: {frames: APP_M6P_FRAMES}}])], |
| 99 | + [ |
| 100 | + 'a chained error', |
| 101 | + buildErrorEvent([ |
| 102 | + {type: 'RangeError', value: WEBKIT_MESSAGE, stacktrace: {frames: APP_M6P_FRAMES}}, |
| 103 | + {type: 'RangeError', value: WEBKIT_MESSAGE}, |
| 104 | + ]), |
| 105 | + ], |
| 106 | + ['a frame in one of our chunks', buildStackOverflowEvent([frame('app:///76-f662df2d477d1a4f.bundle.js', [1, 1]), ...APP_M6P_FRAMES])], |
| 107 | + ['an <anonymous> script', buildStackOverflowEvent(cycle('app:///<anonymous>', [16, 9], [4, 13], 45))], |
| 108 | + ['APP-ZF, a synthesized frame with no stack', buildStackOverflowEvent([frame('app:///undefined', [192, 70])])], |
| 109 | + ['APP-2NH, a vendor script under its own dotted URL', buildStackOverflowEvent(cycle('app:///10042537-100413459.js', [4541, 17291], [4541, 17291], 50))], |
| 110 | + ['a stack overflow with no frames', buildStackOverflowEvent([])], |
| 111 | + ['a RangeError that is not a stack overflow', buildStackOverflowEvent(APP_M6P_FRAMES, THIRD_PARTY_TAGS, 'Invalid array length')], |
| 112 | + ['a wasm frame compiled without a URL, which has no line number', buildStackOverflowEvent([{filename: 'app:///00b2a5aa:wasm-function[38]:0x1b6c'}, ...APP_M6P_FRAMES])], |
| 113 | + ])('keeps %s', (_, event) => { |
| 114 | + // Given an event that satisfies all but one condition of the signature |
| 115 | + // When the predicate runs |
| 116 | + // Then it is kept, because one missed condition is enough to mean this could be an App error we would otherwise lose |
| 117 | + expect(isGoogleTranslateRecursionNoise(event)).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + it('drops the noise and passes anything else through as an integration', () => { |
| 121 | + // Given the integration and one matching and one non-matching event, because Sentry calls `processEvent`, not the predicate |
| 122 | + const kept = buildStackOverflowEvent(APP_M6P_FRAMES, {}); |
| 123 | + |
| 124 | + // When each goes through `processEvent` |
| 125 | + // Then the noise maps to `null`, which is how the SDK drops an event, and the other comes back as the same object so nothing downstream sees a copy |
| 126 | + expect(googleTranslateRecursionNoiseFilterIntegration.processEvent?.(buildStackOverflowEvent(APP_M6P_FRAMES), {}, sentryClientStub)).toBeNull(); |
| 127 | + expect(googleTranslateRecursionNoiseFilterIntegration.processEvent?.(kept, {}, sentryClientStub)).toBe(kept); |
| 128 | + }); |
| 129 | + |
| 130 | + it('is exported from the web index', () => { |
| 131 | + // Given that the native index ships an `undefined` stub and `setupSentryIntegrationOrderTest` mocks the whole module |
| 132 | + // When the unmocked web index export is read |
| 133 | + // Then it is the real filter, because web is the only platform this runs on and nothing else checks the wiring |
| 134 | + expect(webGoogleTranslateRecursionNoiseFilterIntegration).toBe(googleTranslateRecursionNoiseFilterIntegration); |
| 135 | + }); |
| 136 | +}); |
0 commit comments