Skip to content
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

fix(sveltekit): Avoid double-wrapping load functions #8094

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/sveltekit/src/client/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {
} from '@sentry/utils';
import type { LoadEvent } from '@sveltejs/kit';

import type { SentryWrappedFlag } from '../common/utils';
import { isRedirect } from '../common/utils';

type PatchedLoadEvent = LoadEvent & Partial<SentryWrappedFlag>;

function sendErrorToSentry(e: unknown): unknown {
// In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can
// store a seen flag on it.
Expand Down Expand Up @@ -66,11 +69,17 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
return new Proxy(origLoad, {
apply: (wrappingTarget, thisArg, args: Parameters<T>) => {
// Type casting here because `T` cannot extend `Load` (see comment above function signature)
const event = args[0] as LoadEvent;
const event = args[0] as PatchedLoadEvent;

// Check if already wrapped
if (event.__sentry_wrapped__) {
return wrappingTarget.apply(thisArg, args);
}

const patchedEvent = {
const patchedEvent: PatchedLoadEvent = {
...event,
fetch: instrumentSvelteKitFetch(event.fetch),
__sentry_wrapped__: true,
};

const routeId = event.route.id;
Expand Down
8 changes: 8 additions & 0 deletions packages/sveltekit/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import type { Redirect } from '@sveltejs/kit';

export type SentryWrappedFlag = {
/**
* If this flag is set, we know that the load event was already wrapped once
* and we shouldn't wrap it again.
*/
__sentry_wrapped__?: true;
};

/**
* Determines if a thrown "error" is a Redirect object which SvelteKit users can throw to redirect to another route
* see: https://kit.svelte.dev/docs/modules#sveltejs-kit-redirect
Expand Down
34 changes: 30 additions & 4 deletions packages/sveltekit/src/server/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import type { TransactionContext } from '@sentry/types';
import { addExceptionMechanism, objectify } from '@sentry/utils';
import type { HttpError, LoadEvent, ServerLoadEvent } from '@sveltejs/kit';

import type { SentryWrappedFlag } from '../common/utils';
import { isRedirect } from '../common/utils';
import { getTracePropagationData } from './utils';

type PatchedLoadEvent = LoadEvent & SentryWrappedFlag;
type PatchedServerLoadEvent = ServerLoadEvent & SentryWrappedFlag;

function isHttpError(err: unknown): err is HttpError {
return typeof err === 'object' && err !== null && 'status' in err && 'body' in err;
}
Expand Down Expand Up @@ -59,7 +63,18 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
return new Proxy(origLoad, {
apply: (wrappingTarget, thisArg, args: Parameters<T>) => {
// Type casting here because `T` cannot extend `Load` (see comment above function signature)
const event = args[0] as LoadEvent;
// Also, this event possibly already has a sentry wrapped flag attached
const event = args[0] as PatchedLoadEvent;

if (event.__sentry_wrapped__) {
return wrappingTarget.apply(thisArg, args);
}

const patchedEvent: PatchedLoadEvent = {
...event,
__sentry_wrapped__: true,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: Can we use addNonEnumerableProperty and mutate the original event? This way we can just do wrappingTarget.apply(thisArg, args) like before, which is more safe if more args are added in the future.

Suggested change
const patchedEvent: PatchedLoadEvent = {
...event,
__sentry_wrapped__: true,
};
addNonEnumerableProperty(event, '__sentry_wrapped__', true);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can do that easily for the server-side, good call! On the client side, we have to patch the event for the fetch instrumentation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1d1dd92


const routeId = event.route && event.route.id;

const traceLoadContext: TransactionContext = {
Expand All @@ -71,7 +86,7 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
},
};

return trace(traceLoadContext, () => wrappingTarget.apply(thisArg, args), sendErrorToSentry);
return trace(traceLoadContext, () => wrappingTarget.apply(thisArg, [patchedEvent]), sendErrorToSentry);
},
});
}
Expand Down Expand Up @@ -102,7 +117,18 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe
return new Proxy(origServerLoad, {
apply: (wrappingTarget, thisArg, args: Parameters<T>) => {
// Type casting here because `T` cannot extend `ServerLoad` (see comment above function signature)
const event = args[0] as ServerLoadEvent;
// Also, this event possibly already has a sentry wrapped flag attached
const event = args[0] as PatchedServerLoadEvent;

if (event.__sentry_wrapped__) {
return wrappingTarget.apply(thisArg, args);
}

const patchedEvent: PatchedServerLoadEvent = {
...event,
__sentry_wrapped__: true,
};

const routeId = event.route && event.route.id;

const { dynamicSamplingContext, traceparentData } = getTracePropagationData(event);
Expand All @@ -121,7 +147,7 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe
...traceparentData,
};

return trace(traceLoadContext, () => wrappingTarget.apply(thisArg, args), sendErrorToSentry);
return trace(traceLoadContext, () => wrappingTarget.apply(thisArg, [patchedEvent]), sendErrorToSentry);
},
});
}
13 changes: 13 additions & 0 deletions packages/sveltekit/test/client/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,17 @@ describe('wrapLoadWithSentry', () => {
{ handled: false, type: 'sveltekit', data: { function: 'load' } },
);
});

it("doesn't wrap load more than once if the wrapper was applied multiple times", async () => {
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
post: params.id,
};
}

const wrappedLoad = wrapLoadWithSentry(wrapLoadWithSentry(load));
await wrappedLoad(MOCK_LOAD_ARGS);

expect(mockTrace).toHaveBeenCalledTimes(1);
});
});
14 changes: 14 additions & 0 deletions packages/sveltekit/test/server/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ describe('wrapLoadWithSentry calls trace', () => {
expect.any(Function),
);
});

it("doesn't wrap load more than once if the wrapper was applied multiple times", async () => {
const wrappedLoad = wrapLoadWithSentry(wrapLoadWithSentry(wrapLoadWithSentry(load)));
await wrappedLoad(MOCK_LOAD_ARGS);

expect(mockTrace).toHaveBeenCalledTimes(1);
});
});

describe('wrapServerLoadWithSentry calls trace', () => {
Expand Down Expand Up @@ -375,4 +382,11 @@ describe('wrapServerLoadWithSentry calls trace', () => {
expect.any(Function),
);
});

it("doesn't wrap server load more than once if the wrapper was applied multiple times", async () => {
const wrappedLoad = wrapServerLoadWithSentry(wrapServerLoadWithSentry(serverLoad));
await wrappedLoad(MOCK_SERVER_ONLY_LOAD_ARGS);

expect(mockTrace).toHaveBeenCalledTimes(1);
});
});