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): Only instrument SvelteKit fetch if the SDK client is valid #8381

Merged
merged 2 commits into from
Jun 22, 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
25 changes: 19 additions & 6 deletions packages/sveltekit/src/client/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BaseClient } from '@sentry/core';
import { getCurrentHub, trace } from '@sentry/core';
import type { Breadcrumbs, BrowserTracing } from '@sentry/svelte';
import { captureException } from '@sentry/svelte';
import type { ClientOptions, SanitizedRequestData } from '@sentry/types';
import type { Client, ClientOptions, SanitizedRequestData } from '@sentry/types';
import {
addExceptionMechanism,
addNonEnumerableProperty,
Expand Down Expand Up @@ -122,12 +122,14 @@ type SvelteKitFetch = LoadEvent['fetch'];
* @returns a proxy of SvelteKit's fetch implementation
*/
function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch {
const client = getCurrentHub().getClient() as BaseClient<ClientOptions>;
const client = getCurrentHub().getClient();

const browserTracingIntegration =
client.getIntegrationById && (client.getIntegrationById('BrowserTracing') as BrowserTracing | undefined);
const breadcrumbsIntegration =
client.getIntegrationById && (client.getIntegrationById('Breadcrumbs') as Breadcrumbs | undefined);
if (!isValidClient(client)) {
return originalFetch;
}

const browserTracingIntegration = client.getIntegrationById('BrowserTracing') as BrowserTracing | undefined;
const breadcrumbsIntegration = client.getIntegrationById('Breadcrumbs') as Breadcrumbs | undefined;

const browserTracingOptions = browserTracingIntegration && browserTracingIntegration.options;

Expand Down Expand Up @@ -270,3 +272,14 @@ function addFetchBreadcrumb(
},
);
}

type MaybeClientWithGetIntegrationsById =
| (Client & { getIntegrationById?: BaseClient<ClientOptions>['getIntegrationById'] })
| undefined;

type ClientWithGetIntegrationById = Required<MaybeClientWithGetIntegrationsById> &
Exclude<MaybeClientWithGetIntegrationsById, undefined>;

function isValidClient(client: MaybeClientWithGetIntegrationsById): client is ClientWithGetIntegrationById {
return !!client && typeof client.getIntegrationById === 'function';
Copy link
Member

Choose a reason for hiding this comment

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

m: Why are we additionally making sure getIntegrationById is there? That method is on the BaseClient so I think we can be pretty sure it's there. I think a check if the client exists at all is enough no?

Copy link
Member Author

@Lms24 Lms24 Jun 22, 2023

Choose a reason for hiding this comment

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

For the unlikely case that anyone would use a custom client that doesn't extend BaseClient. I'm aware that this is unlikely but then again, the check doesn't hurt us too much.
(iirc I initially did this also because getClient returns the Client interface on which this method isn't defined. So I needed to type-cast to BaseClient but decided to guard for the method)

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Sounds good!

}
34 changes: 29 additions & 5 deletions packages/sveltekit/test/client/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ const mockedGetIntegrationById = vi.fn(id => {
return undefined;
});

const mockedGetClient = vi.fn(() => {
return {
getIntegrationById: mockedGetIntegrationById,
};
});

vi.mock('@sentry/core', async () => {
const original = (await vi.importActual('@sentry/core')) as any;
return {
Expand All @@ -62,11 +68,7 @@ vi.mock('@sentry/core', async () => {
},
getCurrentHub: () => {
return {
getClient: () => {
return {
getIntegrationById: mockedGetIntegrationById,
};
},
getClient: mockedGetClient,
getScope: () => {
return {
getSpan: () => {
Expand Down Expand Up @@ -427,6 +429,28 @@ describe('wrapLoadWithSentry', () => {
});
});

it.each([
['is undefined', undefined],
["doesn't have a `getClientById` method", {}],
])("doesn't instrument fetch if the client %s", async (_, client) => {
// @ts-expect-error: we're mocking the client
mockedGetClient.mockImplementationOnce(() => client);

async function load(_event: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
msg: 'hi',
};
}
const wrappedLoad = wrapLoadWithSentry(load);

const originalFetch = MOCK_LOAD_ARGS.fetch;
await wrappedLoad(MOCK_LOAD_ARGS);

expect(MOCK_LOAD_ARGS.fetch).toStrictEqual(originalFetch);

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

it('adds an exception mechanism', async () => {
const addEventProcessorSpy = vi.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
void callback({}, { event_id: 'fake-event-id' });
Expand Down