Repository navigation
Add ability to customize structured logging serialization options #2383
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
.autover/changes/1d2619b4-c0e7-4fcd-81a6-7ef7dcf4e2e8.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "Projects": [ | ||
| { | ||
| "Name": "Amazon.Lambda.Core", | ||
| "Type": "Minor", | ||
| "ChangelogMessages": [ | ||
| "[Preview] Add LambdaLogger.ConfigureStructuredLogging to customize the JsonSerializerOptions used for serializing logging parameters." | ||
| ] | ||
| }, | ||
| { | ||
| "Name": "Amazon.Lambda.RuntimeSupport", | ||
| "Type": "Minor", | ||
| "ChangelogMessages": [ | ||
| "Add support for handling the structured logging customization from Amazon.Lambda.Core." | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Libraries/src/Amazon.Lambda.Core/StructuredLoggingOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| #if NET8_0_OR_GREATER | ||
| using System.Text.Json; | ||
|
|
||
| namespace Amazon.Lambda.Core; | ||
|
|
||
| /// <summary> | ||
| /// The options that can be overridden for structured logging. | ||
| /// </summary> | ||
| public class StructuredLoggingOptions | ||
| { | ||
| /// <summary> | ||
| /// Override the default JsonSerializerOptions used by the Lambda runtime for serializing object parameters in structured logs. | ||
| /// </summary> | ||
| public JsonSerializerOptions OverrideSerializerOptions { get; set; } | ||
| } | ||
| #endif |
35 changes: 35 additions & 0 deletions
35
.../Amazon.Lambda.RuntimeSupport/Helpers/Logging/ConfigureJsonLogMessageFormatterIsolated.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System; | ||
| using System.Text.Json; | ||
|
|
||
| namespace Amazon.Lambda.RuntimeSupport.Helpers.Logging | ||
| { | ||
| internal class ConfigureJsonLogMessageFormatterIsolated | ||
| { | ||
| internal static void ConfigureCallbackInCore(Action<StructuredLoggingOptions> callback) | ||
| { | ||
| Amazon.Lambda.Core.LambdaLogger.SetConfigureStructuredLoggingAction((Amazon.Lambda.Core.StructuredLoggingOptions coreOptions) => | ||
| { | ||
| if (coreOptions == null) | ||
| { | ||
| callback(null); | ||
| return; | ||
| } | ||
|
|
||
| var isolatedOptions = new StructuredLoggingOptions(); | ||
| try | ||
| { | ||
| isolatedOptions.OverrideSerializerOptions = coreOptions.OverrideSerializerOptions; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| InternalLogger.GetDefaultLogger().LogDebug("Failed to configure structured logging. This generally happens when the version of Amazon.Lambda.Core is out of date. Update to latest version of Amazon.Lambda.Core: " + ex.ToString()); | ||
| } | ||
|
|
||
| callback(isolatedOptions); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
Libraries/test/Amazon.Lambda.Core.Tests/StructuredLoggingTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| using System; | ||
| using System.Reflection; | ||
| using System.Text.Json; | ||
| using Xunit; | ||
|
|
||
| using Amazon.Lambda.Core; | ||
|
|
||
| namespace Amazon.Lambda.Tests | ||
| { | ||
| public class StructuredLoggingTests | ||
| { | ||
| [Fact] | ||
| public void SetConfigureStructuredLoggingAction_CallbackReceivesOptions() | ||
| { | ||
| StructuredLoggingOptions receivedOptions = null; | ||
|
|
||
| LambdaLogger.SetConfigureStructuredLoggingAction(options => | ||
| { | ||
| receivedOptions = options; | ||
| }); | ||
|
|
||
| var expectedOptions = new StructuredLoggingOptions | ||
| { | ||
| OverrideSerializerOptions = new JsonSerializerOptions { WriteIndented = true } | ||
| }; | ||
|
|
||
| LambdaLogger.ConfigureStructuredLogging(expectedOptions); | ||
|
|
||
| Assert.NotNull(receivedOptions); | ||
| Assert.Same(expectedOptions, receivedOptions); | ||
| Assert.True(receivedOptions.OverrideSerializerOptions.WriteIndented); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ConfigureStructuredLogging_BeforeRuntimeSetsAction_OptionsFlowThroughCurrentAction() | ||
| { | ||
| StructuredLoggingOptions capturedOptions = null; | ||
| LambdaLogger.SetConfigureStructuredLoggingAction(options => | ||
| { | ||
| capturedOptions = options; | ||
| }); | ||
|
|
||
| var userOptions = new StructuredLoggingOptions | ||
| { | ||
| OverrideSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase } | ||
| }; | ||
|
|
||
| LambdaLogger.ConfigureStructuredLogging(userOptions); | ||
| Assert.Same(userOptions, capturedOptions); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetConfigureStructuredLoggingAction_PendingOptionsForwardedToNewAction() | ||
| { | ||
| // Reset _placeHolderStructuredLoggingOptions and _configureStructuredLoggingAction to initial state | ||
| // so we can test the forwarding path. | ||
| var placeholderField = typeof(LambdaLogger).GetField("_placeHolderStructuredLoggingOptions", BindingFlags.NonPublic | BindingFlags.Static); | ||
| var actionField = typeof(LambdaLogger).GetField("_configureStructuredLoggingAction", BindingFlags.NonPublic | BindingFlags.Static); | ||
|
|
||
| // Reset to initial state: action stores to placeholder, placeholder is null | ||
| placeholderField.SetValue(null, null); | ||
| actionField.SetValue(null, new Action<StructuredLoggingOptions>(options => | ||
| { | ||
| placeholderField.SetValue(null, options); | ||
| })); | ||
|
|
||
| // Step 1: User calls ConfigureStructuredLogging before runtime is ready. | ||
| // This stores the options in _placeHolderStructuredLoggingOptions. | ||
| var userOptions = new StructuredLoggingOptions | ||
| { | ||
| OverrideSerializerOptions = new JsonSerializerOptions { WriteIndented = true } | ||
| }; | ||
| LambdaLogger.ConfigureStructuredLogging(userOptions); | ||
|
|
||
| Assert.Same(userOptions, placeholderField.GetValue(null)); | ||
|
|
||
| // Step 2: Runtime calls SetConfigureStructuredLoggingAction. | ||
| // The pending options should be forwarded to the new action. | ||
| StructuredLoggingOptions forwardedOptions = null; | ||
| LambdaLogger.SetConfigureStructuredLoggingAction(options => | ||
| { | ||
| forwardedOptions = options; | ||
| }); | ||
|
|
||
| Assert.NotNull(forwardedOptions); | ||
| Assert.Same(userOptions, forwardedOptions); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StructuredLoggingOptions_DefaultsToNull() | ||
| { | ||
| var options = new StructuredLoggingOptions(); | ||
| Assert.Null(options.OverrideSerializerOptions); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.