Skip to content

Commit 50b06b7

Browse files
committed
Use different otel endpoint when request contains the synthetic_request=true header
1 parent 7da7c1b commit 50b06b7

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/frontend/pages/_document.tsx

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33

44
import Document, { DocumentContext, Html, Head, Main, NextScript } from 'next/document';
55
import { ServerStyleSheet } from 'styled-components';
6+
import {context, propagation} from "@opentelemetry/api";
67

7-
const { ENV_PLATFORM, WEB_OTEL_SERVICE_NAME, PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT } = process.env;
8-
9-
const envString = `
10-
window.ENV = {
11-
NEXT_PUBLIC_PLATFORM: '${ENV_PLATFORM}',
12-
NEXT_PUBLIC_OTEL_SERVICE_NAME: '${WEB_OTEL_SERVICE_NAME}',
13-
NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: '${PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT}',
14-
};
15-
`;
8+
const { ENV_PLATFORM, WEB_OTEL_SERVICE_NAME, PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_COLLECTOR_NAME} = process.env;
169

1710
export default class MyDocument extends Document<{ envString: string }> {
1811
static async getInitialProps(ctx: DocumentContext) {
@@ -26,6 +19,19 @@ export default class MyDocument extends Document<{ envString: string }> {
2619
});
2720

2821
const initialProps = await Document.getInitialProps(ctx);
22+
const baggage = propagation.getBaggage(context.active());
23+
const isSyntheticRequest = baggage?.getEntry('synthetic_request')?.value === 'true';
24+
25+
const otlpTracesEndpoint = isSyntheticRequest
26+
? `http://${OTEL_COLLECTOR_NAME}:4318/v1/traces`
27+
: PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT;
28+
29+
const envString = `
30+
window.ENV = {
31+
NEXT_PUBLIC_PLATFORM: '${ENV_PLATFORM}',
32+
NEXT_PUBLIC_OTEL_SERVICE_NAME: '${WEB_OTEL_SERVICE_NAME}',
33+
NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: '${otlpTracesEndpoint}',
34+
};`;
2935
return {
3036
...initialProps,
3137
styles: [initialProps.styles, sheet.getStyleElement()],

0 commit comments

Comments
 (0)