Repository navigation
fix(core): Ignore errors that are never sent in Dedupe #27138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import type { EventHint } from '../types/event'; | ||
|
|
||
| /** | ||
| * How the Dedupe integration treats an error event. The client sets it before event processors run. | ||
| * | ||
| * - `ignore`: The error is never sent. Dedupe does not compare it and does not remember it. | ||
| */ | ||
| export type DedupeContext = { mode: 'ignore' }; | ||
|
|
||
| const dedupeContexts = new WeakMap<EventHint, DedupeContext>(); | ||
|
|
||
| /** Sets how the Dedupe integration treats the error event of this hint. */ | ||
| export function setDedupeContext(hint: EventHint, context: DedupeContext): void { | ||
| dedupeContexts.set(hint, context); | ||
| } | ||
|
|
||
| /** Gets how the Dedupe integration treats the error event of this hint. */ | ||
| export function getDedupeContext(hint: EventHint): DedupeContext | undefined { | ||
| return dedupeContexts.get(hint); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,22 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { | ||
| captureException, | ||
| getCurrentScope, | ||
| linkedErrorsIntegration, | ||
| makeSession, | ||
| setCurrentClient, | ||
| withScope, | ||
| } from '../../../src'; | ||
| import { _shouldDropEvent, dedupeIntegration } from '../../../src/integrations/dedupe'; | ||
| import type { Event as SentryEvent } from '../../../src/types/event'; | ||
| import type { Exception } from '../../../src/types/exception'; | ||
| import type { StackFrame } from '../../../src/types/stackframe'; | ||
| import type { Stacktrace } from '../../../src/types/stacktrace'; | ||
| import { getDefaultTestClientOptions, TestClient, type TestClientOptions } from '../../mocks/client'; | ||
| import { AsyncTestIntegration } from '../../mocks/integration'; | ||
| import { resetGlobals } from '../../testutils'; | ||
|
|
||
| const PUBLIC_DSN = 'https://username@domain/123'; | ||
|
|
||
| type EventWithException = SentryEvent & { | ||
| exception: { | ||
|
|
@@ -178,33 +191,149 @@ describe('Dedupe', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('processEvent', () => { | ||
| it('ignores consecutive errors', () => { | ||
| const integration = dedupeIntegration(); | ||
|
|
||
| expect(integration.processEvent?.(clone(exceptionEvent), {}, {} as any)).not.toBeNull(); | ||
| expect(integration.processEvent?.(clone(exceptionEvent), {}, {} as any)).toBeNull(); | ||
| expect(integration.processEvent?.(clone(exceptionEvent), {}, {} as any)).toBeNull(); | ||
| }); | ||
|
|
||
| it('ignores transactions between errors', () => { | ||
| const integration = dedupeIntegration(); | ||
|
|
||
| expect(integration.processEvent?.(clone(exceptionEvent), {}, {} as any)).not.toBeNull(); | ||
| expect( | ||
| integration.processEvent?.( | ||
| { | ||
| event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', | ||
| message: 'someMessage', | ||
| transaction: 'wat', | ||
| type: 'transaction', | ||
| }, | ||
| {}, | ||
| {} as any, | ||
| ), | ||
| ).not.toBeNull(); | ||
| expect(integration.processEvent?.(clone(exceptionEvent), {}, {} as any)).toBeNull(); | ||
| expect(integration.processEvent?.(clone(exceptionEvent), {}, {} as any)).toBeNull(); | ||
| describe('integration', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [m]
|
||
| beforeEach(() => { | ||
| TestClient.instance = undefined; | ||
| TestClient.sendEventCalled = undefined; | ||
| resetGlobals(); | ||
| }); | ||
|
|
||
| function initClient(options: Partial<TestClientOptions> = {}): TestClient { | ||
| const client = new TestClient( | ||
| getDefaultTestClientOptions({ dsn: PUBLIC_DSN, integrations: [dedupeIntegration()], ...options }), | ||
| ); | ||
| setCurrentClient(client); | ||
| client.init(); | ||
| return client; | ||
| } | ||
|
|
||
| it('drops consecutive duplicate errors', () => { | ||
| const sendEventCalled = vi.fn(); | ||
| TestClient.sendEventCalled = sendEventCalled; | ||
| const client = initClient(); | ||
|
|
||
| client.captureException(new Error('duplicate')); | ||
| client.captureException(new Error('duplicate')); | ||
| client.captureException(new Error('duplicate')); | ||
|
|
||
| expect(sendEventCalled).toHaveBeenCalledOnce(); | ||
| expect(client._clearOutcomes()).toEqual([{ reason: 'event_processor', category: 'error', quantity: 2 }]); | ||
| }); | ||
|
|
||
| it('compares an event only with the previous event', () => { | ||
| const sendEventCalled = vi.fn(); | ||
| TestClient.sendEventCalled = sendEventCalled; | ||
| const client = initClient(); | ||
|
|
||
| for (const value of ['a', 'a', 'b', 'b', 'a']) { | ||
| client.captureException(new Error(value)); | ||
| } | ||
| client.captureMessage('message'); | ||
| client.captureMessage('message'); | ||
|
|
||
| expect(sendEventCalled).toHaveBeenCalledTimes(4); | ||
| expect(client._clearOutcomes()).toEqual([{ reason: 'event_processor', category: 'error', quantity: 3 }]); | ||
| }); | ||
|
|
||
| it('compares errors with the fingerprint from the scope', () => { | ||
| const sendEventCalled = vi.fn(); | ||
| TestClient.sendEventCalled = sendEventCalled; | ||
| const client = initClient(); | ||
|
|
||
| for (const fingerprint of ['a', 'b']) { | ||
| withScope(scope => { | ||
| scope.setFingerprint([fingerprint]); | ||
| client.captureException(new Error('same error'), undefined, scope); | ||
| }); | ||
| } | ||
|
|
||
| expect(sendEventCalled).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('compares errors with their linked errors', () => { | ||
| const sendEventCalled = vi.fn(); | ||
| TestClient.sendEventCalled = sendEventCalled; | ||
| initClient({ integrations: [linkedErrorsIntegration(), dedupeIntegration()] }); | ||
|
|
||
| for (const cause of ['a', 'a', 'b']) { | ||
| captureException(Object.assign(new Error('same error'), { cause: new Error(cause) })); | ||
| } | ||
|
|
||
| expect(sendEventCalled).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('does not let duplicate errors push other errors out of the promise buffer', async () => { | ||
| const sendEventCalled = vi.fn(); | ||
| TestClient.sendEventCalled = sendEventCalled; | ||
| const client = initClient({ | ||
| integrations: [dedupeIntegration(), new AsyncTestIntegration()], | ||
| transportOptions: { bufferSize: 2 }, | ||
| }); | ||
| getCurrentScope().setSession(makeSession()); | ||
| const sendEnvelopeSpy = vi.spyOn(client, 'sendEnvelope'); | ||
|
|
||
| for (let i = 0; i < 10; i++) { | ||
| client.captureException(new Error('duplicate')); | ||
| } | ||
| client.captureException(new Error('other error')); | ||
| await client.flush(); | ||
|
|
||
| expect(sendEventCalled).toHaveBeenCalledTimes(2); | ||
| expect(sendEnvelopeSpy).toHaveBeenCalledWith([ | ||
| expect.any(Object), | ||
| [ | ||
| [ | ||
| { type: 'client_report' }, | ||
| expect.objectContaining({ | ||
| discarded_events: [{ reason: 'event_processor', category: 'error', quantity: 9 }], | ||
| }), | ||
| ], | ||
| ], | ||
| ]); | ||
| }); | ||
|
|
||
| it('does not count a duplicate error that a later event processor drops', () => { | ||
| const client = initClient(); | ||
| const session = makeSession(); | ||
| getCurrentScope().setSession(session); | ||
| client.addEventProcessor(() => null); | ||
|
|
||
| client.captureException(new Error('duplicate')); | ||
| client.captureException(new Error('duplicate')); | ||
|
|
||
| expect(session.errors).toBe(0); | ||
| expect(client._clearOutcomes()).toEqual([{ reason: 'event_processor', category: 'error', quantity: 2 }]); | ||
| }); | ||
|
|
||
| it('drops a handled duplicate error from the same isolation scope without calling `beforeSend` again', () => { | ||
| const beforeSend = vi.fn(() => null); | ||
| const client = initClient({ beforeSend }); | ||
| const session = makeSession(); | ||
| getCurrentScope().setSession(session); | ||
|
|
||
| client.captureException(new Error('duplicate')); | ||
| client.captureException(new Error('duplicate')); | ||
| client.captureException(new Error('duplicate')); | ||
|
|
||
| expect(beforeSend).toHaveBeenCalledOnce(); | ||
| expect(session.errors).toBe(0); | ||
| expect(client._clearOutcomes()).toEqual([ | ||
| { reason: 'before_send', category: 'error', quantity: 1 }, | ||
| { reason: 'event_processor', category: 'error', quantity: 2 }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('does not drop a sampled-in error that duplicates a sampled-out error', () => { | ||
| const sendEventCalled = vi.fn(); | ||
| TestClient.sendEventCalled = sendEventCalled; | ||
| const client = initClient({ sampleRate: 0 }); | ||
| getCurrentScope().setSession(makeSession()); | ||
|
|
||
| client.captureException(new Error('duplicate')); | ||
| client.getOptions().sampleRate = 1; | ||
| client.captureException(new Error('duplicate')); | ||
|
|
||
| expect(sendEventCalled).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[m]
packages/core/src/integrations/dedupe.ts:22: A sampled-out duplicate now counts in sessions, but the same duplicate sampled in does notWith
mode: 'ignore', Dedupe skips the sampled-out error entirely, so it no longer drops it when it duplicates the previous sent error. The error then reachesbeforeSendand_updateSessions. The same duplicate, sampled in, is still dropped by Dedupe and does not count. So session health now depends on the sample roll, which goes against the goal in_updateSessions("sampling only saves quota, so the error must still count" as if it were sent).Trigger:
Error('dup')is sent, a new session starts (or the second copy is unhandled), then the same error is captured again. I checked with a scratch test (Dedupe,beforeSend, a freshoksession before the second capture):session.errorsbeforeSendcallsevent_processorevent_processorsample_rateA fix that keeps both goals: for a sampled-out error, Dedupe still compares it with
previousEvent(and drops it if it is a duplicate) but does not store it aspreviousEvent. Theignoredoc indedupeContext.ts("does not compare it and does not remember it") would change with it.