refactor(core-shared): rename bindSentryInstrumentation → bindOtelInstrumentation

git mv bind-sentry-instrumentation → bind-otel-instrumentation; function
renamed; new impl calls initOtelServerNode and binds OtelTracer (SentryLogger
kept for Phase 3). Barrel re-exports both new name and deprecated alias
bindSentryInstrumentation for one release. ESLint allowlist updated to the
new filename. init-server-node opts gains release? field.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-11 11:42:07 +02:00
parent b61e9d2091
commit 53bfe727b4
7 changed files with 111 additions and 87 deletions

View File

@@ -0,0 +1,55 @@
// packages/core-shared/src/instrumentation/di/bind-otel-instrumentation.test.ts
import "reflect-metadata";
import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("@sentry/nextjs", () => ({
init: vi.fn(),
captureException: vi.fn(),
captureMessage: vi.fn(),
addBreadcrumb: vi.fn(),
setUser: vi.fn(),
}));
vi.mock("@/instrumentation/otel/init-server-node", () => ({
initOtelServerNode: vi.fn(),
}));
import { Container } from "inversify";
import { bindOtelInstrumentation } from "@/instrumentation/di/bind-otel-instrumentation";
import { initOtelServerNode } from "@/instrumentation/otel/init-server-node";
import { INSTRUMENTATION_SYMBOLS } from "@/instrumentation/symbols";
import { OtelTracer } from "@/instrumentation/otel/otel-tracer";
import { SentryLogger } from "@/instrumentation/sentry/sentry-logger";
describe("bindOtelInstrumentation", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("calls initOtelServerNode with the provided DSN and app as serviceName", () => {
const c = new Container();
bindOtelInstrumentation(c, { dsn: "https://x@y/1", app: "web-next" });
expect(initOtelServerNode).toHaveBeenCalledTimes(1);
expect((initOtelServerNode as ReturnType<typeof vi.fn>).mock.calls[0]![0]).toMatchObject({
dsn: "https://x@y/1",
serviceName: "web-next",
});
});
it("binds OtelTracer + SentryLogger to the container", () => {
const c = new Container();
bindOtelInstrumentation(c, { dsn: "https://x@y/1", app: "web-next" });
expect(c.get(INSTRUMENTATION_SYMBOLS.TRACER)).toBeInstanceOf(OtelTracer);
expect(c.get(INSTRUMENTATION_SYMBOLS.LOGGER)).toBeInstanceOf(SentryLogger);
});
it("returns the tracer + logger instances", () => {
const c = new Container();
const { tracer, logger } = bindOtelInstrumentation(c, {
dsn: "https://x@y/1",
app: "web-next",
});
expect(tracer).toBeInstanceOf(OtelTracer);
expect(logger).toBeInstanceOf(SentryLogger);
});
});

View File

@@ -0,0 +1,45 @@
// packages/core-shared/src/instrumentation/di/bind-otel-instrumentation.ts
import type { Container } from "inversify";
import { OtelTracer } from "../otel/otel-tracer";
import { SentryLogger } from "../sentry/sentry-logger";
import { initOtelServerNode } from "../otel/init-server-node";
import { INSTRUMENTATION_SYMBOLS } from "../symbols";
import type { ITracer, ILogger } from "../index";
export type BindOtelOpts = {
dsn: string;
app: "web-next" | "cms" | "web-tanstack";
release?: string;
};
export function bindOtelInstrumentation(
container: Container,
opts: BindOtelOpts,
): { tracer: ITracer; logger: ILogger } {
const environment =
process.env["SENTRY_ENVIRONMENT"] ??
process.env["VERCEL_ENV"] ??
process.env["NODE_ENV"] ??
"development";
const release = opts.release ?? process.env["VERCEL_GIT_COMMIT_SHA"] ?? "unknown";
initOtelServerNode({
dsn: opts.dsn,
serviceName: opts.app,
environment,
release,
});
const tracer = new OtelTracer();
const logger = new SentryLogger(); // Phase 3 replaces with OtelLogger
if (container.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
container.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
}
if (container.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
container.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
}
container.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
container.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
return { tracer, logger };
}

View File

