Fix Sentry deduplication of events that were never sent (#15677)
The Sentry `Dedupe` integration has been filtering out our events, even when they were never sent due to our `beforeSend` handler. It was wrongly identifying them as duplicates because it has no knowledge of `beforeSend` or whether they were actually sent or not. To resolve this, the filtering we were doing in `beforeSend` has been moved to a Sentry integration. This integration is installed ahead of the `Dedupe` integration, so `Dedupe` should never find out about any events that we filter out, and thus will never consider them as sent when they were not.feature/default_network_editable
parent
5a25084eab
commit
1f36ba4b75
@ -0,0 +1,73 @@ |
|||||||
|
import { |
||||||
|
Event as SentryEvent, |
||||||
|
EventProcessor, |
||||||
|
Hub, |
||||||
|
Integration, |
||||||
|
} from '@sentry/types'; |
||||||
|
import { logger } from '@sentry/utils'; |
||||||
|
|
||||||
|
/** |
||||||
|
* Filter events when MetaMetrics is disabled. |
||||||
|
*/ |
||||||
|
export class FilterEvents implements Integration { |
||||||
|
/** |
||||||
|
* Property that holds the integration name. |
||||||
|
*/ |
||||||
|
public static id = 'FilterEvents'; |
||||||
|
|
||||||
|
/** |
||||||
|
* Another property that holds the integration name. |
||||||
|
* |
||||||
|
* I don't know why this exists, but the other Sentry integrations have it. |
||||||
|
*/ |
||||||
|
public name: string = FilterEvents.id; |
||||||
|
|
||||||
|
/** |
||||||
|
* A function that returns whether MetaMetrics is enabled. This should also |
||||||
|
* return `false` if state has not yet been initialzed. |
||||||
|
* |
||||||
|
* @returns `true` if MetaMask's state has been initialized, and MetaMetrics |
||||||
|
* is enabled, `false` otherwise. |
||||||
|
*/ |
||||||
|
private getMetaMetricsEnabled: () => boolean; |
||||||
|
|
||||||
|
/** |
||||||
|
* @param options - Constructor options. |
||||||
|
* @param options.getMetaMetricsEnabled - A function that returns whether |
||||||
|
* MetaMetrics is enabled. This should also return `false` if state has not |
||||||
|
* yet been initialzed. |
||||||
|
*/ |
||||||
|
constructor({ |
||||||
|
getMetaMetricsEnabled, |
||||||
|
}: { |
||||||
|
getMetaMetricsEnabled: () => boolean; |
||||||
|
}) { |
||||||
|
this.getMetaMetricsEnabled = getMetaMetricsEnabled; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Setup the integration. |
||||||
|
* |
||||||
|
* @param addGlobalEventProcessor - A function that allows adding a global |
||||||
|
* event processor. |
||||||
|
* @param getCurrentHub - A function that returns the current Sentry hub. |
||||||
|
*/ |
||||||
|
public setupOnce( |
||||||
|
addGlobalEventProcessor: (callback: EventProcessor) => void, |
||||||
|
getCurrentHub: () => Hub, |
||||||
|
): void { |
||||||
|
addGlobalEventProcessor((currentEvent: SentryEvent) => { |
||||||
|
// Sentry integrations use the Sentry hub to get "this" references, for
|
||||||
|
// reasons I don't fully understand.
|
||||||
|
// eslint-disable-next-line consistent-this
|
||||||
|
const self = getCurrentHub().getIntegration(FilterEvents); |
||||||
|
if (self) { |
||||||
|
if (!self.getMetaMetricsEnabled()) { |
||||||
|
logger.warn(`Event dropped due to MetaMetrics setting.`); |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
return currentEvent; |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue