Skip to content

fix(sveltekit): Termporarily disable serverside load tracing #7587

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 2 commits into from
Mar 23, 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
4 changes: 2 additions & 2 deletions packages/sveltekit/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export * from './server';

import type { Integration, Options, StackParser } from '@sentry/types';
// eslint-disable-next-line import/no-unresolved
import type { HandleClientError, HandleServerError, ServerLoad } from '@sveltejs/kit';
import type { HandleClientError, HandleServerError, Load, ServerLoad } from '@sveltejs/kit';

import type * as clientSdk from './client';
import type * as serverSdk from './server';
Expand All @@ -21,7 +21,7 @@ export declare function handleErrorWithSentry<T extends HandleClientError | Hand
handleError?: T,
): ReturnType<T>;

export declare function wrapLoadWithSentry<S extends ServerLoad>(origLoad: S): S;
export declare function wrapLoadWithSentry<S extends ServerLoad | Load>(origLoad: S): S;

// We export a merged Integrations object so that users can (at least typing-wise) use all integrations everywhere.
export declare const Integrations: typeof clientSdk.Integrations & typeof serverSdk.Integrations;
Expand Down
46 changes: 17 additions & 29 deletions packages/sveltekit/src/server/load.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
/* eslint-disable @sentry-internal/sdk/no-optional-chaining */
import { trace } from '@sentry/core';
import { captureException } from '@sentry/node';
import {
addExceptionMechanism,
baggageHeaderToDynamicSamplingContext,
extractTraceparentData,
objectify,
} from '@sentry/utils';
import type { HttpError, ServerLoad } from '@sveltejs/kit';
import { addExceptionMechanism, isThenable, objectify } from '@sentry/utils';
import type { HttpError, Load, ServerLoad } from '@sveltejs/kit';
import * as domain from 'domain';

function isHttpError(err: unknown): err is HttpError {
Expand Down Expand Up @@ -49,32 +43,26 @@ function sendErrorToSentry(e: unknown): unknown {
*
* @param origLoad SvelteKit user defined load function
*/
export function wrapLoadWithSentry(origLoad: ServerLoad): ServerLoad {
export function wrapLoadWithSentry<T extends ServerLoad | Load>(origLoad: T): T {
return new Proxy(origLoad, {
apply: (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => {
return domain.create().bind(() => {
const [event] = args;
let maybePromiseResult: ReturnType<T>;

const sentryTraceHeader = event.request.headers.get('sentry-trace');
const baggageHeader = event.request.headers.get('baggage');
const traceparentData = sentryTraceHeader ? extractTraceparentData(sentryTraceHeader) : undefined;
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader);
try {
maybePromiseResult = wrappingTarget.apply(thisArg, args);
} catch (e) {
sendErrorToSentry(e);
throw e;
}

const routeId = event.route.id;
return trace(
{
op: 'function.sveltekit.load',
name: routeId ? routeId : event.url.pathname,
status: 'ok',
...traceparentData,
metadata: {
source: routeId ? 'route' : 'url',
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
},
},
() => wrappingTarget.apply(thisArg, args),
sendErrorToSentry,
);
if (isThenable(maybePromiseResult)) {
Promise.resolve(maybePromiseResult).then(null, e => {
sendErrorToSentry(e);
});
}

return maybePromiseResult;
})();
},
});
Expand Down
3 changes: 2 additions & 1 deletion packages/sveltekit/test/server/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ describe('wrapLoadWithSentry', () => {
expect(mockCaptureException).toHaveBeenCalledTimes(1);
});

it('calls trace function', async () => {
// TODO: enable this once we figured out how tracing the load function doesn't result in creating a new transaction
it.skip('calls trace function', async () => {
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
return {
post: params.id,
Expand Down