Unverified Commit 2d139451 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Avoid port collisions in concurrent OpenTelemetry tests (#100538)

Both OpenTelemetry suites used fixed collector ports, and the
instrumentation suite also used a fixed gate-server port. These ports
could collide when #100525 runs multiple copies of a suite.

Collectors now bind to OS-assigned ports and pass their ports to the
fixtures before startup. The parent-span suite resets spans while its
collector remains open. The gate server uses a dynamically selected
port. The streamed-render helper starts the gate timeout after response
headers, so cold dev compilation does not consume the deadline.
parent 3a0a7caf
import { Server as HttpServer } from 'node:http' import { Server as HttpServer } from 'node:http'
import type { AddressInfo } from 'node:net'
import { SavedSpan } from './constants' import { SavedSpan } from './constants'
export interface Collector { export interface Collector {
readonly port: number
getSpans: () => SavedSpan[] getSpans: () => SavedSpan[]
reset: () => void
shutdown: () => Promise<void> shutdown: () => Promise<void>
} }
export async function connectCollector({ export async function connectCollector(): Promise<Collector> {
port,
}: {
port: number
}): Promise<Collector> {
const spans: SavedSpan[] = [] const spans: SavedSpan[] = []
const server = new HttpServer(async (req, res) => { const server = new HttpServer(async (req, res) => {
...@@ -38,19 +37,18 @@ export async function connectCollector({ ...@@ -38,19 +37,18 @@ export async function connectCollector({
}) })
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
server.listen(port, (err?: Error) => { server.once('error', reject)
if (err) { server.listen(0, resolve)
reject(err)
} else {
resolve()
}
})
}) })
return { return {
port: (server.address() as AddressInfo).port,
getSpans() { getSpans() {
return spans return spans
}, },
reset() {
spans.length = 0
},
shutdown() { shutdown() {
return new Promise<void>((resolve, reject) => return new Promise<void>((resolve, reject) =>
server.close((err) => (err ? reject(err) : resolve())) server.close((err) => (err ? reject(err) : resolve()))
......
...@@ -2,29 +2,33 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -2,29 +2,33 @@ import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
import { type Collector, connectCollector } from './collector' import { type Collector, connectCollector } from './collector'
const COLLECTOR_PORT = 9876
// TODO(deploy-test-completion): Re-enable this suite in deploy mode. // TODO(deploy-test-completion): Re-enable this suite in deploy mode.
// No deploy-specific incompatibility is documented. // No deploy-specific incompatibility is documented.
// @force-gate !deploy // @force-gate !deploy
describe('otel-parent-span-propagation', () => { describe('otel-parent-span-propagation', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
skipStart: true,
dependencies: require('./package.json').dependencies, dependencies: require('./package.json').dependencies,
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(COLLECTOR_PORT),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
}, },
}) })
let collector: Collector let collector: Collector
beforeEach(async () => { beforeAll(async () => {
collector = await connectCollector({ port: COLLECTOR_PORT }) collector = await connectCollector()
next.env.TEST_OTEL_COLLECTOR_PORT = String(collector.port)
await next.start()
})
beforeEach(() => {
collector.reset()
}) })
afterEach(async () => { afterAll(async () => {
await collector.shutdown() await collector?.shutdown()
}) })
// Verifies that http.route is set on the handleRequest span. // Verifies that http.route is set on the handleRequest span.
......
import { Server as HttpServer } from 'node:http' import { Server as HttpServer } from 'node:http'
import type { AddressInfo } from 'node:net'
import { SavedSpan } from './constants' import { SavedSpan } from './constants'
export interface Collector { export interface Collector {
readonly port: number
getSpans: () => SavedSpan[] getSpans: () => SavedSpan[]
/** /**
* Like `getSpans`, but also includes spans marked `next.bubble` (e.g. * Like `getSpans`, but also includes spans marked `next.bubble` (e.g.
...@@ -13,11 +15,7 @@ export interface Collector { ...@@ -13,11 +15,7 @@ export interface Collector {
shutdown: () => Promise<void> shutdown: () => Promise<void>
} }
export async function connectCollector({ export async function connectCollector(): Promise<Collector> {
port,
}: {
port: number
}): Promise<Collector> {
const spans: SavedSpan[] = [] const spans: SavedSpan[] = []
const allSpans: SavedSpan[] = [] const allSpans: SavedSpan[] = []
...@@ -56,16 +54,12 @@ export async function connectCollector({ ...@@ -56,16 +54,12 @@ export async function connectCollector({
}) })
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
server.listen(port, (err?: Error) => { server.once('error', reject)
if (err) { server.listen(0, resolve)
reject(err)
} else {
resolve()
}
})
}) })
return { return {
port: (server.address() as AddressInfo).port,
getSpans() { getSpans() {
return spans return spans
}, },
......
import { FileRef, isNextDev, isNextStart, nextTestSetup } from 'e2e-utils' import { FileRef, isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { findPort, retry } from 'next-test-utils'
import { createTestDataServer } from 'test-data-service/writer' import { createTestDataServer } from 'test-data-service/writer'
import { createTestLog } from 'test-log' import { createTestLog } from 'test-log'
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers' import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
...@@ -13,21 +13,16 @@ const EXTERNAL = { ...@@ -13,21 +13,16 @@ const EXTERNAL = {
spanId: '0f6a325411bdc432', spanId: '0f6a325411bdc432',
} as const } as const
const COLLECTOR_PORT = 9001
const ROUTE_PREPARATION_COLLECTOR_PORT = 9002
const INSTRUMENTATION_STARTUP_COLLECTOR_PORT = 9003
const APP_ROUTE_MODULE_LOADING_COLLECTOR_PORT = 9004
const CLIENT_COMPONENT_GATE_PORT = 9005
type NextInstance = ReturnType<typeof nextTestSetup>['next'] type NextInstance = ReturnType<typeof nextTestSetup>['next']
function setupCollector(next: NextInstance, port: number) { function setupCollector(next: NextInstance) {
let collector: Collector | undefined let collector: Collector | undefined
// The app's span exporter remains active for the entire suite. Keep its // The app's span exporter remains active for the entire suite. Keep its
// endpoint available for the same lifetime and only reset collected state. // endpoint available for the same lifetime and only reset collected state.
beforeAll(async () => { beforeAll(async () => {
collector = await connectCollector({ port }) collector = await connectCollector()
next.env.TEST_OTEL_COLLECTOR_PORT = String(collector.port)
await next.start() await next.start()
}) })
...@@ -64,8 +59,6 @@ function setup({ ...@@ -64,8 +59,6 @@ function setup({
...(!useDirectEntrypointHandler ...(!useDirectEntrypointHandler
? { ? {
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(COLLECTOR_PORT),
TEST_CLIENT_COMPONENT_GATE_PORT: String(CLIENT_COMPONENT_GATE_PORT),
NEXT_OTEL_PERFORMANCE_PREFIX: 'client-component-loading-test', NEXT_OTEL_PERFORMANCE_PREFIX: 'client-component-loading-test',
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
}, },
...@@ -81,7 +74,6 @@ function setup({ ...@@ -81,7 +74,6 @@ function setup({
}, },
serverReadyPattern: /- Local:/, serverReadyPattern: /- Local:/,
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(COLLECTOR_PORT),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
NODE_ENV: 'production', NODE_ENV: 'production',
}, },
...@@ -94,13 +86,21 @@ function setup({ ...@@ -94,13 +86,21 @@ function setup({
} }
: undefined, : undefined,
}) })
if (!testSetup.skipped && !useDirectEntrypointHandler) {
beforeAll(async () => {
testSetup.next.env.TEST_CLIENT_COMPONENT_GATE_PORT = String(
await findPort()
)
})
}
return { return {
next: testSetup, next: testSetup,
getCollector: testSetup.skipped getCollector: testSetup.skipped
? () => { ? () => {
throw new Error('OpenTelemetry test setup was skipped') throw new Error('OpenTelemetry test setup was skipped')
} }
: setupCollector(testSetup.next, COLLECTOR_PORT), : setupCollector(testSetup.next),
} }
} }
...@@ -152,7 +152,7 @@ describe.each( ...@@ -152,7 +152,7 @@ describe.each(
}) })
it('closes collector connections after each export', async () => { it('closes collector connections after each export', async () => {
const response = await fetch(`http://localhost:${COLLECTOR_PORT}`, { const response = await fetch(`http://localhost:${getCollector().port}`, {
method: 'POST', method: 'POST',
body: '[]', body: '[]',
}) })
...@@ -181,7 +181,9 @@ describe.each( ...@@ -181,7 +181,9 @@ describe.each(
return `00-${traceId}-${'1'.repeat(16)}-01` return `00-${traceId}-${'1'.repeat(16)}-01`
} }
beforeAll(() => gateServer.listen(CLIENT_COMPONENT_GATE_PORT)) beforeAll(() => {
gateServer.listen(Number(next.env.TEST_CLIENT_COMPONENT_GATE_PORT))
})
beforeEach(() => { beforeEach(() => {
gateArrivals = createTestLog() gateArrivals = createTestLog()
}) })
...@@ -207,13 +209,14 @@ describe.each( ...@@ -207,13 +209,14 @@ describe.each(
traceId: string traceId: string
extra?: boolean extra?: boolean
}) { }) {
const responsePromise = next.fetch( // The gate timeout starts after response headers arrive so it excludes
// dev compilation.
const response = await next.fetch(
`/app/test/client-component-loading?id=${id}${extra ? '&variant=extra' : ''}`, `/app/test/client-component-loading?id=${id}${extra ? '&variant=extra' : ''}`,
{ headers: { traceparent: traceparent(traceId) } } { headers: { traceparent: traceparent(traceId) } }
) )
await gateArrivals.waitFor([id])
const response = await responsePromise
expect(response.status).toBe(200) expect(response.status).toBe(200)
await gateArrivals.waitFor([id])
return response return response
} }
...@@ -2071,7 +2074,6 @@ if (isNextStart) { ...@@ -2071,7 +2074,6 @@ if (isNextStart) {
}, },
serverReadyPattern: /- Local:/, serverReadyPattern: /- Local:/,
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(ROUTE_PREPARATION_COLLECTOR_PORT),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
NODE_ENV: 'production', NODE_ENV: 'production',
}, },
...@@ -2093,10 +2095,9 @@ if (isNextStart) { ...@@ -2093,10 +2095,9 @@ if (isNextStart) {
] ]
it('should trace route module preparation inside the request span', async () => { it('should trace route module preparation inside the request span', async () => {
const connectedCollector = await connectCollector({ const connectedCollector = await connectCollector()
port: ROUTE_PREPARATION_COLLECTOR_PORT,
})
collector = connectedCollector collector = connectedCollector
next.env.TEST_OTEL_COLLECTOR_PORT = String(connectedCollector.port)
await next.start() await next.start()
// The first request starts the process (cold: instrumentation is // The first request starts the process (cold: instrumentation is
...@@ -2264,9 +2265,6 @@ describe.each( ...@@ -2264,9 +2265,6 @@ describe.each(
...(!useDirectEntrypointHandler ...(!useDirectEntrypointHandler
? { ? {
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(
APP_ROUTE_MODULE_LOADING_COLLECTOR_PORT
),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
}, },
} }
...@@ -2280,9 +2278,6 @@ describe.each( ...@@ -2280,9 +2278,6 @@ describe.each(
}, },
serverReadyPattern: /- Local:/, serverReadyPattern: /- Local:/,
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(
APP_ROUTE_MODULE_LOADING_COLLECTOR_PORT
),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
NODE_ENV: 'production', NODE_ENV: 'production',
}, },
...@@ -2298,9 +2293,8 @@ describe.each( ...@@ -2298,9 +2293,8 @@ describe.each(
}) })
it('should trace cold App Route module loading once', async () => { it('should trace cold App Route module loading once', async () => {
collector = await connectCollector({ collector = await connectCollector()
port: APP_ROUTE_MODULE_LOADING_COLLECTOR_PORT, next.env.TEST_OTEL_COLLECTOR_PORT = String(collector.port)
})
await next.start() await next.start()
const pathname = '/api/app/param/data' const pathname = '/api/app/param/data'
...@@ -2737,9 +2731,6 @@ describe.each( ...@@ -2737,9 +2731,6 @@ describe.each(
...(!useDirectEntrypointHandler ...(!useDirectEntrypointHandler
? { ? {
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(
INSTRUMENTATION_STARTUP_COLLECTOR_PORT
),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
}, },
} }
...@@ -2753,9 +2744,6 @@ describe.each( ...@@ -2753,9 +2744,6 @@ describe.each(
}, },
serverReadyPattern: /- Local:/, serverReadyPattern: /- Local:/,
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(
INSTRUMENTATION_STARTUP_COLLECTOR_PORT
),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
NODE_ENV: 'production', NODE_ENV: 'production',
}, },
...@@ -2771,9 +2759,8 @@ describe.each( ...@@ -2771,9 +2759,8 @@ describe.each(
}) })
it('should trace instrumentation startup', async () => { it('should trace instrumentation startup', async () => {
collector = await connectCollector({ collector = await connectCollector()
port: INSTRUMENTATION_STARTUP_COLLECTOR_PORT, next.env.TEST_OTEL_COLLECTOR_PORT = String(collector.port)
})
await next.start() await next.start()
await next.fetch('/app/param/rsc-fetch') await next.fetch('/app/param/rsc-fetch')
...@@ -2850,7 +2837,6 @@ describe.each( ...@@ -2850,7 +2837,6 @@ describe.each(
skipStart: true, skipStart: true,
dependencies: require('./package.json').dependencies, dependencies: require('./package.json').dependencies,
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(COLLECTOR_PORT),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
NEXT_OTEL_VERBOSE: '1', NEXT_OTEL_VERBOSE: '1',
}, },
...@@ -2860,7 +2846,7 @@ describe.each( ...@@ -2860,7 +2846,7 @@ describe.each(
return return
} }
const getCollector = setupCollector(next, COLLECTOR_PORT) const getCollector = setupCollector(next)
// Regression for https://github.com/vercel/otel/issues/107. // Regression for https://github.com/vercel/otel/issues/107.
it('all spans (including verbose) inherit traceId from incoming traceparent header', async () => { it('all spans (including verbose) inherit traceId from incoming traceparent header', async () => {
...@@ -2910,7 +2896,6 @@ describe('opentelemetry with disabled fetch tracing', () => { ...@@ -2910,7 +2896,6 @@ describe('opentelemetry with disabled fetch tracing', () => {
dependencies: require('./package.json').dependencies, dependencies: require('./package.json').dependencies,
env: { env: {
NEXT_OTEL_FETCH_DISABLED: '1', NEXT_OTEL_FETCH_DISABLED: '1',
TEST_OTEL_COLLECTOR_PORT: String(COLLECTOR_PORT),
}, },
}) })
...@@ -2918,7 +2903,7 @@ describe('opentelemetry with disabled fetch tracing', () => { ...@@ -2918,7 +2903,7 @@ describe('opentelemetry with disabled fetch tracing', () => {
return return
} }
const getCollector = setupCollector(next, COLLECTOR_PORT) const getCollector = setupCollector(next)
;(process.env.__NEXT_CACHE_COMPONENTS ? describe.skip : describe)( ;(process.env.__NEXT_CACHE_COMPONENTS ? describe.skip : describe)(
'root context', 'root context',
() => { () => {
...@@ -2988,13 +2973,12 @@ describe('opentelemetry with custom server', () => { ...@@ -2988,13 +2973,12 @@ describe('opentelemetry with custom server', () => {
}, },
serverReadyPattern: /- Local:/, serverReadyPattern: /- Local:/,
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(COLLECTOR_PORT),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
NODE_ENV: isNextDev ? 'development' : 'production', NODE_ENV: isNextDev ? 'development' : 'production',
}, },
}) })
const getCollector = setupCollector(next, COLLECTOR_PORT) const getCollector = setupCollector(next)
it('should set attributes correctly on handleRequest span', async () => { it('should set attributes correctly on handleRequest span', async () => {
await next.fetch('/app/param/rsc-fetch') await next.fetch('/app/param/rsc-fetch')
...@@ -3148,7 +3132,6 @@ if (isNextStart) { ...@@ -3148,7 +3132,6 @@ if (isNextStart) {
}, },
serverReadyPattern: /- Local:/, serverReadyPattern: /- Local:/,
env: { env: {
TEST_OTEL_COLLECTOR_PORT: String(COLLECTOR_PORT),
NEXT_TELEMETRY_DISABLED: '1', NEXT_TELEMETRY_DISABLED: '1',
NODE_ENV: 'production', NODE_ENV: 'production',
}, },
...@@ -3158,7 +3141,7 @@ if (isNextStart) { ...@@ -3158,7 +3141,7 @@ if (isNextStart) {
return return
} }
const getCollector = setupCollector(next, COLLECTOR_PORT) const getCollector = setupCollector(next)
const directEntrypointCases = [ const directEntrypointCases = [
{ pathname: '/app/param/rsc-fetch', route: '/app/[param]/rsc-fetch' }, { pathname: '/app/param/rsc-fetch', route: '/app/[param]/rsc-fetch' },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment