feat(navigation): wire instrumentation — header repo spans + getHeader withSpan

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-07 17:59:14 +02:00
parent 404cb07e79
commit e14a23dccd
7 changed files with 202 additions and 31 deletions

View File

@@ -1,11 +1,14 @@
import "reflect-metadata";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { NoopTracer, NoopLogger } from "@repo/core-shared/instrumentation";
import { bindDevSeedNavigation } from "@/di/bind-dev-seed";
import { navigationContainer } from "@/di/container";
import { NAVIGATION_SYMBOLS } from "@/di/symbols";
import { MockHeaderRepository } from "@/infrastructure/repositories/header.repository.mock";
import type { IHeaderRepository } from "@/application/repositories/header.repository.interface";
const noop = { tracer: new NoopTracer(), logger: new NoopLogger() };
describe("bindDevSeedNavigation", () => {
// Each test starts from the default mock binding and tears down afterwards
// so the global navigationContainer state stays clean for siblings.
@@ -28,7 +31,7 @@ describe("bindDevSeedNavigation", () => {
});
it("populates the header repository with the dev header", async () => {
await bindDevSeedNavigation();
await bindDevSeedNavigation(noop.tracer, noop.logger);
const repo = navigationContainer.get<IHeaderRepository>(
NAVIGATION_SYMBOLS.IHeaderRepository,
@@ -39,7 +42,7 @@ describe("bindDevSeedNavigation", () => {
});
it("seeds a header with a non-empty items array", async () => {
await bindDevSeedNavigation();
await bindDevSeedNavigation(noop.tracer, noop.logger);
const repo = navigationContainer.get<IHeaderRepository>(
NAVIGATION_SYMBOLS.IHeaderRepository,
@@ -53,13 +56,13 @@ describe("bindDevSeedNavigation", () => {
});
it("is idempotent — calling twice rebuilds a fresh populated repo", async () => {
await bindDevSeedNavigation();
await bindDevSeedNavigation(noop.tracer, noop.logger);
const before = navigationContainer.get<IHeaderRepository>(
NAVIGATION_SYMBOLS.IHeaderRepository,
);
const beforeHeader = await before.getHeader();
await bindDevSeedNavigation();
await bindDevSeedNavigation(noop.tracer, noop.logger);
const after = navigationContainer.get<IHeaderRepository>(
NAVIGATION_SYMBOLS.IHeaderRepository,
);

View File

@@ -1,7 +1,15 @@
import {
withSpan,
INSTRUMENTATION_SYMBOLS,
type ITracer,
type ILogger,
} from "@repo/core-shared/instrumentation";
import { navigationContainer } from "./container.js";
import { NAVIGATION_SYMBOLS } from "./symbols.js";
import { MockHeaderRepository } from "../infrastructure/repositories/header.repository.mock.js";
import { buildDevHeader } from "../__seeds__/dev.js";
import { getHeaderUseCase } from "../application/use-cases/get-header.use-case.js";
import { getHeaderController } from "../interface-adapters/controllers/get-header.controller.js";
import type { IHeaderRepository } from "../application/repositories/header.repository.interface.js";
/**
@@ -15,12 +23,49 @@ import type { IHeaderRepository } from "../application/repositories/header.repos
* Idempotent: safe to call multiple times; each call rebuilds a fresh
* populated repo and rebinds the symbol.
*/
export async function bindDevSeedNavigation(): Promise<void> {
export async function bindDevSeedNavigation(tracer: ITracer, logger: ILogger): Promise<void> {
// Bind shared instrumentation into feature container
if (navigationContainer.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
navigationContainer.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
}
if (navigationContainer.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
navigationContainer.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
}
navigationContainer.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
navigationContainer.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
if (navigationContainer.isBound(NAVIGATION_SYMBOLS.IHeaderRepository)) {
navigationContainer.unbind(NAVIGATION_SYMBOLS.IHeaderRepository);
}
const repo = new MockHeaderRepository(buildDevHeader(), tracer, logger);
navigationContainer
.bind<IHeaderRepository>(NAVIGATION_SYMBOLS.IHeaderRepository)
.toConstantValue(new MockHeaderRepository(buildDevHeader()));
.toConstantValue(repo);
// Wrap use case + controller identically to bind-production
const wrappedGetHeader = withSpan(
tracer,
{ name: "navigation.getHeader", op: "use-case" },
getHeaderUseCase(repo),
);
for (const sym of [
NAVIGATION_SYMBOLS.IGetHeaderUseCase,
NAVIGATION_SYMBOLS.IGetHeaderController,
]) {
if (navigationContainer.isBound(sym)) navigationContainer.unbind(sym);
}
navigationContainer
.bind(NAVIGATION_SYMBOLS.IGetHeaderUseCase)
.toConstantValue(wrappedGetHeader);
navigationContainer
.bind(NAVIGATION_SYMBOLS.IGetHeaderController)
.toConstantValue(
withSpan(
tracer,
{ name: "navigation.getHeader", op: "controller" },
getHeaderController(wrappedGetHeader),
),
);
}

View File

@@ -1,13 +1,65 @@
import type { SanitizedConfig } from "payload";
import {
withSpan,
INSTRUMENTATION_SYMBOLS,
type ITracer,
type ILogger,
} from "@repo/core-shared/instrumentation";
import { navigationContainer } from "./container";
import { NAVIGATION_SYMBOLS } from "./symbols";
import { HeaderRepository } from "../infrastructure/repositories/header.repository";
import { getHeaderUseCase } from "../application/use-cases/get-header.use-case";
import { getHeaderController } from "../interface-adapters/controllers/get-header.controller";
export function bindProductionNavigation(config: SanitizedConfig): void {
export function bindProductionNavigation(
config: SanitizedConfig,
tracer: ITracer,
logger: ILogger,
): void {
// Bind shared instrumentation into feature container
if (navigationContainer.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
navigationContainer.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
}
if (navigationContainer.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
navigationContainer.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
}
navigationContainer.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
navigationContainer.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
// Real repository
if (navigationContainer.isBound(NAVIGATION_SYMBOLS.IHeaderRepository)) {
navigationContainer.unbind(NAVIGATION_SYMBOLS.IHeaderRepository);
}
const repo = new HeaderRepository(config, tracer, logger);
navigationContainer
.bind(NAVIGATION_SYMBOLS.IHeaderRepository)
.toConstantValue(new HeaderRepository(config));
.toConstantValue(repo);
// Use case — wrapped with span at bind time
const wrappedGetHeader = withSpan(
tracer,
{ name: "navigation.getHeader", op: "use-case" },
getHeaderUseCase(repo),
);
if (navigationContainer.isBound(NAVIGATION_SYMBOLS.IGetHeaderUseCase)) {
navigationContainer.unbind(NAVIGATION_SYMBOLS.IGetHeaderUseCase);
}
navigationContainer
.bind(NAVIGATION_SYMBOLS.IGetHeaderUseCase)
.toConstantValue(wrappedGetHeader);
// Controller — wrapped with span at bind time
if (navigationContainer.isBound(NAVIGATION_SYMBOLS.IGetHeaderController)) {
navigationContainer.unbind(NAVIGATION_SYMBOLS.IGetHeaderController);
}
navigationContainer
.bind(NAVIGATION_SYMBOLS.IGetHeaderController)
.toConstantValue(
withSpan(
tracer,
{ name: "navigation.getHeader", op: "controller" },
getHeaderController(wrappedGetHeader),
),
);
}