-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore(svelte): Detect and report SvelteKit usage #5594
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 2 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { BrowserOptions, init as browserInit, SDK_VERSION } from '@sentry/browser'; | ||
|
||
import { addGlobalEventProcessor, BrowserOptions, init as browserInit, SDK_VERSION } from '@sentry/browser'; | ||
import type { EventProcessor } from '@sentry/types'; | ||
import { getDomElement } from '@sentry/utils'; | ||
/** | ||
* Inits the Svelte SDK | ||
*/ | ||
|
@@ -17,4 +18,45 @@ export function init(options: BrowserOptions): void { | |
}; | ||
|
||
browserInit(options); | ||
|
||
detectAndReportSvelteKit(); | ||
} | ||
|
||
/** | ||
* Adds a global event processor to detect if the SDK is initialized in a SvelteKit frontend, | ||
* in which case we add SvelteKit an event.modules entry to outgoing events. | ||
* SvelteKit detection is performed only once, when the event processor is called for the | ||
* first time. We cannot perform this check upfront (directly when init is called) because | ||
* at this time, the HTML element might not yet be accessible. | ||
*/ | ||
export function detectAndReportSvelteKit(): void { | ||
let detectedSvelteKit: boolean | undefined = undefined; | ||
|
||
const svelteKitProcessor: EventProcessor = event => { | ||
if (detectedSvelteKit === undefined) { | ||
detectedSvelteKit = isSvelteKitApp(); | ||
} | ||
if (detectedSvelteKit) { | ||
event.modules = { | ||
svelteKit: '1.0', | ||
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. Hmm - feels weird to write 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. I agree, it's weird but we have no way of knowing which SvelteKit version was actually being used (let alone that 1.0 isn't even stable yet). So my train of thought for 1.0 was to simply put there something because afaik we have to supply a version here. But happy to change it to 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. Until we are able to confidently discover correct SvelteKit version I think we can keep it at 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. I would maybe leave it as 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. My worry about leaving it as 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. fine with me. I was also thinking to convert it to a boolean but I don't want to abuse the module field for that 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. Sure, |
||
...event.modules, | ||
}; | ||
} | ||
return event; | ||
}; | ||
svelteKitProcessor.id = 'svelteKitProcessor'; | ||
|
||
addGlobalEventProcessor(svelteKitProcessor); | ||
} | ||
|
||
/** | ||
* To actually detect a SvelteKit frontend, we search the DOM for a special | ||
* div that's inserted by SvelteKit when the page is rendered. It's identifyed | ||
* by its id, 'svelte-announcer', and it's used to improve page accessibility. | ||
* This div is not present when only using Svelte without SvelteKit. | ||
* | ||
* @see https://github.com/sveltejs/kit/issues/307 for more information | ||
*/ | ||
export function isSvelteKitApp(): boolean { | ||
return getDomElement('div#svelte-announcer') !== null; | ||
} |
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.
Where does
event.modules
show up? Is it something that is indexed and we can query for?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.
Yup - it displays in event details page, and is indexed - we can query for it in looker.
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.
@lforst, it's vaguely documented here