UNPKG

@ogcio/o11y-sdk-react

Version:

Opentelemetry standard instrumentation SDK for React based project

84 lines 3.63 kB
import { ErrorsInstrumentation, getWebInstrumentations, initializeFaro, WebVitalsInstrumentation, } from "@grafana/faro-react"; import { TracingInstrumentation } from "@grafana/faro-web-tracing"; import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api"; import { W3CTraceContextPropagator } from "@opentelemetry/core"; import { DocumentLoadInstrumentation } from "@opentelemetry/instrumentation-document-load"; import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch"; import { UserInteractionInstrumentation } from "@opentelemetry/instrumentation-user-interaction"; import { _beforeSend } from "./internals/hooks.js"; import { redactors, } from "./internals/redaction/redactors/index.js"; import { BasicRedactor } from "../../sdk-core/lib/redaction/basic-redactor.js"; export default function buildFaroInstrumentation(config) { if (!config) { console.warn("observability config not set. Skipping Faro OpenTelemetry instrumentation."); return; } if (!config.collectorUrl) { console.warn("collectorUrl not set. Skipping Faro OpenTelemetry instrumentation."); return; } if (!isUrl(config.collectorUrl)) { console.error("collectorUrl does not use a valid format. Skipping Faro OpenTelemetry instrumentation."); return; } const defaultRedactors = Object.entries(redactors) .filter(([key]) => { return config.detection?.[key] !== false; }) .map(([_, value]) => value); const customRedactors = config.detection?.custom?.length ? (config.detection?.custom.filter((redactor) => redactor instanceof BasicRedactor) ?? []) : []; const redactorsChain = [...defaultRedactors, ...customRedactors]; try { diag.setLogger(new DiagConsoleLogger(), config.diagLogLevel ? DiagLogLevel[config.diagLogLevel] : DiagLogLevel.INFO); const faro = initializeFaro({ url: config.collectorUrl, app: { name: config.serviceName, ...(config.appMeta ?? {}), }, batching: { enabled: !(config.collectorMode === "single"), }, beforeSend: _beforeSend(redactorsChain), instrumentations: [ ...getWebInstrumentations({ captureConsole: true, }), new ErrorsInstrumentation(), new WebVitalsInstrumentation(), new TracingInstrumentation({ propagator: new W3CTraceContextPropagator(), instrumentations: [ new DocumentLoadInstrumentation(), new FetchInstrumentation({ ignoreUrls: [config.collectorUrl], // ignore collector to avoid fetch loop propagateTraceHeaderCorsUrls: config.corsTraceHeaders ? config.corsTraceHeaders.split(",").map((s) => new RegExp(s)) : undefined, }), new UserInteractionInstrumentation(), ], }), ], }); console.log("Faro OpenTelemetry instrumentation started successfully."); return faro; } catch (error) { console.error("Error starting Faro OpenTelemetry instrumentation:", error); } } function isUrl(url) { try { new URL(url); return true; } catch (_) { return false; } } //# sourceMappingURL=instrumentation.faro.js.map