-
Notifications
You must be signed in to change notification settings - Fork 90
Bug: v2 StructuredArguments allow logging of reserved keys #1759
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
Comments
TypeScript opted for something similar (aws-powertools/powertools-lambda-typescript#3217). They prevent overwriting of reserved keys and log a warning each time a user attempts to do so. I will do something similar here. Related PR in TypeScript: aws-powertools/powertools-lambda-typescript#3553. |
For Log4j we need to update the There is already a safeguard in place for MDC context keys, however it does not work as intended: Lines 183 to 188 in 7b18029
While this will avoid appending the reserved key a second time, the regular field resolver will still run and set the overwritten value. Idea 1:
Idea 2: We could track MDC context changes on reserved keys only and block changes coming from customer source code. However, this seems overengineered for the problem at hand and might impact logging performance and memory usage to do such a complicated change. Idea 3: Whenever we call As the title of the issue says, I will focus on blocking reserved key usage on |
I created a PR to prevent overwriting of reserved keys via structured arguments. I also added a warning in the documentation for this. This PR also fixes a bug in the serialization of PR: #1822 The output is as expected now: @Logging(clearState = true)
public String handleRequest(final Object input, final Context context) {
LOGGER.info("My message.", StructuredArguments.entry("message", "My duplicated message."),
StructuredArguments.entry("message2", "My second message."),
StructuredArguments.entry("service", "My duplicated service."),
StructuredArguments.entry("function_name", "My duplicated function_name"));
return "OK";
} yields {
"level": "INFO",
"message": "My message.",
"cold_start": true,
"function_arn": "arn:aws:lambda:us-east-1:012345678912:function:HelloWorldFunction",
"function_memory_size": 512,
"function_name": "HelloWorldFunction",
"function_request_id": "0fbef3a7-6b9a-4203-af44-e4e35ff6ca99",
"function_version": "$LATEST",
"service": "service_undefined",
"timestamp": "2025-04-23T14:19:16.552Z",
"message2": "My second message."
} I decided to not log a warning for each log emitted because it felt like too much spam and might drive up CloudWatch costs for users who do not notice the warning. |
Resolved by PR #1822. Please see this PR message for details about the final implementation: #1822 (comment). We opted for adding warning messages if users attempt to log a reserved key via |
What were you trying to accomplish?
The current
2.0.0-SNAPSHOT
allows logging of reserved keys via structured arguments. This can lead to bugs where users accidentally log a key that is reserved by either the Logging module itself (e.g.message
) or Powertools.Examples below are using:
Expected Behavior
Given the following code, my expected behavior is that the
message
andservice
key appended viaStructuredArguments
are ignored and a user warning is logged stating that the appended keys are reserved keys by Powertools logging.Expected output:
Current Behavior
The current behavior outputs the key twice to STDOUT. In CloudWatch, it ignores the first key which will lead to logging of the structured argument only.
Output to STDOUT (second
message
andservice
key comes fromStructuredArguments
):Output in CloudWatch (initial
message
andservice
keys are shadowed by the provided structured arguments without warning):Environment
2.0.0-SNAPSHOT
/ Java11Same behavior in
1.18.0
Same behavior is observed for the current
v1
version1.18.0
.The text was updated successfully, but these errors were encountered: