Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .autover/changes/1d2619b4-c0e7-4fcd-81a6-7ef7dcf4e2e8.json
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."
]
}
]
}
27 changes: 27 additions & 0 deletions Libraries/src/Amazon.Lambda.Core/LambdaLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@ public static void Log(string level, Exception exception, string message, params
/// <param name="message">Message to log. The message may have format arguments.</param>
/// <param name="args">Arguments to format the message with.</param>
public static void Log(LogLevel level, Exception exception, string message, params object[] args) => Log(level.ToString(), exception, message, args);

// This field must not be readonly because SetConfigureStructuredLoggingAction replaces the value
// when Amazon.Lambda.RuntimeSupport registers its structured logging callback.
private static Action<StructuredLoggingOptions> _configureStructuredLoggingAction = (options) => _placeHolderStructuredLoggingOptions = options;

// Because a user might call ConfigureStructuredLogging before the Lambda runtime has a chance to replace the _configureStructuredLoggingAction with the
// real implementation, we need to hold onto the options they provided until the Lambda runtime can use them to configure structured logging.
private static StructuredLoggingOptions _placeHolderStructuredLoggingOptions;

/// <summary>
/// When structured logging is enabled this method will allow overriding the default configuration the Lambda runtime uses for structured logging.
/// </summary>
/// <param name="options">The options to use for configuring structured logging.</param>
[RequiresPreviewFeatures("This method is in preview until the latest changes of the .NET Lambda runtime client have been deployed to the Lambda managed runtimes")]
public static void ConfigureStructuredLogging(StructuredLoggingOptions options) => _configureStructuredLoggingAction(options);

internal static void SetConfigureStructuredLoggingAction(Action<StructuredLoggingOptions> configureStructuredLoggingAction)
{
_configureStructuredLoggingAction = configureStructuredLoggingAction;

// If a user set the structured logging options before the Lambda runtime set the real _configureStructuredLoggingAction,
// we need to call it now to make sure the user's options are applied.
if (_placeHolderStructuredLoggingOptions != null)
{
_configureStructuredLoggingAction(_placeHolderStructuredLoggingOptions);
}
}
#endif
}
}
18 changes: 18 additions & 0 deletions Libraries/src/Amazon.Lambda.Core/StructuredLoggingOptions.cs
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
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());
}
Comment thread
normj marked this conversation as resolved.
Resolved

callback(isolatedOptions);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class JsonLogMessageFormatter : AbstractLogMessageFormatter
private static readonly UTF8Encoding UTF8NoBomNoThrow = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: false);

// Options used when serializing any message property values as a JSON to be added to the structured log message.
private readonly JsonSerializerOptions _jsonSerializationOptions;
private JsonSerializerOptions _jsonSerializationOptions;

/// <summary>
/// Constructs an instance of JsonLogMessageFormatter.
Expand All @@ -30,10 +30,28 @@ public JsonLogMessageFormatter()
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = false
};

try
{
ConfigureJsonLogMessageFormatterIsolated.ConfigureCallbackInCore(ConfigureStructuredLogging);
}
catch (TypeLoadException)
{
InternalLogger.GetDefaultLogger().LogDebug("Failed to configure Amazon.Lambda.Core with callback for configuring structured logging. This happens when the version of Amazon.Lambda.Core referenced by the Lambda function is out of date.");
}
}

private static readonly IReadOnlyList<MessageProperty> _emptyMessageProperties = new List<MessageProperty>();

private void ConfigureStructuredLogging(StructuredLoggingOptions options)
{
if (options == null)
return;

if (options.OverrideSerializerOptions != null)
_jsonSerializationOptions = options.OverrideSerializerOptions;
}

/// <summary>
/// Format the log message as a structured JSON log message.
/// </summary>
Expand Down Expand Up @@ -313,4 +331,13 @@ private void FormatJsonValue(Utf8JsonWriter writer, object value, string formatA

private static string ToInvariantString(object obj) => Convert.ToString(obj, CultureInfo.InvariantCulture);
}

/// <summary>
/// Mirror the version of StructuredLoggingOptions in Amazon.Lambda.Core because we can't guarantee that the version of Amazon.Lambda.Core used by the customer
/// will be the same as the version of Amazon.Lambda.RuntimeSupport.
/// </summary>
internal class StructuredLoggingOptions
{
public JsonSerializerOptions OverrideSerializerOptions { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2336,6 +2336,7 @@ private static DiagnosticResult[] GetExpectedRuntimeSupportDiagnostics()
DiagnosticResult.CompilerError("CS0117").WithSpan(snapFile, 13, 34, 13, 71).WithArguments("Amazon.Lambda.Core.SnapshotRestore", "CopyBeforeSnapshotCallbacksToRegistry"),
DiagnosticResult.CompilerError("CS0117").WithSpan(snapFile, 14, 34, 14, 69).WithArguments("Amazon.Lambda.Core.SnapshotRestore", "CopyAfterRestoreCallbacksToRegistry"),
DiagnosticResult.CompilerError("CS0122").WithSpan($"Amazon.Lambda.RuntimeSupport{Path.DirectorySeparatorChar}Bootstrap{Path.DirectorySeparatorChar}ResponseStreaming{Path.DirectorySeparatorChar}ResponseStreamLambdaCoreInitializerIsolated.cs", 37, 51, 37, 72).WithArguments("Amazon.Lambda.Core.ResponseStreaming.ILambdaResponseStream"),
DiagnosticResult.CompilerError("CS0117").WithSpan($"Amazon.Lambda.RuntimeSupport{Path.DirectorySeparatorChar}Helpers{Path.DirectorySeparatorChar}Logging{Path.DirectorySeparatorChar}ConfigureJsonLogMessageFormatterIsolated.cs", 13, 45, 13, 80).WithArguments("Amazon.Lambda.Core.LambdaLogger", "SetConfigureStructuredLoggingAction"),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AssemblyOriginatorKeyFile>..\..\..\buildtools\public.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<!-- CA2252: Tests call preview features (ConfigureStructuredLogging) which is expected -->
<NoWarn>CA2252</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
96 changes: 96 additions & 0 deletions Libraries/test/Amazon.Lambda.Core.Tests/StructuredLoggingTests.cs
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<AssemblyOriginatorKeyFile>..\..\..\..\buildtools\public.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<!-- This is turning off the warning for unused parameters in method signatures. There are tests that we need to have parameters in the signature for testing purposes. -->
<NoWarn>IDE0060</NoWarn>
<!-- CA2252: Tests call preview features (ConfigureStructuredLogging) which is expected -->
<NoWarn>IDE0060;CA2252</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading