Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lambda context propagation #269

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export interface RestateEndpoint {
* @returns The invocation handler function for to be called by AWS Lambda.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
lambdaHandler(): (event: any) => Promise<any>;
lambdaHandler(): (event: any, ctx: any) => Promise<any>;

/**
* Serve this Restate Endpoint as HTTP2 server, listening to the given port.
Expand Down
2 changes: 1 addition & 1 deletion src/endpoint/endpoint_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class EndpointImpl implements RestateEndpoint {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
lambdaHandler(): (event: any) => Promise<any> {
lambdaHandler(): (event: any, ctx: any) => Promise<any> {
const handler = new LambdaHandler(this);
return handler.handleRequest.bind(handler);
}
Expand Down
11 changes: 7 additions & 4 deletions src/endpoint/lambda_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
APIGatewayProxyEventV2,
APIGatewayProxyResult,
APIGatewayProxyResultV2,
Context,
} from "aws-lambda";
import {
ProtocolMode,
Expand Down Expand Up @@ -44,7 +45,8 @@ export class LambdaHandler {
* This is the main request handling method, effectively a typed variant of `create()`.
*/
async handleRequest(
event: APIGatewayProxyEvent | APIGatewayProxyEventV2
event: APIGatewayProxyEvent | APIGatewayProxyEventV2,
context: Context
): Promise<APIGatewayProxyResult | APIGatewayProxyResultV2> {
let path;
if ("path" in event) {
Expand All @@ -69,7 +71,7 @@ export class LambdaHandler {
pathSegments[pathSegments.length - 3] === "invoke"
) {
const url = "/" + pathSegments.slice(-3).join("/");
return await this.handleInvoke(url, event);
return await this.handleInvoke(url, event, context);
} else if (pathSegments[pathSegments.length - 1] === "discover") {
return this.handleDiscovery();
} else {
Expand All @@ -81,7 +83,8 @@ export class LambdaHandler {

private async handleInvoke(
url: string,
event: APIGatewayProxyEvent | APIGatewayProxyEventV2
event: APIGatewayProxyEvent | APIGatewayProxyEventV2,
context: Context
): Promise<APIGatewayProxyResult | APIGatewayProxyResultV2> {
try {
const method = this.endpoint.methodByUrl(url);
Expand Down Expand Up @@ -120,7 +123,7 @@ export class LambdaHandler {
ProtocolMode.REQUEST_RESPONSE,
method.method.keyedContext,
invocation.inferLoggerContext({
AWSRequestId: event.requestContext.requestId,
AWSRequestId: context.awsRequestId,
})
);
await stateMachine.invoke();
Expand Down
12 changes: 6 additions & 6 deletions test/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe("LambdaGreeter", () => {
getStateMessage("STATE", "Foo"),
])
);
const result = await handler(request);
const result = await handler(request, {});

expect(result.statusCode).toStrictEqual(200);
expect(result.headers).toStrictEqual({
Expand All @@ -156,7 +156,7 @@ describe("LambdaGreeter", () => {
"application/restate",
serializeMessages([startMessage(1), inputMessage(greetRequest("Pete"))])
);
const result = await handler(request);
const result = await handler(request, {});

expect(result.statusCode).toStrictEqual(500);
expect(result.headers).toStrictEqual({
Expand All @@ -177,7 +177,7 @@ describe("LambdaGreeter", () => {
"application/restate",
serializeMessages([startMessage(1), inputMessage(greetRequest("Pete"))])
);
const result = await handler(request);
const result = await handler(request, {});

expect(result.statusCode).toStrictEqual(500);
expect(result.headers).toStrictEqual({
Expand All @@ -197,7 +197,7 @@ describe("LambdaGreeter", () => {
"application/restate",
serializeMessages([startMessage(1), inputMessage(greetRequest("Pete"))])
);
const result = await handler(request);
const result = await handler(request, {});

expect(result.statusCode).toStrictEqual(500);
expect(result.headers).toStrictEqual({
Expand All @@ -217,7 +217,7 @@ describe("LambdaGreeter", () => {
"application/restate",
serializeMessages([startMessage(1), inputMessage(greetRequest("Pete"))])
);
const result = await handler(request);
const result = await handler(request, {});

expect(result.statusCode).toStrictEqual(404);
expect(result.headers).toStrictEqual({
Expand All @@ -241,7 +241,7 @@ describe("LambdaGreeter", () => {
discoverRequest
);

const result = await handler(request);
const result = await handler(request, {});

expect(result.statusCode).toStrictEqual(200);
expect(result.headers).toStrictEqual({
Expand Down
Loading