3
3
4
4
import Document , { DocumentContext , Html , Head , Main , NextScript } from 'next/document' ;
5
5
import { ServerStyleSheet } from 'styled-components' ;
6
+ import { context , propagation } from "@opentelemetry/api" ;
6
7
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 ;
16
9
17
10
export default class MyDocument extends Document < { envString : string } > {
18
11
static async getInitialProps ( ctx : DocumentContext ) {
@@ -26,6 +19,19 @@ export default class MyDocument extends Document<{ envString: string }> {
26
19
} ) ;
27
20
28
21
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
+ };` ;
29
35
return {
30
36
...initialProps ,
31
37
styles : [ initialProps . styles , sheet . getStyleElement ( ) ] ,
0 commit comments