@@ -1,53 +0,0 @@
// packages/core-shared/src/instrumentation/di/bind-sentry-instrumentation.test.ts
import "reflect-metadata";
import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("@sentry/nextjs", () => ({
init: vi.fn(),
startSpan: vi.fn((_opts: unknown, fn: (span: unknown) => unknown) =>
fn({ setAttribute: vi.fn(), setStatus: vi.fn() }),
),
captureException: vi.fn(),
captureMessage: vi.fn(),
addBreadcrumb: vi.fn(),
setUser: vi.fn(),
replayIntegration: vi.fn(() => ({ name: "Replay" })),
}));
import * as Sentry from "@sentry/nextjs";
import { Container } from "inversify";
import { bindSentryInstrumentation } from "@/instrumentation/di/bind-sentry-instrumentation";
import { INSTRUMENTATION_SYMBOLS } from "@/instrumentation/symbols";
import { SentryTracer } from "@/instrumentation/sentry/sentry-tracer";
import { SentryLogger } from "@/instrumentation/sentry/sentry-logger";
describe("bindSentryInstrumentation", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("calls Sentry.init via initSentryServer", () => {
const c = new Container();
bindSentryInstrumentation(c, { dsn: "https://x@y/1", app: "web-next" });
expect(Sentry.init).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((Sentry.init as any).mock.calls[0][0].dsn).toBe("https://x@y/1");
});
it("binds SentryTracer + SentryLogger to the container", () => {
const c = new Container();
bindSentryInstrumentation(c, { dsn: "https://x@y/1", app: "web-next" });
expect(c.get(INSTRUMENTATION_SYMBOLS.TRACER)).toBeInstanceOf(SentryTracer);
expect(c.get(INSTRUMENTATION_SYMBOLS.LOGGER)).toBeInstanceOf(SentryLogger);
});
it("returns the tracer + logger instances", () => {
const c = new Container();
const { tracer, logger } = bindSentryInstrumentation(c, {
dsn: "https://x@y/1",
app: "web-next",
});
expect(tracer).toBeInstanceOf(SentryTracer);
expect(logger).toBeInstanceOf(SentryLogger);
});
});

View File

@@ -1,29 +0,0 @@
// packages/core-shared/src/instrumentation/di/bind-sentry-instrumentation.ts
import type { Container } from "inversify";
import { SentryTracer } from "../sentry/sentry-tracer";
import { SentryLogger } from "../sentry/sentry-logger";
import { initSentryServer, type InitServerOpts } from "../sentry/init-server";
import { INSTRUMENTATION_SYMBOLS } from "../symbols";
import type { ITracer, ILogger } from "../index";
export type BindSentryOpts = InitServerOpts;
export function bindSentryInstrumentation(
container: Container,
opts: BindSentryOpts,
): { tracer: ITracer; logger: ILogger } {
initSentryServer(opts);
const tracer = new SentryTracer();
const logger = new SentryLogger();
if (container.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
container.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
}
if (container.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
container.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
}
container.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
container.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
return { tracer, logger };
}

View File

@@ -17,8 +17,13 @@ export { isReported, markReported } from "./reported-flag";
export { INSTRUMENTATION_SYMBOLS } from "./symbols";
export { bindNoopInstrumentation } from "./di/bind-noop-instrumentation";
export {
bindSentryInstrumentation,
type BindSentryOpts,
} from "./di/bind-sentry-instrumentation";
bindOtelInstrumentation,
type BindOtelOpts,
} from "./di/bind-otel-instrumentation";
// Deprecated alias for one release cycle — callers should migrate to bindOtelInstrumentation.
export { bindOtelInstrumentation as bindSentryInstrumentation } from "./di/bind-otel-instrumentation";
export type { BindOtelOpts as BindSentryOpts } from "./di/bind-otel-instrumentation";
export { initSentryServerNode } from "./sentry/init-server-node";
export { initSentryClientReact } from "./sentry/init-client-react";

View File

@@ -10,6 +10,7 @@ export type InitOtelServerNodeOpts = {
serviceName: string;
serviceVersion?: string;
environment: string;
release?: string;
namespace?: string;
};