Files
agentic-dev/packages/core-shared/src/instrumentation/di/bind-otel-instrumentation.ts
Danijel Martinek 4ea9a5c38e fix(otel): consolidate to single OTel SDK init at instrumentation.register hook
All three apps' instrumentation.ts files now call initOtelServerNode directly
instead of initSentryServer/initSentryServerNode, closing the startup window
where @sentry/nextjs auto-instrumentation could send unscrubbed errors before
bindAll() fires. bindOtelInstrumentation no longer calls initOtelServerNode
(SDK init belongs at app boot, binding at request scope). Orphaned sentry/
init-server*.ts files deleted; their package.json subpath exports removed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 12:38:17 +02:00

47 lines
1.9 KiB
TypeScript

// 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<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
container.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
container.bind<IMetrics>(INSTRUMENTATION_SYMBOLS.METRICS).toConstantValue(metrics);
return { tracer, logger, metrics };
}