Skip to content

Commit 009b5c6

Browse files
author
Sebastien Stormacq
committed
add logging trace details
1 parent 2d6842a commit 009b5c6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Sources/AWSLambdaRuntime/Lambda+LocalServer.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ internal struct LambdaHTTPServer {
125125
logger: Logger,
126126
_ closure: sending @escaping () async throws -> Result
127127
) async throws -> Result {
128+
129+
var l = Logger(label: "HTTPServer")
130+
l.logLevel = .trace
131+
let logger = l
132+
128133
let channel = try await ServerBootstrap(group: eventLoopGroup)
129134
.serverChannelOption(.backlog, value: 256)
130135
.serverChannelOption(.socketOption(.so_reuseaddr), value: 1)
@@ -165,6 +170,8 @@ internal struct LambdaHTTPServer {
165170
let closureBox = UnsafeTransferBox(value: closure)
166171
let result = await withTaskGroup(of: TaskResult<Result>.self, returning: Swift.Result<Result, any Error>.self) {
167172
group in
173+
174+
// this Task will run the content of the closure we received, typically the Lambda Runtime Client HTTP
168175
group.addTask {
169176
let c = closureBox.value
170177
do {
@@ -175,6 +182,7 @@ internal struct LambdaHTTPServer {
175182
}
176183
}
177184

185+
// this Task will create one subtask to handle each individual connection
178186
group.addTask {
179187
do {
180188
// We are handling each incoming connection in a separate child task. It is important
@@ -397,7 +405,7 @@ internal struct LambdaHTTPServer {
397405
// the `for try await ... in` loop will throw an error and we will return a 400 error to the client
398406
do {
399407
for try await response in self.responsePool {
400-
logger[metadataKey: "response requestId"] = "\(response.requestId ?? "nil")"
408+
logger[metadataKey: "response_requestId"] = "\(response.requestId ?? "nil")"
401409
logger.trace("Received response to return to client")
402410
if response.requestId == requestId {
403411
logger.trace("/invoke requestId is valid, sending the response")
@@ -426,6 +434,7 @@ internal struct LambdaHTTPServer {
426434
// This should not happen as the async iterator blocks until there is a response to process
427435
fatalError("No more responses to process - the async for loop should not return")
428436
} catch is LambdaHTTPServer.Pool<LambdaHTTPServer.LocalServerResponse>.PoolError {
437+
logger.trace("PoolError catched")
429438
// detect concurrent invocations of POST and gently decline the requests while we're processing one.
430439
let response = LocalServerResponse(
431440
id: requestId,
@@ -460,7 +469,9 @@ internal struct LambdaHTTPServer {
460469
logger[metadataKey: "requestId"] = "\(invocation.requestId)"
461470
logger.trace("/next retrieved invocation")
462471
// tell the lambda function we accepted the invocation
463-
return try await sendResponse(invocation.acceptedResponse(), outbound: outbound, logger: logger)
472+
try await sendResponse(invocation.acceptedResponse(), outbound: outbound, logger: logger)
473+
logger.trace("/next accepted, returning")
474+
return
464475
}
465476
// What todo when there is no more tasks to process?
466477
// This should not happen as the async iterator blocks until there is a task to process

0 commit comments

Comments
 (0)