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

Add tracing of request events + mention LOG_LEVEL in README #315

Merged
merged 8 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/AWSLambdaRuntimeCore/LambdaRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ internal final class LambdaRunner {
allocator: self.allocator,
invocation: invocation
)
logger.trace("Request", metadata: ["bytes": .string(String(buffer: bytes))])
logger.debug("sending invocation to lambda handler")
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 we should combine those two. debug is the right level imo.
What happens with payloads that aren't string representable. IIRC I can send any payload to lambda.

Copy link
Contributor Author

@sebsto sebsto Dec 17, 2023

Choose a reason for hiding this comment

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

Thank you @fabianfett for the feedback.

I think we should combine those two. debug is the right level imo.

I like the idea of separating debug output from tracing. Trace adds more info, potentially long data trace, which is the case here.
But maybe it's a matter of personal preference.
I can see user preferences for having just the debug logs, while some others want to have the trace level for a limited period of time.
Given the potentially sensitive nature of the data contained in the payload (such as JWT tokens or PII), I think it is a good idea to decouple the debug and trace levels in this case.

What happens with payloads that aren't string representable

According to the AWS Lambda service API documentation, the payload is always a string
https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html

Furthermore, the max payload size for synchronous invocations is 6Mb. They wont be huge payloads passed as input to the swift AWS Lambda Runtime.
https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html

Copy link
Contributor

Choose a reason for hiding this comment

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

we can also do something as follow if we want to eliminate the two logging statements

if logger.logLevel <= .trace {
    logger.trace("...")
  } else {
    logger.debug("...")
  }
}

Copy link
Contributor

@tomerd tomerd Dec 20, 2023

Choose a reason for hiding this comment

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

Furthermore, the max payload size for synchronous invocations is 6Mb

perhaps we want to trim it?

return handler.handle(bytes, context: context)
// Hopping back to "our" EventLoop is important in case the handler returns a future that
Expand Down
39 changes: 39 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,45 @@ Next, create a `MyLambda.swift` and implement your Lambda. Note that the file ca

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.

## Testing locally
sebsto marked this conversation as resolved.
Show resolved Hide resolved

Before to deploy your code to AWS Lambda, you can test it locally with the command:
sebsto marked this conversation as resolved.
Show resolved Hide resolved

```sh
LOCAL_LAMBDA_SERVER_ENABLED=true swift run
```

This starts a local HTTP endpoint listening on port TCP 7000. Your can invoke your code by sending an HTTP POST command to `http://127.0.0.1:7000/invoke`.
sebsto marked this conversation as resolved.
Show resolved Hide resolved

For example:

```sh
curl -v --header "Content-Type:\ application/json" --data @events/create-session.json http://127.0.0.1:7000/invoke
sebsto marked this conversation as resolved.
Show resolved Hide resolved
* Trying 127.0.0.1:7000...
* Connected to 127.0.0.1 (127.0.0.1) port 7000
> POST /invoke HTTP/1.1
> Host: 127.0.0.1:7000
> User-Agent: curl/8.4.0
> Accept: */*
> Content-Type:\ application/json
> Content-Length: 1160
>
< HTTP/1.1 200 OK
< content-length: 247
<
* Connection #0 to host 127.0.0.1 left intact
{"statusCode":200,"isBase64Encoded":false,"body":"...","headers":{"Access-Control-Allow-Origin":"*","Content-Type":"application\/json; charset=utf-8","Access-Control-Allow-Headers":"*"}}
```

## Increase logging verbosity

You can increase the verbosity of the runtime by changing the LOG_LEVEL environment variable.
sebsto marked this conversation as resolved.
Show resolved Hide resolved

- `LOG_LEVEL=debug` displays information about the Swift AWS Lambda Runtime activity and lifecycle
- `LOG_LEVEL=trace` displays a string representation of the input event as received from the AWS Lambda service (before invoking your handler).

You can use 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. 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.
sebsto marked this conversation as resolved.
Show resolved Hide resolved

## Deploying to AWS Lambda

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.
Expand Down