-
Notifications
You must be signed in to change notification settings - Fork 114
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
Conversation
…d before being decoded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds trace-level logging functionality to display the raw payload before it's processed by the Lambda handler, porting a feature from Runtime v1 to v2. This enhancement helps debug decoding errors by showing the exact payload received.
Key changes:
- Adds conditional trace logging that shows the first 1KB of raw event payload
- Implements performance optimization by only computing metadata when trace logging is enabled
- Provides truncation indicator when payload exceeds 1KB
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 ? "..." : "")) | ||
] |
There was a problem hiding this comment.
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.
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.
let buffer = bytes.getSlice(at: 0, length: min(bytes.readableBytes, 1024)) | ||
{ | ||
metadata = [ | ||
"Event's first bytes": .string(String(buffer: buffer) + (bytes.readableBytes > 1024 ? "..." : "")) |
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks ok to me :)
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: