-
Notifications
You must be signed in to change notification settings - Fork 113
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
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2841197
allow the users of client to provide headers
Ro-M 4162c6b
add initialization error header
Ro-M a207384
add error header to failed invocations
Ro-M 03c33da
add error header to failed invocations
Ro-M d75e2f2
enable external setting of ip and port of RuntimeEngine
Ro-M 5c81588
test headers in failed initialization
Ro-M 8da17fd
Merge branch 'rm-bug116' of github.com:Ro-M/swift-aws-lambda-runtime …
Ro-M 1e57c4b
use baseUrl instead of ip in RuntieEngine
Ro-M f8da566
refactor test
Ro-M 72c2b8e
invocation error test
Ro-M d0b3712
remove header change from get method
Ro-M 196b335
test that previous headers are present
Ro-M 1ccd68e
check success case does not have error headers
Ro-M c520b22
swiftformat
Ro-M f5bcbd7
Merge branch 'master' into rm-bug116
Ro-M e5dd619
removed unneeded @testable
Ro-M 5f5f09c
Merge branch 'rm-bug116' of github.com:Ro-M/swift-aws-lambda-runtime …
Ro-M eabbc10
Merge branch 'master' into rm-bug116
tomerd c375ab6
Merge branch 'master' into rm-bug116
tomerd 84e19d2
rename test method
Ro-M 60c4dc4
rename runtimeEngines baseUrl to address
Ro-M 24c9d1b
move headers from HTTPClient into RuntimeClient
Ro-M d8a18e1
run sanity script
Ro-M File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ import Logging | |
import NIO | ||
import NIOHTTP1 | ||
|
||
|
||
|
||
/// An HTTP based client for AWS Runtime Engine. This encapsulates the RESTful methods exposed by the Runtime Engine: | ||
/// * /runtime/invocation/next | ||
/// * /runtime/invocation/response | ||
|
@@ -26,7 +28,7 @@ extension Lambda { | |
private let eventLoop: EventLoop | ||
private let allocator = ByteBufferAllocator() | ||
private let httpClient: HTTPClient | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run ./scripts/sanity |
||
init(eventLoop: EventLoop, configuration: Configuration.RuntimeEngine) { | ||
self.eventLoop = eventLoop | ||
self.httpClient = HTTPClient(eventLoop: eventLoop, configuration: configuration) | ||
|
@@ -36,7 +38,7 @@ extension Lambda { | |
func getNextInvocation(logger: Logger) -> EventLoopFuture<(Invocation, ByteBuffer)> { | ||
let url = Consts.invocationURLPrefix + Consts.getNextInvocationURLSuffix | ||
logger.debug("requesting work from lambda runtime engine using \(url)") | ||
return self.httpClient.get(url: url).flatMapThrowing { response in | ||
return self.httpClient.get(url: url, headers: RuntimeClient.defaultHeaders).flatMapThrowing { response in | ||
guard response.status == .ok else { | ||
throw RuntimeError.badStatusCode(response.status) | ||
} | ||
|
@@ -61,19 +63,23 @@ extension Lambda { | |
func reportResults(logger: Logger, invocation: Invocation, result: Result<ByteBuffer?, Error>) -> EventLoopFuture<Void> { | ||
var url = Consts.invocationURLPrefix + "/" + invocation.requestID | ||
var body: ByteBuffer? | ||
let headers: HTTPHeaders | ||
|
||
switch result { | ||
case .success(let buffer): | ||
url += Consts.postResponseURLSuffix | ||
body = buffer | ||
headers = RuntimeClient.defaultHeaders | ||
case .failure(let error): | ||
url += Consts.postErrorURLSuffix | ||
let errorResponse = ErrorResponse(errorType: Consts.functionError, errorMessage: "\(error)") | ||
let bytes = errorResponse.toJSONBytes() | ||
body = self.allocator.buffer(capacity: bytes.count) | ||
body!.writeBytes(bytes) | ||
headers = RuntimeClient.errorHeaders | ||
} | ||
logger.debug("reporting results to lambda runtime engine using \(url)") | ||
return self.httpClient.post(url: url, body: body).flatMapThrowing { response in | ||
return self.httpClient.post(url: url, headers: headers, body: body).flatMapThrowing { response in | ||
guard response.status == .accepted else { | ||
throw RuntimeError.badStatusCode(response.status) | ||
} | ||
|
@@ -98,7 +104,7 @@ extension Lambda { | |
var body = self.allocator.buffer(capacity: bytes.count) | ||
body.writeBytes(bytes) | ||
logger.warning("reporting initialization error to lambda runtime engine using \(url)") | ||
return self.httpClient.post(url: url, body: body).flatMapThrowing { response in | ||
return self.httpClient.post(url: url, headers: RuntimeClient.errorHeaders, body: body).flatMapThrowing { response in | ||
guard response.status == .accepted else { | ||
throw RuntimeError.badStatusCode(response.status) | ||
} | ||
|
@@ -186,3 +192,15 @@ extension Lambda { | |
} | ||
} | ||
} | ||
|
||
extension Lambda.RuntimeClient { | ||
|
||
internal static let defaultHeaders = HTTPHeaders([("user-agent", "Swift-Lambda/Unknown")]) | ||
|
||
/// These headers must be sent along an invocation or initialization error report | ||
internal static let errorHeaders = HTTPHeaders([ | ||
("user-agent", "Swift-Lambda/Unknown"), | ||
("lambda-runtime-function-error-type", "Unhandled"), | ||
]) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove empty lines