| | | 1 | | import { registerInstrumentations } from "@opentelemetry/instrumentation"; |
| | | 2 | | import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; |
| | | 3 | | import { resourceFromAttributes } from "@opentelemetry/resources"; |
| | | 4 | | import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; |
| | | 5 | | import { ZoneContextManager } from "@opentelemetry/context-zone"; |
| | | 6 | | import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"; |
| | | 7 | | import { WebTracerProvider } from "@opentelemetry/sdk-trace-web"; |
| | | 8 | | import { getWebAutoInstrumentations } from "@opentelemetry/auto-instrumentations-web"; |
| | | 9 | | |
| | | 10 | | import { readPublicRuntimeConfigFromWindow } from "~/public-runtime-config"; |
| | | 11 | | |
| | 0 | 12 | | export function startBrowserOpenTelemetry(): void { |
| | 0 | 13 | | const fromDocument = readPublicRuntimeConfigFromWindow(); |
| | | 14 | | const url = |
| | 0 | 15 | | fromDocument?.otelTracesEndpoint?.trim() || |
| | | 16 | | import.meta.env.VITE_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT; |
| | 0 | 17 | | if (!url) return; |
| | | 18 | | |
| | | 19 | | const serviceName = |
| | 0 | 20 | | fromDocument?.otelServiceName?.trim() || |
| | | 21 | | import.meta.env.VITE_OTEL_SERVICE_NAME || |
| | | 22 | | "clutterstock-frontend-web"; |
| | | 23 | | |
| | 0 | 24 | | const exporter = new OTLPTraceExporter({ url }); |
| | | 25 | | |
| | 0 | 26 | | const provider = new WebTracerProvider({ |
| | | 27 | | resource: resourceFromAttributes({ |
| | | 28 | | [ATTR_SERVICE_NAME]: serviceName, |
| | | 29 | | }), |
| | | 30 | | spanProcessors: [new BatchSpanProcessor(exporter)], |
| | | 31 | | }); |
| | | 32 | | |
| | 0 | 33 | | provider.register({ |
| | | 34 | | contextManager: new ZoneContextManager(), |
| | | 35 | | }); |
| | | 36 | | |
| | 0 | 37 | | registerInstrumentations({ |
| | | 38 | | instrumentations: [ |
| | | 39 | | getWebAutoInstrumentations({ |
| | | 40 | | "@opentelemetry/instrumentation-fetch": { |
| | | 41 | | propagateTraceHeaderCorsUrls: /.*/, |
| | | 42 | | ignoreUrls: [url, /\/v1\/traces(\?|$)/], |
| | | 43 | | clearTimingResources: true, |
| | | 44 | | }, |
| | | 45 | | }), |
| | | 46 | | ], |
| | | 47 | | }); |
| | | 48 | | } |