feat(media): wire instrumentation — media repo spans + getMedia/listMedia/deleteMedia withSpan
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import "reflect-metadata";
|
||||
import { injectable } from "inversify";
|
||||
import {
|
||||
NoopTracer,
|
||||
NoopLogger,
|
||||
type ITracer,
|
||||
type ILogger,
|
||||
} from "@repo/core-shared/instrumentation";
|
||||
|
||||
import type { IMediaRepository } from "../../application/repositories/media.repository.interface";
|
||||
import type { Media } from "../../entities/models/media";
|
||||
@@ -7,6 +13,17 @@ import type { Media } from "../../entities/models/media";
|
||||
@injectable()
|
||||
export class MockMediaRepository implements IMediaRepository {
|
||||
private _media: Media[] = [];
|
||||
private tracer: ITracer;
|
||||
private logger: ILogger;
|
||||
|
||||
constructor(
|
||||
tracer: ITracer = new NoopTracer(),
|
||||
logger: ILogger = new NoopLogger(),
|
||||
) {
|
||||
this.tracer = tracer;
|
||||
this.logger = logger;
|
||||
void this.logger; // currently unused; reserved for future mock-thrown captures
|
||||
}
|
||||
|
||||
/** Test helper — seeds the in-memory store directly. */
|
||||
async _store(media: Media): Promise<void> {
|
||||
@@ -14,16 +31,44 @@ export class MockMediaRepository implements IMediaRepository {
|
||||
}
|
||||
|
||||
async getMedia(id: string): Promise<Media | undefined> {
|
||||
return this._media.find((m) => m.id === id);
|
||||
return this.tracer.startSpan(
|
||||
{ name: "media.getMedia", op: "repository", attributes: { id } },
|
||||
async (span) => {
|
||||
const found = this._media.find((m) => m.id === id);
|
||||
span.setAttribute("found", Boolean(found));
|
||||
return found;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async listMedia(opts?: { limit?: number; offset?: number }): Promise<Media[]> {
|
||||
const offset = opts?.offset ?? 0;
|
||||
const limit = opts?.limit ?? 50;
|
||||
return this._media.slice(offset, offset + limit);
|
||||
return this.tracer.startSpan(
|
||||
{
|
||||
name: "media.listMedia",
|
||||
op: "repository",
|
||||
attributes: {
|
||||
limit: opts?.limit ?? null,
|
||||
offset: opts?.offset ?? null,
|
||||
},
|
||||
},
|
||||
async (span) => {
|
||||
const offset = opts?.offset ?? 0;
|
||||
const limit = opts?.limit ?? 50;
|
||||
const result = this._media.slice(offset, offset + limit);
|
||||
span.setAttribute("count", result.length);
|
||||
return result;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async deleteMedia(id: string): Promise<void> {
|
||||
this._media = this._media.filter((m) => m.id !== id);
|
||||
return this.tracer.startSpan(
|
||||
{ name: "media.deleteMedia", op: "repository", attributes: { id } },
|
||||
async (span) => {
|
||||
const before = this._media.length;
|
||||
this._media = this._media.filter((m) => m.id !== id);
|
||||
span.setAttribute("deleted", this._media.length < before);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { RecordingTracer, RecordingLogger } from "@repo/core-testing/instrumentation";
|
||||
import { MockMediaRepository } from "@/infrastructure/repositories/media.repository.mock";
|
||||
import type { Media } from "@/entities/models/media";
|
||||
|
||||
const SAMPLE_MEDIA: Media = {
|
||||
id: "m1",
|
||||
alt: "Test image",
|
||||
url: "/test/image.jpg",
|
||||
filename: "image.jpg",
|
||||
mimeType: "image/jpeg",
|
||||
filesize: 1024,
|
||||
};
|
||||
|
||||
// Mock repo also wraps in spans (R42).
|
||||
describe("MockMediaRepository emits spans (R42)", () => {
|
||||
it("getMedia emits one span with op='repository' and found attribute", async () => {
|
||||
const tracer = new RecordingTracer();
|
||||
const logger = new RecordingLogger();
|
||||
const repo = new MockMediaRepository(tracer, logger);
|
||||
await repo._store(SAMPLE_MEDIA);
|
||||
await repo.getMedia("m1");
|
||||
expect(tracer.spans).toHaveLength(1);
|
||||
const getSpan = tracer.findSpan("media.getMedia");
|
||||
expect(getSpan).toBeDefined();
|
||||
expect(getSpan).toMatchObject({ name: "media.getMedia", op: "repository" });
|
||||
expect(getSpan!.attributes.found).toBe(true);
|
||||
});
|
||||
|
||||
it("listMedia emits a span with count attribute", async () => {
|
||||
const tracer = new RecordingTracer();
|
||||
const repo = new MockMediaRepository(tracer);
|
||||
await repo._store(SAMPLE_MEDIA);
|
||||
tracer.reset();
|
||||
await repo.listMedia({ limit: 10 });
|
||||
expect(tracer.findSpan("media.listMedia")).toBeDefined();
|
||||
expect(tracer.findSpan("media.listMedia")!.attributes.limit).toBe(10);
|
||||
expect(tracer.findSpan("media.listMedia")!.attributes.count).toBe(1);
|
||||
});
|
||||
|
||||
it("deleteMedia emits a span with deleted=true", async () => {
|
||||
const tracer = new RecordingTracer();
|
||||
const repo = new MockMediaRepository(tracer);
|
||||
await repo._store(SAMPLE_MEDIA);
|
||||
tracer.reset();
|
||||
await repo.deleteMedia("m1");
|
||||
expect(tracer.findSpan("media.deleteMedia")).toBeDefined();
|
||||
expect(tracer.findSpan("media.deleteMedia")!.attributes.deleted).toBe(true);
|
||||
expect(tracer.findSpan("media.deleteMedia")!.attributes.id).toBe("m1");
|
||||
});
|
||||
});
|
||||
@@ -32,7 +32,10 @@ function buildPayloadStub() {
|
||||
findByID: vi.fn(
|
||||
async ({ id }: { collection: string; id: string; overrideAccess?: boolean }) => {
|
||||
const doc = store.get(String(id));
|
||||
if (!doc) throw new Error(`Not found: ${id}`);
|
||||
if (!doc) {
|
||||
const err = Object.assign(new Error(`Not found: ${id}`), { status: 404 });
|
||||
throw err;
|
||||
}
|
||||
return doc;
|
||||
},
|
||||
),
|
||||
@@ -71,7 +74,9 @@ describe("MediaRepository", () => {
|
||||
it("returns undefined when Payload throws (not found)", async () => {
|
||||
const { getPayload } = await import("payload");
|
||||
(getPayload as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
findByID: vi.fn().mockRejectedValue(new Error("Not found")),
|
||||
findByID: vi.fn().mockRejectedValue(
|
||||
Object.assign(new Error("Not found"), { status: 404 }),
|
||||
),
|
||||
});
|
||||
|
||||
const repo = new MediaRepository(stubPayloadConfig);
|
||||
|
||||
@@ -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 { IMediaRepository } from "../../application/repositories/media.repository.interface";
|
||||
import type { Media } from "../../entities/models/media";
|
||||
@@ -30,47 +36,114 @@ function mapDoc(doc: PayloadMediaDoc): Media {
|
||||
};
|
||||
}
|
||||
|
||||
const FEATURE = "media" as const;
|
||||
const REPO = "media" as const;
|
||||
|
||||
@injectable()
|
||||
export class MediaRepository implements IMediaRepository {
|
||||
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 getMedia(id: string): Promise<Media | undefined> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
try {
|
||||
const doc = await payload.findByID({
|
||||
collection: "media",
|
||||
id,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return mapDoc(doc as PayloadMediaDoc);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return this.tracer.startSpan(
|
||||
{ name: "media.getMedia", op: "repository", attributes: { id } },
|
||||
async (span) => {
|
||||
try {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const doc = await payload.findByID({
|
||||
collection: "media",
|
||||
id,
|
||||
overrideAccess: true,
|
||||
});
|
||||
span.setAttribute("found", true);
|
||||
return mapDoc(doc as PayloadMediaDoc);
|
||||
} 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: "getMedia" },
|
||||
});
|
||||
span.setStatus("error", err instanceof Error ? err.message : String(err));
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async listMedia(opts?: { limit?: number; offset?: number }): Promise<Media[]> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const result = await payload.find({
|
||||
collection: "media",
|
||||
limit: opts?.limit ?? 50,
|
||||
page: opts?.offset
|
||||
? Math.floor(opts.offset / (opts.limit ?? 50)) + 1
|
||||
: 1,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return result.docs.map((d) => mapDoc(d as PayloadMediaDoc));
|
||||
return this.tracer.startSpan(
|
||||
{
|
||||
name: "media.listMedia",
|
||||
op: "repository",
|
||||
attributes: {
|
||||
limit: opts?.limit ?? null,
|
||||
offset: opts?.offset ?? null,
|
||||
},
|
||||
},
|
||||
async (span) => {
|
||||
try {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
const result = await payload.find({
|
||||
collection: "media",
|
||||
limit: opts?.limit ?? 50,
|
||||
page: opts?.offset
|
||||
? Math.floor(opts.offset / (opts.limit ?? 50)) + 1
|
||||
: 1,
|
||||
overrideAccess: true,
|
||||
});
|
||||
const items = result.docs.map((d) => mapDoc(d as PayloadMediaDoc));
|
||||
span.setAttribute("count", items.length);
|
||||
return items;
|
||||
} catch (err) {
|
||||
this.logger.captureException(err, {
|
||||
tags: { feature: FEATURE, repo: REPO, method: "listMedia" },
|
||||
});
|
||||
span.setStatus("error", err instanceof Error ? err.message : String(err));
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async deleteMedia(id: string): Promise<void> {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
await payload.delete({
|
||||
collection: "media",
|
||||
id,
|
||||
overrideAccess: true,
|
||||
});
|
||||
return this.tracer.startSpan(
|
||||
{ name: "media.deleteMedia", op: "repository", attributes: { id } },
|
||||
async (span) => {
|
||||
try {
|
||||
const payload = await getPayload({ config: this.config });
|
||||
await payload.delete({
|
||||
collection: "media",
|
||||
id,
|
||||
overrideAccess: true,
|
||||
});
|
||||
span.setAttribute("deleted", true);
|
||||
} catch (err) {
|
||||
span.setAttribute("deleted", false);
|
||||
this.logger.captureException(err, {
|
||||
tags: { feature: FEATURE, repo: REPO, method: "deleteMedia" },
|
||||
});
|
||||
span.setStatus("error", err instanceof Error ? err.message : String(err));
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user