-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sveltekit): Update default integration handling & deprecate `add…
…OrUpdateIntegration` (#10263) This updates the last usage of `addOrUpdateIntegration` and deprecates it.
- Loading branch information
Showing
11 changed files
with
253 additions
and
190 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/sveltekit/src/client/browserTracingIntegration.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,14 @@ | ||
import { BrowserTracing as OriginalBrowserTracing } from '@sentry/svelte'; | ||
import { svelteKitRoutingInstrumentation } from './router'; | ||
|
||
/** | ||
* A custom BrowserTracing integration for Sveltekit. | ||
*/ | ||
export class BrowserTracing extends OriginalBrowserTracing { | ||
public constructor(options?: ConstructorParameters<typeof OriginalBrowserTracing>[0]) { | ||
super({ | ||
routingInstrumentation: svelteKitRoutingInstrumentation, | ||
...options, | ||
}); | ||
} | ||
} |
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,77 @@ | ||
import { defineIntegration } from '@sentry/core'; | ||
import { rewriteFramesIntegration as originalRewriteFramesIntegration } from '@sentry/integrations'; | ||
import type { IntegrationFn, StackFrame } from '@sentry/types'; | ||
import { GLOBAL_OBJ, basename, escapeStringForRegex, join } from '@sentry/utils'; | ||
import { WRAPPED_MODULE_SUFFIX } from '../vite/autoInstrument'; | ||
import type { GlobalWithSentryValues } from '../vite/injectGlobalValues'; | ||
|
||
type StackFrameIteratee = (frame: StackFrame) => StackFrame; | ||
interface RewriteFramesOptions { | ||
root?: string; | ||
prefix?: string; | ||
iteratee?: StackFrameIteratee; | ||
} | ||
|
||
export const customRewriteFramesIntegration = ((options?: RewriteFramesOptions) => { | ||
return originalRewriteFramesIntegration({ | ||
iteratee: rewriteFramesIteratee, | ||
...options, | ||
}); | ||
}) satisfies IntegrationFn; | ||
|
||
export const rewriteFramesIntegration = defineIntegration(customRewriteFramesIntegration); | ||
|
||
/** | ||
* A custom iteratee function for the `RewriteFrames` integration. | ||
* | ||
* Does the same as the default iteratee, but also removes the `module` property from the | ||
* frame to improve issue grouping. | ||
* | ||
* For some reason, our stack trace processing pipeline isn't able to resolve the bundled | ||
* module name to the original file name correctly, leading to individual error groups for | ||
* each module. Removing the `module` field makes the grouping algorithm fall back to the | ||
* `filename` field, which is correctly resolved and hence grouping works as expected. | ||
* | ||
* Exported for tests only. | ||
*/ | ||
export function rewriteFramesIteratee(frame: StackFrame): StackFrame { | ||
if (!frame.filename) { | ||
return frame; | ||
} | ||
const globalWithSentryValues: GlobalWithSentryValues = GLOBAL_OBJ; | ||
const svelteKitBuildOutDir = globalWithSentryValues.__sentry_sveltekit_output_dir; | ||
const prefix = 'app:///'; | ||
|
||
// Check if the frame filename begins with `/` or a Windows-style prefix such as `C:\` | ||
const isWindowsFrame = /^[a-zA-Z]:\\/.test(frame.filename); | ||
const startsWithSlash = /^\//.test(frame.filename); | ||
if (isWindowsFrame || startsWithSlash) { | ||
const filename = isWindowsFrame | ||
? frame.filename | ||
.replace(/^[a-zA-Z]:/, '') // remove Windows-style prefix | ||
.replace(/\\/g, '/') // replace all `\\` instances with `/` | ||
: frame.filename; | ||
|
||
let strippedFilename; | ||
if (svelteKitBuildOutDir) { | ||
strippedFilename = filename.replace( | ||
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- not end user input + escaped anyway | ||
new RegExp(`^.*${escapeStringForRegex(join(svelteKitBuildOutDir, 'server'))}/`), | ||
'', | ||
); | ||
} else { | ||
strippedFilename = basename(filename); | ||
} | ||
frame.filename = `${prefix}${strippedFilename}`; | ||
} | ||
|
||
delete frame.module; | ||
|
||
// In dev-mode, the WRAPPED_MODULE_SUFFIX is still present in the frame's file name. | ||
// We need to remove it to make sure that the frame's filename matches the actual file | ||
if (frame.filename.endsWith(WRAPPED_MODULE_SUFFIX)) { | ||
frame.filename = frame.filename.slice(0, -WRAPPED_MODULE_SUFFIX.length); | ||
} | ||
|
||
return frame; | ||
} |
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 |
---|---|---|
@@ -1,29 +1,23 @@ | ||
import { applySdkMetadata, getCurrentScope } from '@sentry/core'; | ||
import { RewriteFrames } from '@sentry/integrations'; | ||
import type { NodeOptions } from '@sentry/node'; | ||
import { getDefaultIntegrations as getDefaultNodeIntegrations } from '@sentry/node'; | ||
import { init as initNodeSdk } from '@sentry/node'; | ||
import { addOrUpdateIntegration } from '@sentry/utils'; | ||
|
||
import { rewriteFramesIteratee } from './utils'; | ||
import { rewriteFramesIntegration } from './rewriteFramesIntegration'; | ||
|
||
/** | ||
* | ||
* @param options | ||
*/ | ||
export function init(options: NodeOptions): void { | ||
applySdkMetadata(options, 'sveltekit', ['sveltekit', 'node']); | ||
const opts = { | ||
defaultIntegrations: [...getDefaultNodeIntegrations(options), rewriteFramesIntegration()], | ||
...options, | ||
}; | ||
|
||
addServerIntegrations(options); | ||
applySdkMetadata(opts, 'sveltekit', ['sveltekit', 'node']); | ||
|
||
initNodeSdk(options); | ||
initNodeSdk(opts); | ||
|
||
getCurrentScope().setTag('runtime', 'node'); | ||
} | ||
|
||
function addServerIntegrations(options: NodeOptions): void { | ||
options.integrations = addOrUpdateIntegration( | ||
// eslint-disable-next-line deprecation/deprecation | ||
new RewriteFrames({ iteratee: rewriteFramesIteratee }), | ||
options.integrations || [], | ||
); | ||
} |
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
Oops, something went wrong.