From 82e03978b89938219958032efb1448cc76baa181 Mon Sep 17 00:00:00 2001 From: Saumit Date: Sat, 27 Sep 2025 02:14:26 +0530 Subject: Initial snapshot - OpenTelemetry demo 2.1.3 -f --- src/frontend/pages/_document.tsx | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/frontend/pages/_document.tsx (limited to 'src/frontend/pages/_document.tsx') diff --git a/src/frontend/pages/_document.tsx b/src/frontend/pages/_document.tsx new file mode 100644 index 0000000..8a68bc0 --- /dev/null +++ b/src/frontend/pages/_document.tsx @@ -0,0 +1,65 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +import Document, { DocumentContext, Html, Head, Main, NextScript } from 'next/document'; +import { ServerStyleSheet } from 'styled-components'; +import {context, propagation} from "@opentelemetry/api"; + +const { ENV_PLATFORM, WEB_OTEL_SERVICE_NAME, PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_COLLECTOR_HOST} = process.env; + +export default class MyDocument extends Document<{ envString: string }> { + static async getInitialProps(ctx: DocumentContext) { + const sheet = new ServerStyleSheet(); + const originalRenderPage = ctx.renderPage; + + try { + ctx.renderPage = () => + originalRenderPage({ + enhanceApp: App => props => sheet.collectStyles(), + }); + + const initialProps = await Document.getInitialProps(ctx); + const baggage = propagation.getBaggage(context.active()); + const isSyntheticRequest = baggage?.getEntry('synthetic_request')?.value === 'true'; + + const otlpTracesEndpoint = isSyntheticRequest + ? `http://${OTEL_COLLECTOR_HOST}:4318/v1/traces` + : PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT; + + const envString = ` + window.ENV = { + NEXT_PUBLIC_PLATFORM: '${ENV_PLATFORM}', + NEXT_PUBLIC_OTEL_SERVICE_NAME: '${WEB_OTEL_SERVICE_NAME}', + NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: '${otlpTracesEndpoint}', + IS_SYNTHETIC_REQUEST: '${isSyntheticRequest}', + };`; + return { + ...initialProps, + styles: [initialProps.styles, sheet.getStyleElement()], + envString, + }; + } finally { + sheet.seal(); + } + } + + render() { + return ( + + + + + + + +
+ + + + + ); + } +} -- cgit v1.2.3