85 lines
3.5 KiB
TypeScript
85 lines
3.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { getWebMetrics, resetWebMetricsForTests } from "./registry.js";
|
|
import { recordAuthorizedStorageDownload } from "./storage-traffic.js";
|
|
|
|
beforeEach(() => {
|
|
resetWebMetricsForTests();
|
|
});
|
|
|
|
describe("recordAuthorizedStorageDownload", () => {
|
|
it("records a bounded authorization count and known whole-object bytes", async () => {
|
|
recordAuthorizedStorageDownload({
|
|
surface: "files",
|
|
delivery: "signed_redirect",
|
|
objectSizeBytes: 1_048_576,
|
|
});
|
|
|
|
const text = await getWebMetrics().registry.metrics();
|
|
expect(text).toContain(
|
|
'storage_authorized_downloads_total{surface="files",delivery="signed_redirect",size_known="known",service="web",runtime_surface="core"} 1'
|
|
);
|
|
expect(text).toContain(
|
|
'storage_authorized_download_object_bytes_total{surface="files",delivery="signed_redirect",service="web",runtime_surface="core"} 1048576'
|
|
);
|
|
});
|
|
|
|
it.each([undefined, null, -1, Number.NaN, 1.5])("records an unknown size without inventing byte volume for %s", async (objectSizeBytes) => {
|
|
recordAuthorizedStorageDownload({ surface: "portal_document", delivery: "signed_redirect", objectSizeBytes });
|
|
|
|
const text = await getWebMetrics().registry.metrics();
|
|
expect(text).toContain(
|
|
'storage_authorized_downloads_total{surface="portal_document",delivery="signed_redirect",size_known="unknown",service="web",runtime_surface="core"} 1'
|
|
);
|
|
expect(text).not.toContain("storage_authorized_download_object_bytes_total{");
|
|
});
|
|
|
|
it("separates portal download counters from the Core runtime", async () => {
|
|
const previousSurface = process.env.WEB_RUNTIME_SURFACE;
|
|
process.env.WEB_RUNTIME_SURFACE = "portal";
|
|
resetWebMetricsForTests();
|
|
|
|
try {
|
|
recordAuthorizedStorageDownload({ surface: "portal_document", delivery: "signed_redirect", objectSizeBytes: 512 });
|
|
const text = await getWebMetrics().registry.metrics();
|
|
expect(text).toContain('runtime_surface="portal"');
|
|
expect(text).not.toContain('runtime_surface="core"');
|
|
} finally {
|
|
if (previousSurface === undefined) delete process.env.WEB_RUNTIME_SURFACE;
|
|
else process.env.WEB_RUNTIME_SURFACE = previousSurface;
|
|
resetWebMetricsForTests();
|
|
}
|
|
});
|
|
|
|
it("folds impossible runtime label values into other and never exports them", async () => {
|
|
recordAuthorizedStorageDownload({
|
|
surface: "tenant-secret" as "files",
|
|
delivery: "https://objects.example.test/signed" as "signed_redirect",
|
|
objectSizeBytes: 10,
|
|
});
|
|
|
|
const text = await getWebMetrics().registry.metrics();
|
|
expect(text).toContain('delivery="other"');
|
|
expect(text).toContain('surface="other"');
|
|
expect(text).not.toContain("tenant-secret");
|
|
expect(text).not.toContain("objects.example.test");
|
|
});
|
|
|
|
it("stays fail-open when either counter rejects an observation", () => {
|
|
const throwingCounter = {
|
|
labels: vi.fn(() => ({
|
|
inc: vi.fn(() => {
|
|
throw new Error("metrics down");
|
|
}),
|
|
})),
|
|
};
|
|
const metrics = {
|
|
storageAuthorizedDownloadsTotal: throwingCounter,
|
|
storageAuthorizedDownloadObjectBytesTotal: throwingCounter,
|
|
} as unknown as NonNullable<Parameters<typeof recordAuthorizedStorageDownload>[1]>["metrics"];
|
|
|
|
expect(() => recordAuthorizedStorageDownload({ surface: "files", delivery: "signed_redirect", objectSizeBytes: 42 }, { metrics })).not.toThrow();
|
|
expect(throwingCounter.labels).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|