Skip to content

chore: Fix Service override on decorator #704

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void OnEntry(
_correlationIdPath = trigger.CorrelationIdPath;
_clearState = trigger.ClearState;

Logger.LoggerProvider ??= new LoggerProvider(_config, _powertoolsConfigurations, _systemWrapper);
Logger.LoggerProvider = new LoggerProvider(_config, _powertoolsConfigurations, _systemWrapper);

if (!_initializeContext)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,72 @@ public void Should_Log_When_Not_Using_Decorator()
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"service_undefined\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"test\"}"))
);
}

public void Dispose()
{
Environment.SetEnvironmentVariable("POWERTOOLS_LOGGER_CASE", "");
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "");
LoggingAspect.ResetForTest();
PowertoolsLoggingSerializer.ClearOptions();
}
}

[Collection("A Sequential")]
public class ServiceTests : IDisposable
{
private readonly TestServiceHandler _testHandler;

public ServiceTests()
{
_testHandler = new TestServiceHandler();
}

[Fact]
public void When_Setting_Service_Should_Override_Env()
{
// Arrange
var consoleOut = Substitute.For<StringWriter>();
SystemWrapper.Instance.SetOut(consoleOut);

// Act
_testHandler.LogWithEnv();
_testHandler.Handler();

// Assert

consoleOut.Received(1).WriteLine(
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"Environment Service\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"Service: Environment Service\""))
);
consoleOut.Received(1).WriteLine(
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"Attribute Service\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"Service: Attribute Service\""))
);
}

[Fact]
public void When_Setting_Service_Should_Override_Env_And_Empty()
{
// Arrange
var consoleOut = Substitute.For<StringWriter>();
SystemWrapper.Instance.SetOut(consoleOut);

// Act
_testHandler.LogWithAndWithoutEnv();
_testHandler.Handler();

// Assert

consoleOut.Received(2).WriteLine(
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"service_undefined\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"Service: service_undefined\""))
);
consoleOut.Received(1).WriteLine(
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"Attribute Service\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"Service: Attribute Service\""))
);
}

public void Dispose()
{
Environment.SetEnvironmentVariable("POWERTOOLS_LOGGER_CASE", "");
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "");
LoggingAspect.ResetForTest();
PowertoolsLoggingSerializer.ClearOptions();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* permissions and limitations under the License.
*/

using System;
using System.Text.Json.Serialization;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.ApplicationLoadBalancerEvents;
Expand Down Expand Up @@ -40,12 +41,13 @@ public void TestMethodDebug()
public void LogEventNoArgs()
{
}

[Logging(LogEvent = true, LoggerOutputCase = LoggerOutputCase.PascalCase, CorrelationIdPath = "/Headers/MyRequestIdHeader")]

[Logging(LogEvent = true, LoggerOutputCase = LoggerOutputCase.PascalCase,
CorrelationIdPath = "/Headers/MyRequestIdHeader")]
public void LogEvent(TestObject testObject, ILambdaContext context)
{
}

[Logging(LogEvent = false)]
public void LogEventFalse(ILambdaContext context)
{
Expand Down Expand Up @@ -81,104 +83,129 @@ public void CorrelationCloudWatchEvent(CloudWatchEvent<S3ObjectCreate> cwEvent)
public void CorrelationIdFromString(TestObject testObject)
{
}

[Logging(CorrelationIdPath = "/headers/my_request_id_header")]
public void CorrelationIdFromStringSnake(TestObject testObject)
{
}

[Logging(CorrelationIdPath = "/Headers/MyRequestIdHeader", LoggerOutputCase = LoggerOutputCase.PascalCase)]
public void CorrelationIdFromStringPascal(TestObject testObject)
{
}

[Logging(CorrelationIdPath = "/headers/myRequestIdHeader", LoggerOutputCase = LoggerOutputCase.CamelCase)]
public void CorrelationIdFromStringCamel(TestObject testObject)
{
}

[Logging(CorrelationIdPath = "/headers/my_request_id_header")]
public void CorrelationIdFromStringSnakeEnv(TestObject testObject)
{
}

[Logging(CorrelationIdPath = "/Headers/MyRequestIdHeader")]
public void CorrelationIdFromStringPascalEnv(TestObject testObject)
{
}

[Logging(CorrelationIdPath = "/headers/myRequestIdHeader")]
public void CorrelationIdFromStringCamelEnv(TestObject testObject)
{
}

[Logging(Service = "test", LoggerOutputCase = LoggerOutputCase.CamelCase)]
public void HandlerService()
{
Logger.LogInformation("test");
}

[Logging(SamplingRate = 0.5, LoggerOutputCase = LoggerOutputCase.CamelCase, LogLevel = LogLevel.Information)]
public void HandlerSamplingRate()
{
Logger.LogInformation("test");
}

[Logging(LogLevel = LogLevel.Critical)]
public void TestLogLevelCritical()
{
Logger.LogCritical("test");
}

[Logging(LogLevel = LogLevel.Critical, LogEvent = true)]
public void TestLogLevelCriticalLogEvent(ILambdaContext context)
{
}

[Logging(LogLevel = LogLevel.Debug, LogEvent = true)]
public void TestLogEventWithoutContext()
{
}

[Logging(LogEvent = true, SamplingRate = 0.2, Service = "my_service")]
public void TestCustomFormatterWithDecorator(string input, ILambdaContext context)
{
}

[Logging(LogEvent = true, SamplingRate = 0.2, Service = "my_service")]
public void TestCustomFormatterWithDecoratorNoContext(string input)
{
}

public void TestCustomFormatterNoDecorator(string input, ILambdaContext context)
{
Logger.LogInformation(input);
}

public void TestLogNoDecorator()
{
Logger.LogInformation("test");
}

[Logging(Service = "test", LoggerOutputCase = LoggerOutputCase.SnakeCase)]
public void TestEnums(string input, ILambdaContext context)
{
Logger.LogInformation(Pet.Dog);
Logger.LogInformation(Thing.Five);
}

public enum Thing
{
One = 1,
Three = 3,
Five = 5
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum Pet
{
Cat = 1,
Dog = 3,
Lizard = 5
}
}

public class TestServiceHandler
{
public void LogWithEnv()
{
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "Environment Service");

Logger.LogInformation("Service: Environment Service");
}

public void LogWithAndWithoutEnv()
{
Logger.LogInformation("Service: service_undefined");

Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "Environment Service");

Logger.LogInformation("Service: service_undefined");
}

[Logging(Service = "Attribute Service")]
public void Handler()
{
Logger.LogInformation("Service: Attribute Service");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace AWS.Lambda.Powertools.Logging.Tests
{
[Collection("Sequential")]
[Collection("X Sequential")]
public class PowertoolsLoggerTest : IDisposable
{
public PowertoolsLoggerTest()
Expand Down Expand Up @@ -1337,6 +1337,8 @@ public void Log_WhenMemoryStream_LogsBase64String_UnsafeRelaxedJsonEscaping()
[Fact]
public void Log_Set_Execution_Environment_Context()
{
var _originalValue = Environment.GetEnvironmentVariable("POWERTOOLS_SERVICE_NAME");

// Arrange
var loggerName = Guid.NewGuid().ToString();
var assemblyName = "AWS.Lambda.Powertools.Logger";
Expand Down Expand Up @@ -1707,6 +1709,7 @@ public void Log_Should_Use_Powertools_Log_Level_When_Set(bool willLog, LogLevel
public void Dispose()
{
PowertoolsLoggingSerializer.ClearOptions();
LoggingAspect.ResetForTest();
}
}
}
17 changes: 16 additions & 1 deletion libraries/tests/AWS.Lambda.Powertools.Logging.Tests/TestSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
* permissions and limitations under the License.
*/

using System.Collections.Generic;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

[assembly: CollectionBehavior(DisableTestParallelization = true)]
[assembly: TestCollectionOrderer("AWS.Lambda.Powertools.Logging.Tests.DisplayNameOrderer", "AWS.Lambda.Powertools.Logging.Tests")]
[assembly: CollectionBehavior(DisableTestParallelization = true)]


namespace AWS.Lambda.Powertools.Logging.Tests;

public class DisplayNameOrderer : ITestCollectionOrderer
{
public IEnumerable<ITestCollection> OrderTestCollections(IEnumerable<ITestCollection> testCollections)
{
return testCollections.OrderBy(collection => collection.DisplayName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FunctionTests(ITestOutputHelper testOutputHelper)
[Trait("Category", "AOT")]
[Theory]
[InlineData("E2ETestLambda_X64_AOT_NET8_logging")]
// [InlineData("E2ETestLambda_ARM_AOT_NET8_metrics")]
[InlineData("E2ETestLambda_ARM_AOT_NET8_logging")]
public async Task AotFunctionTest(string functionName)
{
await TestFunction(functionName);
Expand Down
Loading