Skip to content

fix [core] trace level shows the first kb of the payload before being decoded #534

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 6 commits into from
Jul 22, 2025
Merged
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
15 changes: 15 additions & 0 deletions Sources/AWSLambdaRuntime/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ public enum Lambda {
let (invocation, writer) = try await runtimeClient.nextInvocation()
logger[metadataKey: "aws-request-id"] = "\(invocation.metadata.requestID)"

// when log level is trace or lower, print the first Kb of the payload
let bytes = invocation.event
var metadata: Logger.Metadata? = nil
if logger.logLevel <= .trace,
let buffer = bytes.getSlice(at: 0, length: min(bytes.readableBytes, 1024))
{
metadata = [
"Event's first bytes": .string(String(buffer: buffer) + (bytes.readableBytes > 1024 ? "..." : ""))
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

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

The magic number 1024 appears again here. This should use the same named constant suggested for line 51 to ensure consistency and easier maintenance.

Suggested change
let buffer = bytes.getSlice(at: 0, length: min(bytes.readableBytes, 1024))
{
metadata = [
"Event's first bytes": .string(String(buffer: buffer) + (bytes.readableBytes > 1024 ? "..." : ""))
let buffer = bytes.getSlice(at: 0, length: min(bytes.readableBytes, MAX_PREVIEW_BYTES))
{
metadata = [
"Event's first bytes": .string(String(buffer: buffer) + (bytes.readableBytes > MAX_PREVIEW_BYTES ? "..." : ""))

Copilot uses AI. Check for mistakes.

]
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

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

[nitpick] Initialize metadata as nil and then conditionally assign it can be simplified. Consider declaring it as an optional and assigning it directly within the if block, or initialize it as an empty dictionary and conditionally populate it.

Suggested change
var metadata: Logger.Metadata? = nil
if logger.logLevel <= .trace,
let buffer = bytes.getSlice(at: 0, length: min(bytes.readableBytes, 1024))
{
metadata = [
"Event's first bytes": .string(String(buffer: buffer) + (bytes.readableBytes > 1024 ? "..." : ""))
]
if logger.logLevel <= .trace,
let buffer = bytes.getSlice(at: 0, length: min(bytes.readableBytes, 1024))
{
let metadata: Logger.Metadata = [
"Event's first bytes": .string(String(buffer: buffer) + (bytes.readableBytes > 1024 ? "..." : ""))
]
logger.trace(
"Sending invocation event to lambda handler",
metadata: metadata
)
} else {
logger.trace("Sending invocation event to lambda handler")

Copilot uses AI. Check for mistakes.

}
logger.trace(
"Sending invocation event to lambda handler",
metadata: metadata
)

do {
try await handler.handle(
invocation.event,
Expand Down