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

AWS lambda - improvements in custom type handling in wrappers, SQS ev… #4254

Merged
merged 5 commits into from Oct 8, 2021
Merged

AWS lambda - improvements in custom type handling in wrappers, SQS ev… #4254

merged 5 commits into from Oct 8, 2021

Conversation

ghost
Copy link

@ghost ghost commented Sep 30, 2021

…en wrapper added

Two goals with this PR:

  • allow users of lambda wrappers to use custom types in their handlers (along with simple types and build-in events)
  • add wrapper for SQS event handler

@ghost ghost requested a review from mateuszrzeszutek October 4, 2021 21:24
} else {
try {
event = new APIGatewayProxyResponseEvent();
event.setBody(OBJECT_MAPPER.writeValueAsString(result));
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we know that our OBJECT_MAPPER is configured the same as Lambda?

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately no guarantees here. Haven't seen an official spec by AWS guys regarding how the JSON is mapped in their code.

event = new APIGatewayProxyResponseEvent();
event.setBody(OBJECT_MAPPER.writeValueAsString(result));
} catch (JsonProcessingException e) {
throw new IllegalStateException("Could not serialize return value.", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

These exceptions seem worrisome, are we sure we won't break the app?

Copy link
Author

Choose a reason for hiding this comment

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

The silver lining here is that if the wrapper is unable to map to a custom type, the user can still use the "APIGatewayProxyRequestEvent" and avoid mapping on the wrapper side entirely. So in the worst case it will work exactly the same way it does right now.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it still means we have the possibility of breaking the lambda just by the presence of a lambda layer such as the opentelemetry-lambda ones. I don't think it's good to have the possibility of adding crashing.

That being said I guess I can't find any alternative unfortunately. It's too bad the Context object doesn't provide information about HTTP requests...

Is it at least beneficial to split into one where the target handler is proxy that doesn't have any mapping and this one that does for custom return type?

Copy link
Author

Choose a reason for hiding this comment

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

It's implemented (kind of) this way - mapping ins only done if the return type of the handler is different than APIGatewayProxyResponseEvent. So if the wrapper mapping causes issues, the user can always switch to return the event. In other words both with input and result if the new wrapper breaks, user can implement own handler to input / return the event - and everything will work the same way it did (no mapping if no custom type used).

…lemetry/instrumentation/awslambda/v1_0/LambdaParameters.java

Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
@ghost ghost requested a review from anuraaga October 5, 2021 09:23
…lemetry/instrumentation/awslambda/v1_0/LambdaParameters.java

Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
event = new APIGatewayProxyResponseEvent();
event.setBody(OBJECT_MAPPER.writeValueAsString(result));
} catch (JsonProcessingException e) {
throw new IllegalStateException("Could not serialize return value.", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it still means we have the possibility of breaking the lambda just by the presence of a lambda layer such as the opentelemetry-lambda ones. I don't think it's good to have the possibility of adding crashing.

That being said I guess I can't find any alternative unfortunately. It's too bad the Context object doesn't provide information about HTTP requests...

Is it at least beneficial to split into one where the target handler is proxy that doesn't have any mapping and this one that does for custom return type?

}

// Visible for testing
static Object map(Object jsonMap, Class clazz) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised this non-API Gateway class (so not populating HTTP attrs) is mapping, does it need to?

Copy link
Author

Choose a reason for hiding this comment

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

With our wrapper requesting Object input type, AWS will (out of my testing):

  • invoke with value in case of primitive values
  • invoke with Map<String, Object> in case of custom types
    So in order to support pre-mapped custom types we need to map the Map into appropriate class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants