| | | 1 | | import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node"; |
| | | 2 | | import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http"; |
| | | 3 | | import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http"; |
| | | 4 | | import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; |
| | | 5 | | import { NodeSDK } from "@opentelemetry/sdk-node"; |
| | | 6 | | import { BatchLogRecordProcessor } from "@opentelemetry/sdk-logs"; |
| | | 7 | | import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"; |
| | | 8 | | |
| | 0 | 9 | | let started = false; |
| | | 10 | | |
| | | 11 | | /** |
| | | 12 | | * Node SDK reads standard OTEL_* env vars at process runtime (not Vite `import.meta`). |
| | | 13 | | * Set e.g. OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_TRACES_ENDPOINT in K8s/Docker. |
| | | 14 | | * Browser traces use URLs injected from the root loader (see root.tsx); those may differ |
| | | 15 | | * from the in-cluster collector URL the Node exporter uses. |
| | | 16 | | */ |
| | 0 | 17 | | export function startNodeOpenTelemetry(): void { |
| | 0 | 18 | | if (started) return; |
| | 0 | 19 | | if (process.env.OTEL_SDK_DISABLED === "true") return; |
| | | 20 | | |
| | 0 | 21 | | started = true; |
| | | 22 | | |
| | 0 | 23 | | const sdk = new NodeSDK({ |
| | | 24 | | serviceName: |
| | | 25 | | process.env.OTEL_SERVICE_NAME ?? "clutterstock-frontend-ssr", |
| | | 26 | | traceExporter: new OTLPTraceExporter(), |
| | | 27 | | metricReaders: [ |
| | | 28 | | new PeriodicExportingMetricReader({ |
| | | 29 | | exporter: new OTLPMetricExporter(), |
| | | 30 | | }), |
| | | 31 | | ], |
| | | 32 | | logRecordProcessors: [ |
| | | 33 | | new BatchLogRecordProcessor(new OTLPLogExporter()), |
| | | 34 | | ], |
| | | 35 | | instrumentations: [ |
| | | 36 | | getNodeAutoInstrumentations({ |
| | | 37 | | "@opentelemetry/instrumentation-fs": { enabled: false }, |
| | | 38 | | }), |
| | | 39 | | ], |
| | | 40 | | }); |
| | | 41 | | |
| | 0 | 42 | | sdk.start(); |
| | | 43 | | } |