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

feat(clients): support recursion detection in Lambda #3654

Merged
merged 3 commits into from Jun 3, 2022

Conversation

AllanZhengYP
Copy link
Contributor

Ref JS-3324

Description

Injects a trace id header in Lambda runtime to avoid recursive invocation.

Testing

Unit test


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

kuhe
kuhe previously approved these changes Jun 3, 2022
Comment on lines 25 to 48
const recursionDetectionMiddleware = (options: PreviouslyResolved): BuildMiddleware<any, any> =>
(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
const { request } = args;
if (
!HttpRequest.isInstance(request) ||
options.runtime !== "node" ||
request.headers.hasOwnProperty(TRACE_ID_HEADER_NAME)
) {
return next(args);
}

const functionName = process.env[ENV_LAMBDA_FUNCTION_NAME];
const traceId = process.env[ENV_TRACE_ID];
const nonEmptyString = (str: unknown): str is string => typeof str === "string" && str.length > 0;
if (nonEmptyString(functionName) && nonEmptyString(traceId)) {
request.headers[TRACE_ID_HEADER_NAME] = traceId;
}
return next({
...args,
request,
});
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove additional semicolon and update formatting

Suggested change
const recursionDetectionMiddleware = (options: PreviouslyResolved): BuildMiddleware<any, any> =>
(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
const { request } = args;
if (
!HttpRequest.isInstance(request) ||
options.runtime !== "node" ||
request.headers.hasOwnProperty(TRACE_ID_HEADER_NAME)
) {
return next(args);
}
const functionName = process.env[ENV_LAMBDA_FUNCTION_NAME];
const traceId = process.env[ENV_TRACE_ID];
const nonEmptyString = (str: unknown): str is string => typeof str === "string" && str.length > 0;
if (nonEmptyString(functionName) && nonEmptyString(traceId)) {
request.headers[TRACE_ID_HEADER_NAME] = traceId;
}
return next({
...args,
request,
});
};
}
const recursionDetectionMiddleware =
(options: PreviouslyResolved): BuildMiddleware<any, any> =>
(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
const { request } = args;
if (
!HttpRequest.isInstance(request) ||
options.runtime !== "node" ||
request.headers.hasOwnProperty(TRACE_ID_HEADER_NAME)
) {
return next(args);
}
const functionName = process.env[ENV_LAMBDA_FUNCTION_NAME];
const traceId = process.env[ENV_TRACE_ID];
const nonEmptyString = (str: unknown): str is string => typeof str === "string" && str.length > 0;
if (nonEmptyString(functionName) && nonEmptyString(traceId)) {
request.headers[TRACE_ID_HEADER_NAME] = traceId;
}
return next({
...args,
request,
});
};

@AllanZhengYP
Copy link
Contributor Author

The CodeBuild test failed because of transient issue in the last size benchmarking. The rest tests succeeded

@AllanZhengYP AllanZhengYP merged commit ecfe46e into aws:main Jun 3, 2022
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants