Skip to content

Added header when reporting failing invocations or initializations #116 #128

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

Merged
merged 23 commits into from
Jun 17, 2020

Conversation

Ro-M
Copy link
Contributor

@Ro-M Ro-M commented Jun 13, 2020

Motivation

This fixes #116

We need to send a special header when we are reporting a failing invocation or when the runtime encounters an error during initialization.

Changes

  • The lambda-runtime-function-error-type header is added by the RuntimeClient. As of my understanding this should not be done by the HTTPClient
  • To allow this I added the option to provide additional headers in the post method of HTTPClient. Those headers will be added to the default headers of the HTTPClient
  • For testing purposes I opted into using the NIOHTTP1TestServer instead of the MockLambdaServer.
  • I am using RuntimeEngine's baseURL parameter as the primary source to set the ip and port property, falling back to searching in the environment if it wasn't provided. baseURL did exist as a parameter before but was unused. I needed a settable baseURL for the NIOHTTP1TestServer

Discussion

  • If we are adding an additionalHeaders parameter to the post method of the HTTPClient, should this parameter also be available in the get method? (It's not needed needed for this PR)
  • If using the NIOHTTP1TestServer is ok but evaluating the inbound reads and outbound writes is too much boilerplate code: Should this evaluation be refactored into helper methods? (similar helper methods are internally used in the swift-nio package)
  • Concerning the baseURL parameter of RuntimeEngine's initializer: Directly mapping a url to the ip and port property does not sound right: I would not expect an ip property to contain a url. Maybe rename ip to host?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

3 similar comments
@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@fabianfett
Copy link
Member

@swift-server-bot test this please

Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

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

@Ro-M Thanks for this great pr. Have you tested this by any chance on Lambda itself?

I think we need to clarify the HTTPClients API first. Should the HTTPClient have static headers it merges with injected once or should the Lambda.RuntimeClient inject all headers on every call.

I think I would vote for the later but let's wait what @tomerd thinks.

Comment on lines 46 to 48
func post(url: String, body: ByteBuffer?, timeout: TimeAmount? = nil, additionalHeaders: HTTPHeaders? = nil) -> EventLoopFuture<Response> {
var headers = HTTPClient.headers
additionalHeaders.flatMap { headers.add(contentsOf: $0) }
Copy link
Member

Choose a reason for hiding this comment

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

@tomerd I would vote to inject all headers on every call and to not have them split over two files. wdyt? In hindsight having static headers directly on the HTTPClient doesn't make much sense IMHO.

Copy link
Contributor

@tomerd tomerd Jun 16, 2020

Choose a reason for hiding this comment

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

hi @fabianfett do you mean have RuntimeClient fully control the headers instead of injecting "additional" ones? if so, that sounds good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be ok to include that change in this PR or should I make a separate one for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest we fix it in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I moved the static header declaration into an extension on RuntimeClient. The get and post methods on HTTPClient now have a non-optional headers parameter (without a default value). I also moved the headers parameter to come after the urlparameter ... that ordering made a more cohesive impression to me ^^

XCTAssertNoThrow(try result.wait())
}

func testSuccessHeaders() {
Copy link
Member

Choose a reason for hiding this comment

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

I think I would rename this to testInvocationResponse because it isn't as generic as SuccessHeaders

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. I think Headers is generally wrong at this point because the method is also testing a lot of other things as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is testInvocationSuccessResponseok with you as well? To disambiguate between success and error tests

Copy link
Member

Choose a reason for hiding this comment

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

@Ro-M yes testInvocationSuccessResponse is perfect

@fabianfett fabianfett requested a review from tomerd June 13, 2020 22:27
@tomerd
Copy link
Contributor

tomerd commented Jun 16, 2020

@swift-server-bot test this please

@tomerd
Copy link
Contributor

tomerd commented Jun 16, 2020

Concerning the baseURL parameter of RuntimeEngine's initializer: Directly mapping a url to the ip and port property does not sound right: I would not expect an ip property to contain a url. Maybe rename ip to host?

happy to explore other names but keep in mind that Lambda will always bind to a localhost IP address, and AWS provides that as an env variables that combines the two as parsed by the code

@tomerd tomerd added this to the 0.1.1 milestone Jun 16, 2020
@Ro-M
Copy link
Contributor Author

Ro-M commented Jun 16, 2020

Lambda will always bind to a localhost IP address

@tomerd I didn't think about that, thanks!
So ip and port seem to definitely be the correct names in that scenario and baseURL should be removed I guess?
If we keep ip and port, do we want to only set them through environment variables or do we want to provide a parameter (or multiple) to optionally set those properties via an initializer instead of the environment?

I would prefer the latter approach: providing the option to set them through an initializer but falling back to the environment and also default the ip and port parameters to nil. But on the other hand I can see the reasoning behind only using the environment if AWS is doing it that way ...

@tomerd
Copy link
Contributor

tomerd commented Jun 16, 2020

If we keep ip and port, do we want to only set them through environment variables or do we want to provide a parameter (or multiple) to optionally set those properties via an initializer instead of the environment?

I would prefer the latter approach: providing the option to set them through an initializer but falling back to the environment and also default the ip and port parameters to nil. But on the other hand I can see the reasoning behind only using the environment if AWS is doing it that way ...

using only env variables makes testing hard, so I also prefer the approach of being able to set it via the config and falling back to the env variable

name wise, maybe "address"?

@tomerd
Copy link
Contributor

tomerd commented Jun 16, 2020

@swift-server-bot test this please

@tomerd tomerd self-assigned this Jun 16, 2020
@@ -16,6 +16,8 @@ import Logging
import NIO
import NIOHTTP1



Copy link
Contributor

Choose a reason for hiding this comment

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

remove empty lines

@@ -26,7 +28,7 @@ extension Lambda {
private let eventLoop: EventLoop
private let allocator = ByteBufferAllocator()
private let httpClient: HTTPClient

Copy link
Contributor

Choose a reason for hiding this comment

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

run ./scripts/sanity

@fabianfett
Copy link
Member

@swift-server-bot test this please

Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

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

Thanks @Ro-M for contributing. This looks really good now!

@tomerd tomerd merged commit 7728066 into swift-server:master Jun 17, 2020
@tomerd
Copy link
Contributor

tomerd commented Jun 17, 2020

thanks @Ro-M

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.

Lambda-Runtime-Function-Error-Type header missing when reporting errors.
4 participants