Skip to content

fix(sveltekit): Improve server-side grouping by removing stack frame module #7835

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

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/sveltekit/src/server/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { init as initNodeSdk, Integrations } from '@sentry/node';
import { addOrUpdateIntegration } from '@sentry/utils';

import { applySdkMetadata } from '../common/metadata';
import { rewriteFramesIteratee } from './utils';

/**
*
Expand All @@ -24,5 +25,8 @@ export function init(options: NodeOptions): void {

function addServerIntegrations(options: NodeOptions): void {
options.integrations = addOrUpdateIntegration(new Integrations.Undici(), options.integrations || []);
options.integrations = addOrUpdateIntegration(new RewriteFrames(), options.integrations || []);
options.integrations = addOrUpdateIntegration(
new RewriteFrames({ iteratee: rewriteFramesIteratee }),
options.integrations || [],
);
}
41 changes: 39 additions & 2 deletions packages/sveltekit/src/server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DynamicSamplingContext, TraceparentData } from '@sentry/types';
import { baggageHeaderToDynamicSamplingContext, extractTraceparentData } from '@sentry/utils';
import type { DynamicSamplingContext, StackFrame, TraceparentData } from '@sentry/types';
import { baggageHeaderToDynamicSamplingContext, basename, extractTraceparentData } from '@sentry/utils';
import type { RequestEvent } from '@sveltejs/kit';

/**
Expand All @@ -17,3 +17,40 @@ export function getTracePropagationData(event: RequestEvent): {

return { traceparentData, dynamicSamplingContext };
}

/**
* 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.
*/
export function rewriteFramesIteratee(frame: StackFrame): StackFrame {
if (!frame.filename) {
return frame;
}

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;

const base = basename(filename);
frame.filename = `${prefix}${base}`;
}

delete frame.module;

return frame;
}
44 changes: 43 additions & 1 deletion packages/sveltekit/test/server/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getTracePropagationData } from '../../src/server/utils';
import { RewriteFrames } from '@sentry/integrations';
import type { StackFrame } from '@sentry/types';

import { getTracePropagationData, rewriteFramesIteratee } from '../../src/server/utils';

const MOCK_REQUEST_EVENT: any = {
request: {
Expand Down Expand Up @@ -53,3 +56,42 @@ describe('getTracePropagationData', () => {
expect(dynamicSamplingContext).toBeUndefined();
});
});

describe('rewriteFramesIteratee', () => {
it('removes the module property from the frame', () => {
const frame: StackFrame = {
filename: '/some/path/to/server/chunks/3-ab34d22f.js',
module: '3-ab34d22f.js',
};

const result = rewriteFramesIteratee(frame);

expect(result).not.toHaveProperty('module');
});

it('does the same filename modification as the default RewriteFrames iteratee', () => {
const frame: StackFrame = {
filename: '/some/path/to/server/chunks/3-ab34d22f.js',
lineno: 1,
colno: 1,
module: '3-ab34d22f.js',
};

const originalRewriteFrames = new RewriteFrames();
// @ts-ignore this property exists
const defaultIteratee = originalRewriteFrames._iteratee;

const defaultResult = defaultIteratee({ ...frame });
delete defaultResult.module;

const result = rewriteFramesIteratee({ ...frame });

expect(result).toEqual({
filename: 'app:///3-ab34d22f.js',
lineno: 1,
colno: 1,
});

expect(result).toStrictEqual(defaultResult);
});
});