Skip to content

Commit 620bd7e

Browse files
authored
Merge pull request #203 from aws/maxday/fix-tenantId
fix: include tenantId in custom JSON log entries
2 parents 631d461 + 3991a9d commit 620bd7e

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎src/logging/log-patch.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe("LogPatch", async () => {
1010
const originalConsole = { ...console };
1111
const mockTimestamp = "2024-01-01T00:00:00.000Z";
1212
const mockRequestId = "test-request-id";
13+
const mockTenantId = "test-tenant-id";
1314

1415
const invokeStore = await InvokeStore.getInstanceAsync();
1516

@@ -25,6 +26,7 @@ describe("LogPatch", async () => {
2526

2627
// Mock InvokeStore
2728
vi.spyOn(invokeStore, "getRequestId").mockReturnValue(mockRequestId);
29+
vi.spyOn(invokeStore, "getTenantId").mockReturnValue(mockTenantId);
2830

2931
// Mock Date
3032
vi.spyOn(Date.prototype, "toISOString").mockReturnValue(mockTimestamp);
@@ -230,6 +232,9 @@ describe("LogPatch", async () => {
230232
expect(process.stdout.write).toHaveBeenCalledWith(
231233
expect.stringContaining(`"requestId":"${mockRequestId}"`),
232234
);
235+
expect(process.stdout.write).toHaveBeenCalledWith(
236+
expect.stringContaining(`"tenantId":"${mockTenantId}"`),
237+
);
233238
expect(process.stdout.write).toHaveBeenCalledWith(
234239
expect.stringContaining(`"level":"INFO"`),
235240
);

‎src/runtime/runtime.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,30 @@ describe("Runtime", () => {
353353
]);
354354
});
355355

356+
it("should set tenantId in InvokeStore for buffered invocations", async () => {
357+
// GIVEN
358+
const mockRapidClient = createMockRapidClient(-1, false, true);
359+
const storeOrder: (string | undefined)[] = [];
360+
361+
const BufferedHandler: BufferedHandler = async () => {
362+
storeOrder.push(invokeStore.getTenantId());
363+
await new Promise((resolve) => setTimeout(resolve, 50)); // Simulate work
364+
return "done";
365+
};
366+
367+
const runtime = Runtime.create({
368+
rapidClient: mockRapidClient,
369+
handler: BufferedHandler,
370+
isMultiConcurrent: false,
371+
});
372+
373+
// WHEN
374+
await startAndTickRuntime(runtime, 60);
375+
376+
// THEN - tenantId is pinned to the sandbox, constant across invocations
377+
expect(storeOrder).toEqual(["test-tenant", "test-tenant"]);
378+
});
379+
356380
it("should set requestId and traceId in InvokeStore for streaming invocations", async () => {
357381
// GIVEN
358382
const mockRapidClient = createMockRapidClient(3, true);
@@ -556,6 +580,45 @@ describe("Runtime", () => {
556580
expect(mockRapidClient.postInvocationResponse).toHaveBeenCalledTimes(2);
557581
});
558582

583+
it("should set tenantId in InvokeStore for concurrent invocations", async () => {
584+
// GIVEN
585+
const mockRapidClient = createMockRapidClient(3, false, true);
586+
const storeOrder: (string | undefined)[] = [];
587+
const completionOrder: (string | undefined)[] = [];
588+
589+
const BufferedHandler: BufferedHandler = async () => {
590+
const invocationNumber = storeOrder.length + 1;
591+
592+
storeOrder.push(invokeStore.getTenantId());
593+
594+
await new Promise((resolve) =>
595+
setTimeout(resolve, invocationNumber === 1 ? 10 : 5),
596+
);
597+
598+
completionOrder.push(invokeStore.getTenantId());
599+
600+
return "done";
601+
};
602+
603+
const runtime = Runtime.create({
604+
rapidClient: mockRapidClient,
605+
handler: BufferedHandler,
606+
isMultiConcurrent: true,
607+
});
608+
609+
// WHEN
610+
runtime.start();
611+
await new Promise((resolve) => setTimeout(resolve, 20));
612+
613+
// THEN - tenantId is pinned to the sandbox, so every concurrent
614+
// invocation reads the same value, both at start and after async gaps.
615+
expect(storeOrder).toEqual(["test-tenant", "test-tenant"]);
616+
expect(completionOrder).toEqual(["test-tenant", "test-tenant"]);
617+
618+
expect(mockRapidClient.nextInvocation).toHaveBeenCalledTimes(3);
619+
expect(mockRapidClient.postInvocationResponse).toHaveBeenCalledTimes(2);
620+
});
621+
559622
it("should maintain context when mixing different timer types in RIC", async () => {
560623
// GIVEN
561624
const mockRapidClient = createMockRapidClient(3, true);
@@ -897,6 +960,7 @@ async function startAndTickRuntime(
897960
function createMockRapidClient(
898961
throttleAt: number = -1,
899962
passXRayTraceId: boolean = false,
963+
passTenantId: boolean = false,
900964
): RAPIDClient {
901965
let invocationCount = 0;
902966
return {
@@ -915,6 +979,14 @@ function createMockRapidClient(
915979
"lambda-runtime-trace-id": `trace-id-${invocationCount}`,
916980
}
917981
: {}),
982+
...(passTenantId
983+
? {
984+
// tenantId is pinned to the sandbox, so it's constant across
985+
// every invocation in the execution environment's lifecycle
986+
// (unlike requestId/traceId, which change per invocation).
987+
"lambda-runtime-aws-tenant-id": "test-tenant",
988+
}
989+
: {}),
918990
},
919991
});
920992
}),

‎src/runtime/runtime.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class Runtime {
6767
await this.runWithInvokeContext(
6868
context.awsRequestId,
6969
context.xRayTraceId,
70+
context.tenantId,
7071
() => processor.processInvoke(context, event),
7172
);
7273
}
@@ -82,6 +83,7 @@ export class Runtime {
8283
await this.runWithInvokeContext(
8384
context.awsRequestId,
8485
context.xRayTraceId,
86+
context.tenantId,
8587
() => processor.processInvoke(context, event),
8688
);
8789
});
@@ -91,13 +93,15 @@ export class Runtime {
9193
private async runWithInvokeContext(
9294
requestId: string,
9395
xRayTraceId: string | undefined,
96+
tenantId: string | undefined,
9497
fn: () => Promise<void>,
9598
): Promise<void> {
9699
const invokeStore = await InvokeStore.getInstanceAsync();
97100
return invokeStore.run(
98101
{
99102
[InvokeStoreBase.PROTECTED_KEYS.REQUEST_ID]: requestId,
100103
[InvokeStoreBase.PROTECTED_KEYS.X_RAY_TRACE_ID]: xRayTraceId,
104+
[InvokeStoreBase.PROTECTED_KEYS.TENANT_ID]: tenantId,
101105
},
102106
fn,
103107
);

0 commit comments

Comments
 (0)