Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import type { ReactNode } from 'react';
import { Outlet, createRootRoute, HeadContent, Scripts } from '@tanstack/react-router';
import { getTraceData } from '@sentry/tanstackstart-react';

export const Route = createRootRoute({
head: () => {
const traceData = getTraceData();
const sentryMeta = Object.entries(traceData).map(([key, value]) => ({
name: key,
content: value,
}));

return {
meta: [
{
charSet: 'utf-8',
},
{
name: 'viewport',
content: 'width=device-width, initial-scale=1',
},
{
title: 'TanStack Start Cloudflare E2E Test',
},
...sentryMeta,
],
};
},
head: () => ({
meta: [
{
charSet: 'utf-8',
},
{
name: 'viewport',
content: 'width=device-width, initial-scale=1',
},
{
title: 'TanStack Start Cloudflare E2E Test',
},
],
}),
component: RootComponent,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test.describe('Trace propagation', () => {
test('should inject metatags in ssr pageload', async ({ page }) => {
await page.goto('/');

const sentryTraceContent = await page.getAttribute('meta[name="sentry-trace"]', 'content');
expect(sentryTraceContent).toBeDefined();
expect(sentryTraceContent).toMatch(/^[a-f0-9]{32}-[a-f0-9]{16}-[01]$/);

const baggageContent = await page.getAttribute('meta[name="baggage"]', 'content');
expect(baggageContent).toBeDefined();
expect(baggageContent).toContain('sentry-environment=qa');
expect(baggageContent).toContain('sentry-public_key=');
expect(baggageContent).toContain('sentry-trace_id=');
expect(baggageContent).toContain('sentry-sampled=');
});

test('should have trace connection between server and client', async ({ page }) => {
const serverTxPromise = waitForTransaction('tanstackstart-react-cloudflare', transactionEvent => {
return transactionEvent?.contexts?.trace?.op === 'http.server' && transactionEvent?.transaction === 'GET /';
});

const clientTxPromise = waitForTransaction('tanstackstart-react-cloudflare', transactionEvent => {
return transactionEvent?.contexts?.trace?.op === 'pageload' && transactionEvent?.transaction === '/';
});

await page.goto('/');

const serverTx = await serverTxPromise;
const clientTx = await clientTxPromise;

expect(clientTx.contexts?.trace?.trace_id).toBe(serverTx.contexts?.trace?.trace_id);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,3 @@ test('Sends server-side transaction for page request', async ({ baseURL }) => {
status: 'ok',
});
});

test('Propagates trace from server to client', async ({ page }) => {
const serverTransactionPromise = waitForTransaction('tanstackstart-react-cloudflare', transactionEvent => {
return transactionEvent?.contexts?.trace?.op === 'http.server' && transactionEvent?.transaction === 'GET /';
});

const clientTransactionPromise = waitForTransaction('tanstackstart-react-cloudflare', transactionEvent => {
return transactionEvent?.contexts?.trace?.op === 'pageload' && transactionEvent?.transaction === '/';
});

await page.goto('/');

const serverTransaction = await serverTransactionPromise;
const clientTransaction = await clientTransactionPromise;

const serverTraceId = serverTransaction.contexts?.trace?.trace_id;
const clientTraceId = clientTransaction.contexts?.trace?.trace_id;

expect(serverTraceId).toMatch(/[a-f0-9]{32}/);
expect(clientTraceId).toMatch(/[a-f0-9]{32}/);
expect(clientTraceId).toBe(serverTraceId);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

const usesManagedTunnelRoute =
(process.env.E2E_TEST_TUNNEL_ROUTE_MODE ?? 'off') !== 'off' || process.env.E2E_TEST_CUSTOM_TUNNEL_ROUTE === '1';

test.skip(usesManagedTunnelRoute, 'Default e2e suites run only in the proxy variant');

test.describe('Trace propagation', () => {
test('should inject metatags in ssr pageload', async ({ page }) => {
await page.goto('/');

const sentryTraceContent = await page.getAttribute('meta[name="sentry-trace"]', 'content');
expect(sentryTraceContent).toBeDefined();
expect(sentryTraceContent).toMatch(/^[a-f0-9]{32}-[a-f0-9]{16}-[01]$/);

const baggageContent = await page.getAttribute('meta[name="baggage"]', 'content');
expect(baggageContent).toBeDefined();
expect(baggageContent).toContain('sentry-environment=qa');
expect(baggageContent).toContain('sentry-public_key=');
expect(baggageContent).toContain('sentry-trace_id=');
expect(baggageContent).toContain('sentry-sampled=');
});

test('should have trace connection between server and client', async ({ page }) => {
const serverTxPromise = waitForTransaction('tanstackstart-react', transactionEvent => {
return transactionEvent?.contexts?.trace?.op === 'http.server' && transactionEvent?.transaction === 'GET /';
});

const clientTxPromise = waitForTransaction('tanstackstart-react', transactionEvent => {
return transactionEvent?.contexts?.trace?.op === 'pageload' && transactionEvent?.transaction === '/';
});

await page.goto('/');

const serverTx = await serverTxPromise;
const clientTx = await clientTxPromise;

expect(clientTx.contexts?.trace?.trace_id).toBe(serverTx.contexts?.trace?.trace_id);
});
});
105 changes: 102 additions & 3 deletions packages/tanstackstart-react/src/server/wrapFetchWithSentry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,110 @@
import { flushIfServerless } from '@sentry/core';
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/node';
import { flushIfServerless, getTraceMetaTags } from '@sentry/core';
import {
captureException,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
startSpan,
} from '@sentry/node';
import { extractServerFunctionSha256 } from './utils';

export type ServerEntry = {
fetch: (request: Request, opts?: unknown) => Promise<Response> | Response;
};

/**
* This function optimistically assumes that the HTML coming in chunks will not be split
* within the <head> tag. If this still happens, we simply won't replace anything.
*/
function addMetaTagToHead(htmlChunk: string, metaTagsStr: string): string {
if (typeof htmlChunk !== 'string' || !metaTagsStr) {
return htmlChunk;
}

if (htmlChunk.includes('"sentry-trace"')) {
return htmlChunk;
}

// Skip quoted attribute values so we don't match <head> inside e.g. data-code="...<head>..."
let replaced = false;
return htmlChunk.replace(/"[^"]*"|'[^']*'|(<head>)/g, (match, headTag) => {
if (headTag && !replaced) {
replaced = true;
return `<head>${metaTagsStr}`;
}
return match;
});
}

function injectMetaTagsInResponse(originalResponse: Response): Response {
try {
const contentType = originalResponse.headers.get('content-type');

const isPageloadRequest = contentType?.startsWith('text/html');
if (!isPageloadRequest) {
return originalResponse;
}

// Type case necessary b/c the body's ReadableStream type doesn't include
// the async iterator that is actually available in Node
// We later on use the async iterator to read the body chunks
// see https://github.com/microsoft/TypeScript/issues/39051
const originalBody = originalResponse.body as NodeJS.ReadableStream | null;
if (!originalBody) {
return originalResponse;
}

const metaTagsStr = getTraceMetaTags();
const decoder = new TextDecoder();

const newResponseStream = new ReadableStream({
start: async controller => {
// Assign to a new variable to avoid TS losing the narrower type checked above.
const body = originalBody;

async function* bodyReporter(): AsyncGenerator<string | Buffer> {
try {
for await (const chunk of body) {
yield chunk;
}
} catch (e) {
captureException(e, {
mechanism: { type: 'auto.http.tanstackstart', handled: false },
});
throw e;
}
}

let errored = false;
try {
for await (const chunk of bodyReporter()) {
const html = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
const modifiedHtml = addMetaTagToHead(html, metaTagsStr);
controller.enqueue(new TextEncoder().encode(modifiedHtml));
}
Comment thread
nicohrubec marked this conversation as resolved.
} catch (e) {
errored = true;
controller.error(e);
} finally {
if (!errored) {
controller.close();
}
}
Comment thread
cursor[bot] marked this conversation as resolved.
},
});

return new Response(newResponseStream, {
status: originalResponse.status,
statusText: originalResponse.statusText,
headers: new Headers(originalResponse.headers),
});
} catch (e) {
captureException(e, {
mechanism: { type: 'auto.http.tanstackstart', handled: false },
});
throw e;
}
}
Comment thread
nicohrubec marked this conversation as resolved.

/**
* This function can be used to wrap the server entry request handler to add tracing to server-side functionality.
* You must explicitly define a server entry point in your application for this to work. This is done by passing the request handler to the `createServerEntry` function.
Expand Down Expand Up @@ -62,7 +161,7 @@ export function wrapFetchWithSentry(serverEntry: ServerEntry): ServerEntry {
);
}

return await target.apply(thisArg, args);
return injectMetaTagsInResponse(await target.apply(thisArg, args));
} finally {
await flushIfServerless();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ vi.mock('@sentry/node', async importOriginal => {
};
});

const getTraceMetaTagsSpy = vi
.fn()
.mockReturnValue(
'<meta name="sentry-trace" content="abc123-def456-1"/>\n<meta name="baggage" content="sentry-trace_id=abc123"/>',
);

vi.mock('@sentry/core', async importOriginal => {
const original = await importOriginal();
return {
...original,
flushIfServerless: (...args: unknown[]) => flushIfServerlessSpy(...args),
getTraceMetaTags: () => getTraceMetaTagsSpy(),
};
});

Expand Down Expand Up @@ -53,6 +60,94 @@ describe('wrapFetchWithSentry', () => {
expect(flushIfServerlessSpy).toHaveBeenCalledTimes(1);
});

it('injects meta tags into HTML responses', async () => {
const mockResponse = new Response('<head><meta charset="utf-8"/></head><body></body>', {
headers: new Headers({ 'content-type': 'text/html; charset=utf-8' }),
});
const fetchFn = vi.fn().mockResolvedValue(mockResponse);

const serverEntry = wrapFetchWithSentry({ fetch: fetchFn });
const request = new Request('http://localhost:3000/');

const response = await serverEntry.fetch(request);
const html = await response.text();

expect(html).toContain('<meta name="sentry-trace" content="abc123-def456-1"/>');
expect(html).toContain('<meta name="baggage" content="sentry-trace_id=abc123"/>');
expect(html).toContain('<meta charset="utf-8"/>');
});

it('does not inject meta tags into non-HTML responses', async () => {
const mockResponse = new Response('{"data": "value"}', {
headers: new Headers({ 'content-type': 'application/json' }),
});
const fetchFn = vi.fn().mockResolvedValue(mockResponse);

const serverEntry = wrapFetchWithSentry({ fetch: fetchFn });
const request = new Request('http://localhost:3000/_serverFn/abc123');

const response = await serverEntry.fetch(request);
const body = await response.text();

expect(body).toBe('{"data": "value"}');
expect(body).not.toContain('sentry-trace');
});

it('does not inject duplicate meta tags if sentry-trace already exists', async () => {
const existingHtml =
'<head><meta name="sentry-trace" content="existing-trace"/><meta name="baggage" content="existing-baggage"/></head>';
const mockResponse = new Response(existingHtml, {
headers: new Headers({ 'content-type': 'text/html' }),
});
const fetchFn = vi.fn().mockResolvedValue(mockResponse);

const serverEntry = wrapFetchWithSentry({ fetch: fetchFn });
const request = new Request('http://localhost:3000/');

const response = await serverEntry.fetch(request);
const html = await response.text();

expect(html).toBe(existingHtml);
});

it('preserves response status and headers when injecting meta tags', async () => {
const mockResponse = new Response('<head></head>', {
status: 201,
statusText: 'Created',
headers: new Headers({
'content-type': 'text/html',
'X-Custom-Header': 'custom-value',
}),
});
const fetchFn = vi.fn().mockResolvedValue(mockResponse);

const serverEntry = wrapFetchWithSentry({ fetch: fetchFn });
const request = new Request('http://localhost:3000/');

const response = await serverEntry.fetch(request);

expect(response.status).toBe(201);
expect(response.statusText).toBe('Created');
expect(response.headers.get('content-type')).toBe('text/html');
expect(response.headers.get('X-Custom-Header')).toBe('custom-value');
});

it('does not inject meta tags into <head> inside quoted attribute values', async () => {
const mockResponse = new Response('<head></head><body><div data-content="<head>ignore"></div></body>', {
headers: new Headers({ 'content-type': 'text/html' }),
});
const fetchFn = vi.fn().mockResolvedValue(mockResponse);

const serverEntry = wrapFetchWithSentry({ fetch: fetchFn });
const request = new Request('http://localhost:3000/');

const response = await serverEntry.fetch(request);
const html = await response.text();

expect(html).toContain('<head><meta name="sentry-trace"');
expect(html).toContain('data-content="<head>ignore"');
});

it('calls flushIfServerless even if the handler throws', async () => {
const fetchFn = vi.fn().mockRejectedValue(new Error('handler error'));

Expand Down
Loading