Skip to content

Commit 2219782

Browse files
sebstoMahdiBM
andauthored
Add tracing of request events + mention LOG_LEVEL in README (#315)
* add tracing of request events + mention LOG_LEVEL in README * fix typo Co-authored-by: Mahdi Bahrami <[email protected]> * clarify the use of env variable and the swift cli Co-authored-by: Mahdi Bahrami <[email protected]> * clarify language about HTTP server Co-authored-by: Mahdi Bahrami <[email protected]> * formatting Co-authored-by: Mahdi Bahrami <[email protected]> * factor in additional suggestions from @MahdiBM * combine two logger statement into one and print only for Kb of payload --------- Co-authored-by: Mahdi Bahrami <[email protected]>
1 parent e6a59c2 commit 2219782

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Sources/AWSLambdaRuntimeCore/LambdaRunner.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ internal final class LambdaRunner {
7878
allocator: self.allocator,
7979
invocation: invocation
8080
)
81-
logger.debug("sending invocation to lambda handler")
81+
// when log level is trace or lower, print the first Kb of the payload
82+
if logger.logLevel <= .trace, let buffer = bytes.getSlice(at: 0, length: max(bytes.readableBytes, 1024)) {
83+
logger.trace("sending invocation to lambda handler",
84+
metadata: ["1024 first bytes": .string(String(buffer: buffer))])
85+
} else {
86+
logger.debug("sending invocation to lambda handler")
87+
}
8288
return handler.handle(bytes, context: context)
8389
// Hopping back to "our" EventLoop is important in case the handler returns a future that
8490
// originated from a foreign EventLoop/EventLoopGroup.

readme.md

+44
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,50 @@ Next, create a `MyLambda.swift` and implement your Lambda. Note that the file ca
174174

175175
Beyond the small cognitive complexity of using the `EventLoopFuture` based APIs, note these APIs should be used with extra care. An `EventLoopLambdaHandler` will execute the user code on the same `EventLoop` (thread) as the library, making processing faster but requiring the user code to never call blocking APIs as it might prevent the underlying process from functioning.
176176

177+
## Testing Locally
178+
179+
Before deploying your code to AWS Lambda, you can test it locally by setting the `LOCAL_LAMBDA_SERVER_ENABLED` environment variable to true. It will look like this on CLI:
180+
181+
```sh
182+
LOCAL_LAMBDA_SERVER_ENABLED=true swift run
183+
```
184+
185+
This starts a local HTTP server listening on port 7000. You can invoke your local Lambda function by sending an HTTP POST request to `http://127.0.0.1:7000/invoke`.
186+
187+
The request must include the JSON payload expected as an `Event` by your function. You can create a text file with the JSON payload documented by AWS or captured from a trace. In this example, we used [the APIGatewayv2 JSON payload from the documentation](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html#apigateway-example-event), saved as `events/create-session.json` text file.
188+
189+
Then we use curl to invoke the local endpoint with the test JSON payload.
190+
191+
```sh
192+
curl -v --header "Content-Type:\ application/json" --data @events/create-session.json http://127.0.0.1:7000/invoke
193+
* Trying 127.0.0.1:7000...
194+
* Connected to 127.0.0.1 (127.0.0.1) port 7000
195+
> POST /invoke HTTP/1.1
196+
> Host: 127.0.0.1:7000
197+
> User-Agent: curl/8.4.0
198+
> Accept: */*
199+
> Content-Type:\ application/json
200+
> Content-Length: 1160
201+
>
202+
< HTTP/1.1 200 OK
203+
< content-length: 247
204+
<
205+
* Connection #0 to host 127.0.0.1 left intact
206+
{"statusCode":200,"isBase64Encoded":false,"body":"...","headers":{"Access-Control-Allow-Origin":"*","Content-Type":"application\/json; charset=utf-8","Access-Control-Allow-Headers":"*"}}
207+
```
208+
209+
## Increase logging verbosity
210+
211+
You can increase the verbosity of the runtime using the `LOG_LEVEL` environment variable.
212+
213+
- `LOG_LEVEL=debug` displays information about the Swift AWS Lambda Runtime activity and lifecycle
214+
- `LOG_LEVEL=trace` displays a string representation of the input event as received from the AWS Lambda service (before invoking your handler).
215+
216+
You can modify the verbosity of a Lambda function by passing the LOG_LEVEL environment variable both during your local testing (LOG_LEVEL=trace LOCAL_LAMBDA_SERVER_ENABLED=true swift run) or when you deploy your code on AWS Lambda.
217+
You can [define environment variables for your Lambda functions](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html) in the AWS console or programmatically.
218+
219+
This repository follows [Swift's Log Level Guidelines](https://www.swift.org/server/guides/libraries/log-levels.html). At `LOG_LEVEL=trace`, the AWS Lambda runtime will display a string representation of the input event as received from the AWS Lambda service before invoking your handler, for maximum debuggability.
220+
177221
## Deploying to AWS Lambda
178222

179223
To deploy Lambda functions to AWS Lambda, you need to compile the code for Amazon Linux which is the OS used on AWS Lambda microVMs, package it as a Zip file, and upload to AWS.

0 commit comments

Comments
 (0)