Skip to content

Commit 94e1e85

Browse files
committed
fix: inject tenantId into InvokeStore for custom JSON logs
runWithInvokeContext only stored REQUEST_ID and X_RAY_TRACE_ID in the InvokeStore, never TENANT_ID. The JSON loggers read tenantId via invokeStore.getTenantId() (PROTECTED_KEYS.TENANT_ID), so it was always undefined, fell back to "", and formatJsonMessage's `if (tenantId)` guard dropped the field. The header was parsed correctly onto context.tenantId (hence available in code and platform events), but that value never reached the store the loggers consult. Thread context.tenantId through runWithInvokeContext and set PROTECTED_KEYS.TENANT_ID for both the single- and multi-concurrent call sites so tenantId appears in custom JSON log entries, matching the documented behavior and the Python runtime. Add regression tests covering both call sites. tenantId is pinned to the sandbox, so it stays constant across every invocation in the execution environment's lifecycle (unlike requestId/traceId). Ref: V2290595765 cr: https://code.amazon.com/reviews/CR-290016955
1 parent 1114b37 commit 94e1e85

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎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)