From 47072bf4c52c7cda63b37ef051550da9c76f7449 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 10 Sep 2024 17:32:01 +0200 Subject: [PATCH] Pass client trace metadata to client in pages router (#69620) --- packages/next/src/pages/_document.tsx | 14 + .../next/src/server/app-render/app-render.tsx | 6 +- .../make-get-server-inserted-html.tsx | 8 - packages/next/src/server/lib/trace/utils.ts | 13 + packages/next/src/server/render.tsx | 5 + .../shared/lib/html-context.shared-runtime.ts | 1 + .../{ => app-router}/dynamic-page/page.tsx | 0 .../{ => app-router}/static-page-2/page.tsx | 0 .../app/{ => app-router}/static-page/page.tsx | 4 +- .../app/{ => app-router}/suspense/client.tsx | 0 .../app/{ => app-router}/suspense/page.tsx | 0 .../client-trace-metadata.test.ts | 440 ++++++++++++------ .../client-trace-metadata/instrumentation.ts | 2 +- .../pages/pages-router/dynamic-page.tsx | 7 + .../pages/pages-router/static-page-2.tsx | 3 + .../pages/pages-router/static-page.tsx | 15 + 16 files changed, 361 insertions(+), 157 deletions(-) create mode 100644 packages/next/src/server/lib/trace/utils.ts rename test/e2e/opentelemetry/client-trace-metadata/app/{ => app-router}/dynamic-page/page.tsx (100%) rename test/e2e/opentelemetry/client-trace-metadata/app/{ => app-router}/static-page-2/page.tsx (100%) rename test/e2e/opentelemetry/client-trace-metadata/app/{ => app-router}/static-page/page.tsx (62%) rename test/e2e/opentelemetry/client-trace-metadata/app/{ => app-router}/suspense/client.tsx (100%) rename test/e2e/opentelemetry/client-trace-metadata/app/{ => app-router}/suspense/page.tsx (100%) create mode 100644 test/e2e/opentelemetry/client-trace-metadata/pages/pages-router/dynamic-page.tsx create mode 100644 test/e2e/opentelemetry/client-trace-metadata/pages/pages-router/static-page-2.tsx create mode 100644 test/e2e/opentelemetry/client-trace-metadata/pages/pages-router/static-page.tsx diff --git a/packages/next/src/pages/_document.tsx b/packages/next/src/pages/_document.tsx index 97ae7b62b783a..531903b03e10f 100644 --- a/packages/next/src/pages/_document.tsx +++ b/packages/next/src/pages/_document.tsx @@ -22,6 +22,8 @@ import { import type { HtmlProps } from '../shared/lib/html-context.shared-runtime' import { encodeURIPath } from '../shared/lib/encode-uri-path' import type { DeepReadonly } from '../shared/lib/deep-readonly' +import { getTracer } from '../server/lib/trace/tracer' +import { getTracedMetadata } from '../server/lib/trace/utils' export type { DocumentContext, DocumentInitialProps, DocumentProps } @@ -758,6 +760,17 @@ export class Head extends React.Component { assetPrefix ) + const tracingMetadata = getTracedMetadata( + getTracer().getTracePropagationData(), + this.context.experimentalClientTraceMetadata + ) + + const traceMetaTags = (tracingMetadata || []).map( + ({ key, value }, index) => ( + + ) + ) + return ( {this.context.isDevelopment && ( @@ -882,6 +895,7 @@ export class Head extends React.Component { // (by default, style-loader injects at the bottom of )