diff --git a/dev-packages/e2e-tests/test-applications/nextjs-16/tests/next-dev-internal-requests.streaming-lifecycle.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-16/tests/next-dev-internal-requests.streaming-lifecycle.test.ts new file mode 100644 index 000000000000..6f6f42e1bab9 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nextjs-16/tests/next-dev-internal-requests.streaming-lifecycle.test.ts @@ -0,0 +1,29 @@ +import { expect, test } from '@playwright/test'; +import { getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils'; +import { isDevMode } from './isDevMode'; + +const NEXT_DEV_REQUEST_URL = + /^https:\/\/(telemetry\.nextjs\.org\/api\/v1\/record|registry\.npmjs\.org\/-\/package\/next\/dist-tags)$/; + +test('Sends no spans for the telemetry and version check requests of `next dev`', async ({ page }) => { + test.skip(!isDevMode, '`next dev` sends these requests only in development'); + + // `next dev` sends its telemetry at startup, before any test runs, so the search starts at the first envelope. + const nextDevRequestSpanPromise = waitForStreamedSpan( + 'nextjs-16', + span => { + const url = span.attributes['url.full']?.value; + return getSpanOp(span) === 'http.client' && typeof url === 'string' && NEXT_DEV_REQUEST_URL.test(url); + }, + 0, + ); + + // The dev overlay connects to the dev server, which then runs the version check. + await page.goto('/'); + + const result = await Promise.race([ + nextDevRequestSpanPromise.then(span => span.name), + new Promise(resolve => setTimeout(() => resolve('timeout'), 5000)), + ]); + expect(result).toBe('timeout'); +}); diff --git a/packages/nextjs/src/server/serverSpanHooks.ts b/packages/nextjs/src/server/serverSpanHooks.ts index b1ad6f9991c5..2c8dd3483f65 100644 --- a/packages/nextjs/src/server/serverSpanHooks.ts +++ b/packages/nextjs/src/server/serverSpanHooks.ts @@ -5,8 +5,9 @@ import { HTTP_ROUTE, SENTRY_OP, SENTRY_ORIGIN, + URL_FULL, } from '@sentry/conventions/attributes'; -import { MIDDLEWARE } from '@sentry/conventions/op'; +import { HTTP_CLIENT, MIDDLEWARE } from '@sentry/conventions/op'; import type { Client, Options, Span } from '@sentry/core'; import { getIsolationScope, @@ -49,6 +50,15 @@ export const NEXTJS_SERVER_IGNORE_SPANS: NonNullable = [ // Spans flagged via TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION // (set in `dropMiddlewareTunnelRequests` during `spanStart`) { attributes: { [TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION]: true } }, + // Requests that `next dev` sends on its own, for its telemetry and its version check. They are not part of a + // request, so with span streaming they would each become a root span. + { + op: HTTP_CLIENT, + attributes: { + [URL_FULL]: + /^https:\/\/(telemetry\.nextjs\.org\/api\/v1\/record|registry\.npmjs\.org\/-\/package\/next\/dist-tags)$/, + }, + }, ]; /**