-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check for telemetry consent before error reporting (#7019)
* feat: check for telemetry consent before error reporting * refactor: try approach with custom transport instead * fixup!: await in flushBuffer and type check / invoke transport * fixup!: type transport in sentry reporter and comments in buffered transport Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com> * fixup!: reduce errorReporting enablement and cleanup sentry files * chore: update pnpm from next * fix: add back browseroptions cast --------- Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>
- Loading branch information
1 parent
666c420
commit 83e1111
Showing
7 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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
63 changes: 63 additions & 0 deletions
63
packages/sanity/src/core/error/sentry/makeBufferedTransport.ts
This file contains 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,63 @@ | ||
import {makeFetchTransport} from '@sentry/react' | ||
import {type EventEnvelope, type Transport, type TransportMakeRequestResponse} from '@sentry/types' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export type BufferedTransport = Transport & { | ||
setConsent: (consentGiven: boolean) => Promise<void> | ||
} | ||
|
||
/** | ||
* Because we want to buffer events until the user has given consent to telemetry, | ||
* we need to implement a custom transport, but mostly wrap the fetch transport. | ||
* @internal | ||
*/ | ||
export function makeBufferedTransport(options: any): BufferedTransport { | ||
let buffer: EventEnvelope[] = [] | ||
let consentGiven: boolean | undefined | ||
const fetchTransport = makeFetchTransport(options) | ||
|
||
const send = async (event: EventEnvelope) => { | ||
if (consentGiven) { | ||
return sendImmediately(event) | ||
} | ||
|
||
//we may not have received consent yet. Buffer the event until we know what to do. | ||
if (typeof consentGiven === 'undefined') { | ||
buffer.push(event) | ||
} | ||
// consent not given, skip sending the event | ||
return {} | ||
} | ||
|
||
const sendImmediately = async (event: EventEnvelope): Promise<TransportMakeRequestResponse> => { | ||
return fetchTransport.send(event) | ||
} | ||
|
||
const setConsent = async (consent: boolean) => { | ||
consentGiven = consent | ||
//we clear the buffer if consent is given (since we've sent the buffered events) | ||
//and we clear the buffer if consent is revoked (since the events should not be sent) | ||
if (consent) { | ||
await flushBuffer() | ||
} | ||
buffer = [] | ||
} | ||
|
||
const flushBuffer = async () => { | ||
await Promise.all(buffer.map(sendImmediately)).catch((err) => { | ||
console.error('Failed to send buffered events from transport: ', err) | ||
}) | ||
} | ||
|
||
const flush = async () => { | ||
return fetchTransport.flush() | ||
} | ||
|
||
return { | ||
flush, | ||
send, | ||
setConsent, | ||
} | ||
} |
This file contains 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 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,34 @@ | ||
import {type SanityClient} from '@sanity/client' | ||
import {useEffect} from 'react' | ||
|
||
import {errorReporter} from '../error/errorReporter' | ||
import {useClient} from '../hooks' | ||
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../studioClient' | ||
|
||
async function fetchTelemetryConsent(client: SanityClient) { | ||
return client.request({uri: '/intake/telemetry-status'}) | ||
} | ||
|
||
export function EnableErrorReporting() { | ||
const client = useClient().withConfig(DEFAULT_STUDIO_CLIENT_OPTIONS) | ||
|
||
useEffect(() => { | ||
async function checkConsent() { | ||
try { | ||
const res = await fetchTelemetryConsent(client) | ||
|
||
if (res?.status === 'granted') { | ||
errorReporter.enable() | ||
} else { | ||
errorReporter.disable() | ||
} | ||
} catch (e) { | ||
console.error('Error fetching telemetry status', e) | ||
} | ||
} | ||
|
||
checkConsent() | ||
}, [client]) | ||
|
||
return null | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.