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:
@@ -2,6 +2,12 @@ import "reflect-metadata";
|
||||
import { injectable } from "inversify";
|
||||
import { getPayload } from "payload";
|
||||
import type { SanitizedConfig } from "payload";
|
||||
import {
|
||||
NoopTracer,
|
||||
NoopLogger,
|
||||
type ITracer,
|
||||
type ILogger,
|
||||
} from "@repo/core-shared/instrumentation";
|
||||
|
||||
import type { IArticlesRepository } from "../../application/repositories/articles.repository.interface";
|
||||
import type { Article } from "../../entities/models/article";
|
||||
@@ -36,38 +42,83 @@ function mapDoc(doc: PayloadArticleDoc): Article {
|
||||
};
|
||||
}
|
||||
|
||||
const FEATURE = "blog" as const;
|
||||
const REPO = "articles" as const;
|
||||
|
||||
@injectable()
|
||||
export class ArticlesRepository implements IArticlesRepository {
|
||||
private config: SanitizedConfig;
|
||||
private tracer: ITracer;
|
||||
private logger: ILogger;
|
||||
|
||||
constructor(config: SanitizedConfig) {
|
||||
constructor(
|
||||
config: SanitizedConfig,
|
||||
tracer: ITracer = new NoopTracer(),
|
||||
logger: ILogger = new NoopLogger(),
|
||||
) {
|
||||
this.config = config;
|
||||
this.tracer = tracer;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
async getArticle(id: string): Promise<Article | undefined> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
try {
|
||||
const doc = await payload.findByID({
|
||||
collection: "articles",
|
||||
id,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return mapDoc(doc as PayloadArticleDoc);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return this.tracer.startSpan(
|
||||
{ name: "articles.getArticle", op: "repository", attributes: { id } },
|
||||
async (span) => {
|
||||
try {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const doc = await payload.findByID({
|
||||
collection: "articles",
|
||||
id,
|
||||
overrideAccess: true,
|
||||
});
|
||||
span.setAttribute("found", true);
|
||||
return mapDoc(doc as PayloadArticleDoc);
|
||||
} catch (err) {
|
||||
// Payload throws on not-found; treat as undefined per existing semantics
|
||||
if (
|
||||
err &&
|
||||
typeof err === "object" &&
|
||||
"status" in err &&
|
||||
(err as { status: unknown }).status === 404
|
||||
) {
|
||||
span.setAttribute("found", false);
|
||||
return undefined;
|
||||
}
|
||||
this.logger.captureException(err, {
|
||||
tags: { feature: FEATURE, repo: REPO, method: "getArticle" },
|
||||
});
|
||||
span.setStatus("error", err instanceof Error ? err.message : String(err));
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async getArticleBySlug(slug: string): Promise<Article | undefined> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const result = await payload.find({
|
||||
collection: "articles",
|
||||
where: { slug: { equals: slug } },
|
||||
limit: 1,
|
||||
overrideAccess: true,
|
||||
});
|
||||
const doc = result.docs[0] as PayloadArticleDoc | undefined;
|
||||
return doc ? mapDoc(doc) : undefined;
|
||||
return this.tracer.startSpan(
|
||||
{ name: "articles.getArticleBySlug", op: "repository", attributes: { slug } },
|
||||
async (span) => {
|
||||
try {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const result = await payload.find({
|
||||
collection: "articles",
|
||||
where: { slug: { equals: slug } },
|
||||
limit: 1,
|
||||
overrideAccess: true,
|
||||
});
|
||||
const doc = result.docs[0] as PayloadArticleDoc | undefined;
|
||||
span.setAttribute("found", Boolean(doc));
|
||||
return doc ? mapDoc(doc) : undefined;
|
||||
} catch (err) {
|
||||
this.logger.captureException(err, {
|
||||
tags: { feature: FEATURE, repo: REPO, method: "getArticleBySlug" },
|
||||
});
|
||||
span.setStatus("error", err instanceof Error ? err.message : String(err));
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async getArticles(options?: {
|
||||
@@ -76,60 +127,116 @@ export class ArticlesRepository implements IArticlesRepository {
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}): Promise<Article[]> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const where: Record<string, { equals: string }> = {};
|
||||
if (options?.status) where.status = { equals: options.status };
|
||||
if (options?.authorId) where.author = { equals: options.authorId };
|
||||
return this.tracer.startSpan(
|
||||
{
|
||||
name: "articles.getArticles",
|
||||
op: "repository",
|
||||
attributes: {
|
||||
status: options?.status ?? null,
|
||||
authorId: options?.authorId ?? null,
|
||||
limit: options?.limit ?? null,
|
||||
offset: options?.offset ?? null,
|
||||
},
|
||||
},
|
||||
async (span) => {
|
||||
try {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const where: Record<string, { equals: string }> = {};
|
||||
if (options?.status) where.status = { equals: options.status };
|
||||
if (options?.authorId) where.author = { equals: options.authorId };
|
||||
|
||||
const result = await payload.find({
|
||||
collection: "articles",
|
||||
where: where as never,
|
||||
limit: options?.limit ?? 50,
|
||||
page: options?.offset
|
||||
? Math.floor(options.offset / (options.limit ?? 50)) + 1
|
||||
: 1,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return result.docs.map((d) => mapDoc(d as PayloadArticleDoc));
|
||||
const result = await payload.find({
|
||||
collection: "articles",
|
||||
where: where as never,
|
||||
limit: options?.limit ?? 50,
|
||||
page: options?.offset
|
||||
? Math.floor(options.offset / (options.limit ?? 50)) + 1
|
||||
: 1,
|
||||
overrideAccess: true,
|
||||
});
|
||||
span.setAttribute("count", result.docs.length);
|
||||
return result.docs.map((d) => mapDoc(d as PayloadArticleDoc));
|
||||
} catch (err) {
|
||||
this.logger.captureException(err, {
|
||||
tags: { feature: FEATURE, repo: REPO, method: "getArticles" },
|
||||
});
|
||||
span.setStatus("error", err instanceof Error ? err.message : String(err));
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async createArticle(input: Article): Promise<Article> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const created = await payload.create({
|
||||
collection: "articles",
|
||||
data: {
|
||||
title: input.title,
|
||||
slug: input.slug,
|
||||
content: input.content,
|
||||
status: input.status,
|
||||
author: input.authorId,
|
||||
} as never,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return mapDoc(created as PayloadArticleDoc);
|
||||
return this.tracer.startSpan(
|
||||
{ name: "articles.createArticle", op: "repository", attributes: { slug: input.slug } },
|
||||
async (span) => {
|
||||
try {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const created = await payload.create({
|
||||
collection: "articles",
|
||||
data: {
|
||||
title: input.title,
|
||||
slug: input.slug,
|
||||
content: input.content,
|
||||
status: input.status,
|
||||
author: input.authorId,
|
||||
} as never,
|
||||
overrideAccess: true,
|
||||
});
|
||||
span.setAttribute("id", String((created as PayloadArticleDoc).id));
|
||||
return mapDoc(created as PayloadArticleDoc);
|
||||
} catch (err) {
|
||||
this.logger.captureException(err, {
|
||||
tags: { feature: FEATURE, repo: REPO, method: "createArticle" },
|
||||
});
|
||||
span.setStatus("error", err instanceof Error ? err.message : String(err));
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async updateArticle(
|
||||
id: string,
|
||||
input: Partial<Article>,
|
||||
): Promise<Article | undefined> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
try {
|
||||
const updated = await payload.update({
|
||||
collection: "articles",
|
||||
id,
|
||||
data: {
|
||||
...(input.title !== undefined && { title: input.title }),
|
||||
...(input.slug !== undefined && { slug: input.slug }),
|
||||
...(input.content !== undefined && { content: input.content }),
|
||||
...(input.status !== undefined && { status: input.status }),
|
||||
...(input.authorId !== undefined && { author: input.authorId }),
|
||||
} as never,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return mapDoc(updated as PayloadArticleDoc);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return this.tracer.startSpan(
|
||||
{ name: "articles.updateArticle", op: "repository", attributes: { id } },
|
||||
async (span) => {
|
||||
try {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const updated = await payload.update({
|
||||
collection: "articles",
|
||||
id,
|
||||
data: {
|
||||
...(input.title !== undefined && { title: input.title }),
|
||||
...(input.slug !== undefined && { slug: input.slug }),
|
||||
...(input.content !== undefined && { content: input.content }),
|
||||
...(input.status !== undefined && { status: input.status }),
|
||||
...(input.authorId !== undefined && { author: input.authorId }),
|
||||
} as never,
|
||||
overrideAccess: true,
|
||||
});
|
||||
span.setAttribute("found", true);
|
||||
return mapDoc(updated as PayloadArticleDoc);
|
||||
} catch (err) {
|
||||
if (
|
||||
err &&
|
||||
typeof err === "object" &&
|
||||
"status" in err &&
|
||||
(err as { status: unknown }).status === 404
|
||||
) {
|
||||
span.setAttribute("found", false);
|
||||
return undefined;
|
||||
}
|
||||
this.logger.captureException(err, {
|
||||
tags: { feature: FEATURE, repo: REPO, method: "updateArticle" },
|
||||
});
|
||||
span.setStatus("error", err instanceof Error ? err.message : String(err));
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user