Files
agentic-dev/packages/blog/src/di/bind-production.ts
Danijel Martinek f0775d6ecc feat(instrumentation): close R44 gap — throw-site capture for use cases + controllers
Plan 10 documented R44 (capture at originating-throw layer) but only the
R43 repo leg was wired. captureException had zero call sites in any
controller or use-case body. This commit closes the gap.

Mechanism:
- Extract __sentryReported flag helpers into core-shared/instrumentation/
  reported-flag.ts. SentryLogger switches to importing them; RecordingLogger
  carries an inlined copy (tooling → core boundary disallows the import).
- Add withCapture(logger, tags, fn) higher-order wrapper paralleling
  withSpan. On throw: capture-with-tags, mark, re-throw. Bail if the flag
  was already set — covers the bubbled-from-repo case so each error
  surfaces in the logger exactly once with the inner-most layer's tags.
- Apply withSpan(withCapture(factory)) in every feature's bind-production
  and bind-dev-seed: auth (3 use cases × 3 controllers), blog (3×3),
  marketing-pages (2×2), navigation (1×1), media (3×3). Span is outermost
  so the errored span timing reflects the capture-and-rethrow.
- RecordingLogger.captureException now also honours the flag — test
  capture counts stay honest when both repo and outer layer wrap.

Tests:
- packages/core-shared/src/instrumentation/with-capture.test.ts —
  4 cases covering success, capture-on-throw, mark-on-capture, no-double
  via the flag.
- packages/blog/tests/r44-no-double-capture.test.ts — 3 cases: repo throw
  → 1 capture with repo tags; controller parse fail → 1 capture with
  controller tags; success → 0 captures.

Verification: pnpm test 26/26, pnpm lint 15/15, pnpm typecheck 14/14.

Docs: ADR-014 and the refactor log gain a "Post-merge follow-up" section
recording the gap, the fix, and the underlying lesson (don't describe
intent as shipped state — grep first).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 00:28:22 +02:00

135 lines
4.9 KiB
TypeScript

import type { SanitizedConfig } from "payload";
import {
withSpan,
withCapture,
INSTRUMENTATION_SYMBOLS,
type ITracer,
type ILogger,
} from "@repo/core-shared/instrumentation";
import { blogContainer } from "./container";
import { BLOG_SYMBOLS } from "./symbols";
import { ArticlesRepository } from "../infrastructure/repositories/articles.repository";
import { getArticlesUseCase } from "../application/use-cases/get-articles.use-case";
import { getArticleBySlugUseCase } from "../application/use-cases/get-article-by-slug.use-case";
import { createArticleUseCase } from "../application/use-cases/create-article.use-case";
import { getArticlesController } from "../interface-adapters/controllers/get-articles.controller";
import { getArticleBySlugController } from "../interface-adapters/controllers/get-article-by-slug.controller";
import { createArticleController } from "../interface-adapters/controllers/create-article.controller";
export function bindProductionBlog(
config: SanitizedConfig,
tracer: ITracer,
logger: ILogger,
): void {
// Bind shared instrumentation into feature container
if (blogContainer.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
blogContainer.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
}
if (blogContainer.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
blogContainer.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
}
blogContainer.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
blogContainer.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
// Real repository
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
}
const repo = new ArticlesRepository(config, tracer, logger);
blogContainer.bind(BLOG_SYMBOLS.IArticlesRepository).toConstantValue(repo);
// Use cases — wrapped with span + capture at bind time (R41, R44)
const wrappedGetArticles = withSpan(
tracer,
{ name: "blog.getArticles", op: "use-case" },
withCapture(
logger,
{ feature: "blog", layer: "use-case", name: "blog.getArticles" },
getArticlesUseCase(repo),
),
);
const wrappedGetArticleBySlug = withSpan(
tracer,
{ name: "blog.getArticleBySlug", op: "use-case" },
withCapture(
logger,
{ feature: "blog", layer: "use-case", name: "blog.getArticleBySlug" },
getArticleBySlugUseCase(repo),
),
);
const wrappedCreateArticle = withSpan(
tracer,
{ name: "blog.createArticle", op: "use-case" },
withCapture(
logger,
{ feature: "blog", layer: "use-case", name: "blog.createArticle" },
createArticleUseCase(repo),
),
);
if (blogContainer.isBound(BLOG_SYMBOLS.IGetArticlesUseCase)) {
blogContainer.unbind(BLOG_SYMBOLS.IGetArticlesUseCase);
}
if (blogContainer.isBound(BLOG_SYMBOLS.IGetArticleBySlugUseCase)) {
blogContainer.unbind(BLOG_SYMBOLS.IGetArticleBySlugUseCase);
}
if (blogContainer.isBound(BLOG_SYMBOLS.ICreateArticleUseCase)) {
blogContainer.unbind(BLOG_SYMBOLS.ICreateArticleUseCase);
}
blogContainer.bind(BLOG_SYMBOLS.IGetArticlesUseCase).toConstantValue(wrappedGetArticles);
blogContainer
.bind(BLOG_SYMBOLS.IGetArticleBySlugUseCase)
.toConstantValue(wrappedGetArticleBySlug);
blogContainer.bind(BLOG_SYMBOLS.ICreateArticleUseCase).toConstantValue(wrappedCreateArticle);
// Controllers — wrapped with span at bind time
if (blogContainer.isBound(BLOG_SYMBOLS.IGetArticlesController)) {
blogContainer.unbind(BLOG_SYMBOLS.IGetArticlesController);
}
if (blogContainer.isBound(BLOG_SYMBOLS.IGetArticleBySlugController)) {
blogContainer.unbind(BLOG_SYMBOLS.IGetArticleBySlugController);
}
if (blogContainer.isBound(BLOG_SYMBOLS.ICreateArticleController)) {
blogContainer.unbind(BLOG_SYMBOLS.ICreateArticleController);
}
blogContainer
.bind(BLOG_SYMBOLS.IGetArticlesController)
.toConstantValue(
withSpan(
tracer,
{ name: "blog.getArticles", op: "controller" },
withCapture(
logger,
{ feature: "blog", layer: "controller", name: "blog.getArticles" },
getArticlesController(wrappedGetArticles),
),
),
);
blogContainer
.bind(BLOG_SYMBOLS.IGetArticleBySlugController)
.toConstantValue(
withSpan(
tracer,
{ name: "blog.getArticleBySlug", op: "controller" },
withCapture(
logger,
{ feature: "blog", layer: "controller", name: "blog.getArticleBySlug" },
getArticleBySlugController(wrappedGetArticleBySlug),
),
),
);
blogContainer
.bind(BLOG_SYMBOLS.ICreateArticleController)
.toConstantValue(
withSpan(
tracer,
{ name: "blog.createArticle", op: "controller" },
withCapture(
logger,
{ feature: "blog", layer: "controller", name: "blog.createArticle" },
createArticleController(wrappedCreateArticle),
),
),
);
}