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

Conversation

sebsto
Copy link
Contributor

@sebsto sebsto commented Jul 21, 2025

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

@sebsto sebsto requested a review from Copilot July 21, 2025 07:03
@sebsto sebsto self-assigned this Jul 21, 2025
@sebsto sebsto added the 🆕 semver/minor Adds new public API. label Jul 21, 2025
Copy link
Contributor

@Copilot Copilot AI left a 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

Comment on lines 49 to 55
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.

[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.

Comment on lines 51 to 54
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
Contributor

@ktoso ktoso left a 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 :)

@sebsto sebsto merged commit cb85fdd into swift-server:main Jul 22, 2025
32 checks passed
@sebsto sebsto deleted the sebsto/log_event branch July 22, 2025 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[core] Make sure TRACE log level exists in runtime v2
2 participants