Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(tanstackstart-react): Add distributed tracing#21144
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
694bff8c8e927c295b57ac5b8f100c8b11894fb6a06fc3d1153a6c2bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
| @@ -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); | ||
| }); | ||
| }); |
| 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; | ||
| } | ||
nicohrubec marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // 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 }, | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could also test that those exceptions are correctly captured. | ||
| }); | ||
| 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)); | ||
| } | ||
nicohrubec marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. nicohrubec marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } catch (e) { | ||
| errored = true; | ||
| controller.error(e); | ||
| } finally { | ||
| if (!errored) { | ||
| controller.close(); | ||
| } | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }, | ||
| }); | ||
| 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; | ||
| } | ||
| } | ||
nicohrubec marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * 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. | ||
| @@ -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(); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.