// packages/core-shared/src/instrumentation/di/bind-otel-instrumentation.ts import type { Container } from "inversify"; import { OtelTracer } from "../otel/otel-tracer"; import { OtelLogger } from "../otel/otel-logger"; import { OtelMetrics } from "../otel/otel-metrics"; import { INSTRUMENTATION_SYMBOLS } from "../symbols"; import type { ITracer, ILogger, IMetrics } from "../index"; export type BindOtelOpts = { dsn: string; app: "web-next" | "cms" | "web-tanstack"; release?: string; }; /** * Binds OtelTracer, OtelLogger, and OtelMetrics to the DI container. * * NOTE: The OTel NodeSDK is NOT initialized here. It is initialized by each * app's instrumentation.ts `register()` hook (Next.js convention / server-entry * hook for TanStack) so that PII scrub processors are active before the very * first request handler runs — before bindAll() fires. Calling initOtelServerNode * here as well would create a second SDK init path and reintroduce the startup * window vulnerability (C1 fix). */ export function bindOtelInstrumentation( container: Container, opts: BindOtelOpts, ): { tracer: ITracer; logger: ILogger; metrics: IMetrics } { const tracer = new OtelTracer(); const logger = new OtelLogger(); const metrics = new OtelMetrics(); if (container.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) { container.unbind(INSTRUMENTATION_SYMBOLS.TRACER); } if (container.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) { container.unbind(INSTRUMENTATION_SYMBOLS.LOGGER); } if (container.isBound(INSTRUMENTATION_SYMBOLS.METRICS)) { container.unbind(INSTRUMENTATION_SYMBOLS.METRICS); } container.bind(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer); container.bind(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger); container.bind(INSTRUMENTATION_SYMBOLS.METRICS).toConstantValue(metrics); return { tracer, logger, metrics }; }