Skip to content
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
11 changes: 10 additions & 1 deletion packages/tanstackstart-react/src/server/wrapFetchWithSentry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { getTraceMetaTags } from '@sentry/core';
import { getClient, getTraceMetaTags, SDK_VERSION } from '@sentry/core';
import { flushIfServerless } from '@sentry/core/server';
import { captureException } from '@sentry/node';
import { updateSpanWithRouteParametrization } from './routeParametrization';

declare const __SENTRY_ROUTE_PATTERNS__: string[] | undefined;

const SDK_NAME = 'sentry.javascript.tanstackstart-react';

export type ServerEntry = {
// `opts` is forwarded verbatim to the wrapped handler, so this must accept whatever shape
// the real framework entry uses (e.g. TanStack's `RequestOptions<Register>`). Under
Expand Down Expand Up @@ -136,6 +138,13 @@ export function wrapFetchWithSentry(serverEntry: ServerEntry): ServerEntry {
serverEntry.fetch = new Proxy<typeof serverEntry.fetch>(serverEntry.fetch, {
async apply(target, thisArg, args) {
try {
// With `sentryCloudflareVitePlugin()` of `@sentry/cloudflare/vite`, `@sentry/cloudflare` inits the client.
const sdk = getClient()?.getOptions()._metadata?.sdk;
if (sdk && sdk.name !== SDK_NAME) {
sdk.name = SDK_NAME;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since the SDK is renamed only after the client is created, the sentry_client param in the envelope endpoint URL will still say cloudflare, and only the event payload says tanstackstart-react. Same as Astro, so probably fine?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah it is fine. It’s a good point though to fix it even for the beginning as well. Might only work with Vite I guess. I’ll add a follow up task for that

sdk.packages = [{ name: 'npm:@sentry/tanstackstart-react', version: SDK_VERSION }, ...(sdk.packages || [])];
}

const request: Request = args[0];
const url = new URL(request.url);
const method = request.method || 'GET';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Client } from '@sentry/core';
import { getCurrentScope, SDK_VERSION } from '@sentry/core';
import { afterEach, describe, expect, it, vi } from 'vitest';

const flushIfServerlessSpy = vi.fn().mockResolvedValue(undefined);
Expand Down Expand Up @@ -40,6 +42,29 @@ const { wrapFetchWithSentry } = await import('../../src/server/wrapFetchWithSent
describe('wrapFetchWithSentry', () => {
afterEach(() => {
vi.clearAllMocks();
getCurrentScope().setClient(undefined);
});

it('sets the TanStack Start SDK name on a client that another SDK inited', async () => {
const sdk = {
name: 'sentry.javascript.cloudflare',
packages: [{ name: 'npm:@sentry/cloudflare', version: SDK_VERSION }],
version: SDK_VERSION,
};
getCurrentScope().setClient({ getOptions: () => ({ _metadata: { sdk } }) } as unknown as Client);
const serverEntry = wrapFetchWithSentry({ fetch: () => new Response('ok') });

await serverEntry.fetch(new Request('http://localhost:3000/page'));
await serverEntry.fetch(new Request('http://localhost:3000/_serverFn/abc123'));

expect(sdk).toEqual({
name: 'sentry.javascript.tanstackstart-react',
packages: [
{ name: 'npm:@sentry/tanstackstart-react', version: SDK_VERSION },
{ name: 'npm:@sentry/cloudflare', version: SDK_VERSION },
],
version: SDK_VERSION,
});
});

it('calls flushIfServerless after a regular request', async () => {
Expand Down
Loading