feat(diagnostics): correlate staff operations errors and audit records
CI / check (push) Successful in 56s
CI / deploy (push) Successful in 19s
CI / publish-container (push) Successful in 1m15s

This commit is contained in:
Simo committed 2026-09-11 00:34:49 +02:00
1 parent 440ce4e556
commit a2954e4408
14 files changed
+473 -110

No files matched your search

@@ -0,0 +1,27 @@
import { expect, it } from "vitest";
import {
createStore,
getOperationContext,
runWithStore,
setContextUserId,
} from "./request-context";
import type { IpAddress, UserId } from "./types";
it("isolates operation IDs and users across concurrent asynchronous requests", async () => {
const first = createStore("test" as IpAddress),
second = createStore("test" as IpAddress);
expect(first.requestId).not.toBe(second.requestId);
await Promise.all(
[first, second].map((store, index) =>
runWithStore(store, async () => {
setContextUserId((index + 1) as UserId);
await new Promise((resolve) => setTimeout(resolve, 5 - index));
expect(getOperationContext()).toEqual({
operationId: store.requestId,
userId: index + 1,
});
}),
),
);
expect(getOperationContext()).toEqual({});
});
+14
View File
@@ -38,3 +38,17 @@ export function setContextUserId(userId: UserId): void {
const store = als.getStore();
if (store) store.userId = userId;
}
/** Stable correlation data exists only inside an active request or job. */
export function getOperationContext(): {
operationId?: string;
userId?: number;
} {
const store = als.getStore();
return store
? {
operationId: store.requestId,
...(store.userId === null ? {} : { userId: Number(store.userId) }),
}
: {};
}