feat(blog): wire instrumentation — repo spans + use-case/controller withSpan + logger capture
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import "reflect-metadata";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { NoopTracer, NoopLogger } from "@repo/core-shared/instrumentation";
|
||||
import { bindDevSeedBlog } from "@/di/bind-dev-seed";
|
||||
import { blogContainer } from "@/di/container";
|
||||
import { BLOG_SYMBOLS } from "@/di/symbols";
|
||||
@@ -7,11 +8,14 @@ import { MockArticlesRepository } from "@/infrastructure/repositories/articles.r
|
||||
import type { IArticlesRepository } from "@/application/repositories/articles.repository.interface";
|
||||
|
||||
describe("bindDevSeedBlog", () => {
|
||||
const tracer = new NoopTracer();
|
||||
const logger = new NoopLogger();
|
||||
|
||||
// Each test starts from the default empty-mock binding and tears down
|
||||
// afterwards so the global blogContainer state stays clean for siblings.
|
||||
beforeEach(() => {
|
||||
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
|
||||
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
|
||||
for (const sym of Object.values(BLOG_SYMBOLS)) {
|
||||
if (blogContainer.isBound(sym)) blogContainer.unbind(sym);
|
||||
}
|
||||
blogContainer
|
||||
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
|
||||
@@ -19,8 +23,8 @@ describe("bindDevSeedBlog", () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
|
||||
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
|
||||
for (const sym of Object.values(BLOG_SYMBOLS)) {
|
||||
if (blogContainer.isBound(sym)) blogContainer.unbind(sym);
|
||||
}
|
||||
blogContainer
|
||||
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
|
||||
@@ -28,7 +32,7 @@ describe("bindDevSeedBlog", () => {
|
||||
});
|
||||
|
||||
it("populates the repository with the dev articles", async () => {
|
||||
await bindDevSeedBlog();
|
||||
await bindDevSeedBlog(tracer, logger);
|
||||
|
||||
const repo = blogContainer.get<IArticlesRepository>(
|
||||
BLOG_SYMBOLS.IArticlesRepository,
|
||||
@@ -39,7 +43,7 @@ describe("bindDevSeedBlog", () => {
|
||||
});
|
||||
|
||||
it("seeds the welcome article reachable by slug", async () => {
|
||||
await bindDevSeedBlog();
|
||||
await bindDevSeedBlog(tracer, logger);
|
||||
|
||||
const repo = blogContainer.get<IArticlesRepository>(
|
||||
BLOG_SYMBOLS.IArticlesRepository,
|
||||
@@ -52,13 +56,13 @@ describe("bindDevSeedBlog", () => {
|
||||
});
|
||||
|
||||
it("is idempotent — calling twice rebuilds a fresh populated repo", async () => {
|
||||
await bindDevSeedBlog();
|
||||
await bindDevSeedBlog(tracer, logger);
|
||||
const before = blogContainer.get<IArticlesRepository>(
|
||||
BLOG_SYMBOLS.IArticlesRepository,
|
||||
);
|
||||
const beforeCount = (await before.getArticles()).length;
|
||||
|
||||
await bindDevSeedBlog();
|
||||
await bindDevSeedBlog(tracer, logger);
|
||||
const after = blogContainer.get<IArticlesRepository>(
|
||||
BLOG_SYMBOLS.IArticlesRepository,
|
||||
);
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
import {
|
||||
withSpan,
|
||||
INSTRUMENTATION_SYMBOLS,
|
||||
type ITracer,
|
||||
type ILogger,
|
||||
} from "@repo/core-shared/instrumentation";
|
||||
import { blogContainer } from "./container.js";
|
||||
import { BLOG_SYMBOLS } from "./symbols.js";
|
||||
import { MockArticlesRepository } from "../infrastructure/repositories/articles.repository.mock.js";
|
||||
import { buildDevArticles } from "../__seeds__/dev.js";
|
||||
import { getArticlesUseCase } from "../application/use-cases/get-articles.use-case.js";
|
||||
import { getArticleBySlugUseCase } from "../application/use-cases/get-article-by-slug.use-case.js";
|
||||
import { createArticleUseCase } from "../application/use-cases/create-article.use-case.js";
|
||||
import { getArticlesController } from "../interface-adapters/controllers/get-articles.controller.js";
|
||||
import { getArticleBySlugController } from "../interface-adapters/controllers/get-article-by-slug.controller.js";
|
||||
import { createArticleController } from "../interface-adapters/controllers/create-article.controller.js";
|
||||
import type { IArticlesRepository } from "../application/repositories/articles.repository.interface.js";
|
||||
|
||||
/**
|
||||
@@ -14,17 +26,86 @@ import type { IArticlesRepository } from "../application/repositories/articles.r
|
||||
* Idempotent: safe to call multiple times; each call rebuilds a fresh
|
||||
* populated repo and rebinds the symbol.
|
||||
*/
|
||||
export async function bindDevSeedBlog(): Promise<void> {
|
||||
export async function bindDevSeedBlog(tracer: ITracer, logger: ILogger): Promise<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);
|
||||
|
||||
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
|
||||
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
|
||||
}
|
||||
|
||||
const repo = new MockArticlesRepository();
|
||||
const repo = new MockArticlesRepository(tracer, logger);
|
||||
for (const article of buildDevArticles()) {
|
||||
await repo.createArticle(article);
|
||||
}
|
||||
|
||||
blogContainer
|
||||
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
|
||||
.toConstantValue(repo);
|
||||
|
||||
// Wrap use cases + controllers identically to bind-production
|
||||
const wrappedGetArticles = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticles", op: "use-case" },
|
||||
getArticlesUseCase(repo),
|
||||
);
|
||||
const wrappedGetArticleBySlug = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticleBySlug", op: "use-case" },
|
||||
getArticleBySlugUseCase(repo),
|
||||
);
|
||||
const wrappedCreateArticle = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.createArticle", op: "use-case" },
|
||||
createArticleUseCase(repo),
|
||||
);
|
||||
|
||||
for (const sym of [
|
||||
BLOG_SYMBOLS.IGetArticlesUseCase,
|
||||
BLOG_SYMBOLS.IGetArticleBySlugUseCase,
|
||||
BLOG_SYMBOLS.ICreateArticleUseCase,
|
||||
BLOG_SYMBOLS.IGetArticlesController,
|
||||
BLOG_SYMBOLS.IGetArticleBySlugController,
|
||||
BLOG_SYMBOLS.ICreateArticleController,
|
||||
]) {
|
||||
if (blogContainer.isBound(sym)) blogContainer.unbind(sym);
|
||||
}
|
||||
blogContainer.bind(BLOG_SYMBOLS.IGetArticlesUseCase).toConstantValue(wrappedGetArticles);
|
||||
blogContainer
|
||||
.bind(BLOG_SYMBOLS.IGetArticleBySlugUseCase)
|
||||
.toConstantValue(wrappedGetArticleBySlug);
|
||||
blogContainer.bind(BLOG_SYMBOLS.ICreateArticleUseCase).toConstantValue(wrappedCreateArticle);
|
||||
|
||||
blogContainer
|
||||
.bind(BLOG_SYMBOLS.IGetArticlesController)
|
||||
.toConstantValue(
|
||||
withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticles", op: "controller" },
|
||||
getArticlesController(wrappedGetArticles),
|
||||
),
|
||||
);
|
||||
blogContainer
|
||||
.bind(BLOG_SYMBOLS.IGetArticleBySlugController)
|
||||
.toConstantValue(
|
||||
withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticleBySlug", op: "controller" },
|
||||
getArticleBySlugController(wrappedGetArticleBySlug),
|
||||
),
|
||||
);
|
||||
blogContainer
|
||||
.bind(BLOG_SYMBOLS.ICreateArticleController)
|
||||
.toConstantValue(
|
||||
withSpan(
|
||||
tracer,
|
||||
{ name: "blog.createArticle", op: "controller" },
|
||||
createArticleController(wrappedCreateArticle),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,109 @@
|
||||
import type { SanitizedConfig } from "payload";
|
||||
import {
|
||||
withSpan,
|
||||
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): void {
|
||||
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 at bind time (R41)
|
||||
const wrappedGetArticles = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticles", op: "use-case" },
|
||||
getArticlesUseCase(repo),
|
||||
);
|
||||
const wrappedGetArticleBySlug = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticleBySlug", op: "use-case" },
|
||||
getArticleBySlugUseCase(repo),
|
||||
);
|
||||
const wrappedCreateArticle = withSpan(
|
||||
tracer,
|
||||
{ name: "blog.createArticle", op: "use-case" },
|
||||
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.IArticlesRepository)
|
||||
.toConstantValue(new ArticlesRepository(config));
|
||||
.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" },
|
||||
getArticlesController(wrappedGetArticles),
|
||||
),
|
||||
);
|
||||
blogContainer
|
||||
.bind(BLOG_SYMBOLS.IGetArticleBySlugController)
|
||||
.toConstantValue(
|
||||
withSpan(
|
||||
tracer,
|
||||
{ name: "blog.getArticleBySlug", op: "controller" },
|
||||
getArticleBySlugController(wrappedGetArticleBySlug),
|
||||
),
|
||||
);
|
||||
blogContainer
|
||||
.bind(BLOG_SYMBOLS.ICreateArticleController)
|
||||
.toConstantValue(
|
||||
withSpan(
|
||||
tracer,
|
||||
{ name: "blog.createArticle", op: "controller" },
|
||||
createArticleController(wrappedCreateArticle),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user