From e430d57d33f0fff5a9ef9681032f34a3ea7c9b63 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 14 May 2024 17:17:47 +0200 Subject: [PATCH] use array to filer the injected metadata --- packages/next/src/export/index.ts | 2 +- .../next/src/server/app-render/app-render.tsx | 18 ++++--- .../make-get-server-inserted-html.tsx | 31 ++++++++---- packages/next/src/server/app-render/types.ts | 2 +- packages/next/src/server/base-server.ts | 3 +- packages/next/src/server/config-schema.ts | 2 +- packages/next/src/server/config-shared.ts | 6 +-- .../client-trace-metadata.test.ts | 47 ++++++++----------- .../client-trace-metadata/next.config.js | 10 ++++ 9 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 test/e2e/opentelemetry/client-trace-metadata/next.config.js diff --git a/packages/next/src/export/index.ts b/packages/next/src/export/index.ts index b667808c6c8ec..b52db8cd6d3dd 100644 --- a/packages/next/src/export/index.ts +++ b/packages/next/src/export/index.ts @@ -420,8 +420,8 @@ export async function exportAppImpl( deploymentId: nextConfig.deploymentId, experimental: { isAppPPREnabled: checkIsAppPPREnabled(nextConfig.experimental.ppr), + clientTraceMetadata: nextConfig.experimental.clientTraceMetadata, swrDelta: nextConfig.experimental.swrDelta, - clientTraceMetadata: nextConfig.experimental.clientTraceMetadata === true, }, } diff --git a/packages/next/src/server/app-render/app-render.tsx b/packages/next/src/server/app-render/app-render.tsx index b5a0a1478edf8..23facb23c4902 100644 --- a/packages/next/src/server/app-render/app-render.tsx +++ b/packages/next/src/server/app-render/app-render.tsx @@ -76,7 +76,10 @@ import { appendMutableCookies } from '../web/spec-extension/adapters/request-coo import { createServerInsertedHTML } from './server-inserted-html' import { getRequiredScripts } from './required-scripts' import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix' -import { makeGetServerInsertedHTML } from './make-get-server-inserted-html' +import { + getTracedMetadata, + makeGetServerInsertedHTML, +} from './make-get-server-inserted-html' import { walkTreeWithFlightRouterState } from './walk-tree-with-flight-router-state' import { createComponentTree } from './create-component-tree' import { getAssetQueryString } from './get-asset-query-string' @@ -912,6 +915,11 @@ async function renderToHTMLOrFlightImpl( tree, formState, }: RenderToStreamOptions): Promise => { + const tracingMetadata = getTracedMetadata( + getTracer().getTracePropagationData(), + renderOpts.experimental.clientTraceMetadata + ) + const polyfills: JSX.IntrinsicElements['script'][] = buildManifest.polyfillFiles .filter( @@ -995,9 +1003,7 @@ async function renderToHTMLOrFlightImpl( renderServerInsertedHTML, serverCapturedErrors: allCapturedErrors, basePath: renderOpts.basePath, - traceData: renderOpts.experimental.clientTraceMetadata - ? getTracer().getTracePropagationData() - : [], + tracingMetadata: tracingMetadata, }) const renderer = createStaticRenderer({ @@ -1322,9 +1328,7 @@ async function renderToHTMLOrFlightImpl( renderServerInsertedHTML, serverCapturedErrors: [], basePath: renderOpts.basePath, - traceData: renderOpts.experimental.clientTraceMetadata - ? getTracer().getTracePropagationData() - : [], + tracingMetadata: tracingMetadata, }), serverInsertedHTMLToHead: true, validateRootLayout, diff --git a/packages/next/src/server/app-render/make-get-server-inserted-html.tsx b/packages/next/src/server/app-render/make-get-server-inserted-html.tsx index 8571cf949b3c9..624b1df8d7ac7 100644 --- a/packages/next/src/server/app-render/make-get-server-inserted-html.tsx +++ b/packages/next/src/server/app-render/make-get-server-inserted-html.tsx @@ -11,16 +11,24 @@ import { RedirectStatusCode } from '../../client/components/redirect-status-code import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix' import type { ClientTraceDataEntry } from '../lib/trace/tracer' +export function getTracedMetadata( + traceData: ClientTraceDataEntry[], + clientTraceMetadata: string[] | undefined +): ClientTraceDataEntry[] | undefined { + if (!clientTraceMetadata) return undefined + return traceData.filter(({ key }) => clientTraceMetadata.includes(key)) +} + export function makeGetServerInsertedHTML({ polyfills, renderServerInsertedHTML, serverCapturedErrors, - traceData, + tracingMetadata, basePath, }: { polyfills: JSX.IntrinsicElements['script'][] renderServerInsertedHTML: () => React.ReactNode - traceData: ClientTraceDataEntry[] + tracingMetadata: ClientTraceDataEntry[] | undefined serverCapturedErrors: Error[] basePath: string }) { @@ -84,14 +92,17 @@ export function makeGetServerInsertedHTML({ return