Skip to content

Commit cb85fdd

Browse files
sebstoCopilot
andauthored
fix [core] trace level shows the first kb of the payload before being decoded (#534)
Add a log statement in the Lambda loop, before calling the user's handler to show the raw payload before any attempt to decode it. This was available in Runtime v1 and is now ported to v2. This fixes #404 ### Motivation: This is useful when handling custom event and there is a Decoding error. It allows to see the exact payload received by the handler before any attempt to decode it. ### Modifications: Add a log.trace statement with metatadata. Metadata are computed only when the log level is trace or below. ### Result: ``` 2025-07-21T08:58:33+0200 trace LambdaRuntime : Event's first bytes={"name": "me", "age": 50} aws-request-id=769127502334125 [AWSLambdaRuntime] sending invocation event to lambda handler ``` --------- Co-authored-by: Copilot <[email protected]>
1 parent db7e789 commit cb85fdd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sources/AWSLambdaRuntime/Lambda.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ public enum Lambda {
4444
let (invocation, writer) = try await runtimeClient.nextInvocation()
4545
logger[metadataKey: "aws-request-id"] = "\(invocation.metadata.requestID)"
4646

47+
// when log level is trace or lower, print the first Kb of the payload
48+
let bytes = invocation.event
49+
let maxPayloadPreviewSize = 1024
50+
var metadata: Logger.Metadata? = nil
51+
if logger.logLevel <= .trace,
52+
let buffer = bytes.getSlice(at: 0, length: min(bytes.readableBytes, maxPayloadPreviewSize))
53+
{
54+
metadata = [
55+
"Event's first bytes": .string(
56+
String(buffer: buffer) + (bytes.readableBytes > maxPayloadPreviewSize ? "..." : "")
57+
)
58+
]
59+
}
60+
logger.trace(
61+
"Sending invocation event to lambda handler",
62+
metadata: metadata
63+
)
64+
4765
do {
4866
try await handler.handle(
4967
invocation.event,

0 commit comments

Comments
 (0)