Skip to content

Commit 84765d0

Browse files
authored
Merge pull request #704 from hjgraca/fix(logging)-service-name-override
chore: Fix Service override on decorator
2 parents d539850 + 5984626 commit 84765d0

File tree

6 files changed

+132
-25
lines changed

6 files changed

+132
-25
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Internal/LoggingAspect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void OnEntry(
138138
_correlationIdPath = trigger.CorrelationIdPath;
139139
_clearState = trigger.ClearState;
140140

141-
Logger.LoggerProvider ??= new LoggerProvider(_config, _powertoolsConfigurations, _systemWrapper);
141+
Logger.LoggerProvider = new LoggerProvider(_config, _powertoolsConfigurations, _systemWrapper);
142142

143143
if (!_initializeContext)
144144
return;

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Attributes/LoggingAttributeTest.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,72 @@ public void Should_Log_When_Not_Using_Decorator()
471471
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"service_undefined\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"test\"}"))
472472
);
473473
}
474+
475+
public void Dispose()
476+
{
477+
Environment.SetEnvironmentVariable("POWERTOOLS_LOGGER_CASE", "");
478+
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "");
479+
LoggingAspect.ResetForTest();
480+
PowertoolsLoggingSerializer.ClearOptions();
481+
}
482+
}
483+
484+
[Collection("A Sequential")]
485+
public class ServiceTests : IDisposable
486+
{
487+
private readonly TestServiceHandler _testHandler;
488+
489+
public ServiceTests()
490+
{
491+
_testHandler = new TestServiceHandler();
492+
}
493+
494+
[Fact]
495+
public void When_Setting_Service_Should_Override_Env()
496+
{
497+
// Arrange
498+
var consoleOut = Substitute.For<StringWriter>();
499+
SystemWrapper.Instance.SetOut(consoleOut);
500+
501+
// Act
502+
_testHandler.LogWithEnv();
503+
_testHandler.Handler();
504+
505+
// Assert
506+
507+
consoleOut.Received(1).WriteLine(
508+
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"Environment Service\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"Service: Environment Service\""))
509+
);
510+
consoleOut.Received(1).WriteLine(
511+
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"Attribute Service\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"Service: Attribute Service\""))
512+
);
513+
}
514+
515+
[Fact]
516+
public void When_Setting_Service_Should_Override_Env_And_Empty()
517+
{
518+
// Arrange
519+
var consoleOut = Substitute.For<StringWriter>();
520+
SystemWrapper.Instance.SetOut(consoleOut);
521+
522+
// Act
523+
_testHandler.LogWithAndWithoutEnv();
524+
_testHandler.Handler();
525+
526+
// Assert
527+
528+
consoleOut.Received(2).WriteLine(
529+
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"service_undefined\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"Service: service_undefined\""))
530+
);
531+
consoleOut.Received(1).WriteLine(
532+
Arg.Is<string>(i => i.Contains("\"level\":\"Information\",\"service\":\"Attribute Service\",\"name\":\"AWS.Lambda.Powertools.Logging.Logger\",\"message\":\"Service: Attribute Service\""))
533+
);
534+
}
474535

475536
public void Dispose()
476537
{
477538
Environment.SetEnvironmentVariable("POWERTOOLS_LOGGER_CASE", "");
539+
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "");
478540
LoggingAspect.ResetForTest();
479541
PowertoolsLoggingSerializer.ClearOptions();
480542
}

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Handlers/TestHandlers.cs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Text.Json.Serialization;
1718
using Amazon.Lambda.APIGatewayEvents;
1819
using Amazon.Lambda.ApplicationLoadBalancerEvents;
@@ -40,12 +41,13 @@ public void TestMethodDebug()
4041
public void LogEventNoArgs()
4142
{
4243
}
43-
44-
[Logging(LogEvent = true, LoggerOutputCase = LoggerOutputCase.PascalCase, CorrelationIdPath = "/Headers/MyRequestIdHeader")]
44+
45+
[Logging(LogEvent = true, LoggerOutputCase = LoggerOutputCase.PascalCase,
46+
CorrelationIdPath = "/Headers/MyRequestIdHeader")]
4547
public void LogEvent(TestObject testObject, ILambdaContext context)
4648
{
4749
}
48-
50+
4951
[Logging(LogEvent = false)]
5052
public void LogEventFalse(ILambdaContext context)
5153
{
@@ -81,104 +83,129 @@ public void CorrelationCloudWatchEvent(CloudWatchEvent<S3ObjectCreate> cwEvent)
8183
public void CorrelationIdFromString(TestObject testObject)
8284
{
8385
}
84-
86+
8587
[Logging(CorrelationIdPath = "/headers/my_request_id_header")]
8688
public void CorrelationIdFromStringSnake(TestObject testObject)
8789
{
8890
}
89-
91+
9092
[Logging(CorrelationIdPath = "/Headers/MyRequestIdHeader", LoggerOutputCase = LoggerOutputCase.PascalCase)]
9193
public void CorrelationIdFromStringPascal(TestObject testObject)
9294
{
9395
}
94-
96+
9597
[Logging(CorrelationIdPath = "/headers/myRequestIdHeader", LoggerOutputCase = LoggerOutputCase.CamelCase)]
9698
public void CorrelationIdFromStringCamel(TestObject testObject)
9799
{
98100
}
99-
101+
100102
[Logging(CorrelationIdPath = "/headers/my_request_id_header")]
101103
public void CorrelationIdFromStringSnakeEnv(TestObject testObject)
102104
{
103105
}
104-
106+
105107
[Logging(CorrelationIdPath = "/Headers/MyRequestIdHeader")]
106108
public void CorrelationIdFromStringPascalEnv(TestObject testObject)
107109
{
108110
}
109-
111+
110112
[Logging(CorrelationIdPath = "/headers/myRequestIdHeader")]
111113
public void CorrelationIdFromStringCamelEnv(TestObject testObject)
112114
{
113115
}
114-
116+
115117
[Logging(Service = "test", LoggerOutputCase = LoggerOutputCase.CamelCase)]
116118
public void HandlerService()
117119
{
118120
Logger.LogInformation("test");
119121
}
120-
122+
121123
[Logging(SamplingRate = 0.5, LoggerOutputCase = LoggerOutputCase.CamelCase, LogLevel = LogLevel.Information)]
122124
public void HandlerSamplingRate()
123125
{
124126
Logger.LogInformation("test");
125127
}
126-
128+
127129
[Logging(LogLevel = LogLevel.Critical)]
128130
public void TestLogLevelCritical()
129131
{
130132
Logger.LogCritical("test");
131133
}
132-
134+
133135
[Logging(LogLevel = LogLevel.Critical, LogEvent = true)]
134136
public void TestLogLevelCriticalLogEvent(ILambdaContext context)
135137
{
136138
}
137-
139+
138140
[Logging(LogLevel = LogLevel.Debug, LogEvent = true)]
139141
public void TestLogEventWithoutContext()
140142
{
141143
}
142-
144+
143145
[Logging(LogEvent = true, SamplingRate = 0.2, Service = "my_service")]
144146
public void TestCustomFormatterWithDecorator(string input, ILambdaContext context)
145147
{
146148
}
147-
149+
148150
[Logging(LogEvent = true, SamplingRate = 0.2, Service = "my_service")]
149151
public void TestCustomFormatterWithDecoratorNoContext(string input)
150152
{
151153
}
152-
154+
153155
public void TestCustomFormatterNoDecorator(string input, ILambdaContext context)
154156
{
155157
Logger.LogInformation(input);
156158
}
157-
159+
158160
public void TestLogNoDecorator()
159161
{
160162
Logger.LogInformation("test");
161163
}
162-
164+
163165
[Logging(Service = "test", LoggerOutputCase = LoggerOutputCase.SnakeCase)]
164166
public void TestEnums(string input, ILambdaContext context)
165167
{
166168
Logger.LogInformation(Pet.Dog);
167169
Logger.LogInformation(Thing.Five);
168170
}
169-
171+
170172
public enum Thing
171173
{
172174
One = 1,
173175
Three = 3,
174176
Five = 5
175177
}
176-
178+
177179
[JsonConverter(typeof(JsonStringEnumConverter))]
178180
public enum Pet
179181
{
180182
Cat = 1,
181183
Dog = 3,
182184
Lizard = 5
183185
}
186+
}
187+
188+
public class TestServiceHandler
189+
{
190+
public void LogWithEnv()
191+
{
192+
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "Environment Service");
193+
194+
Logger.LogInformation("Service: Environment Service");
195+
}
196+
197+
public void LogWithAndWithoutEnv()
198+
{
199+
Logger.LogInformation("Service: service_undefined");
200+
201+
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "Environment Service");
202+
203+
Logger.LogInformation("Service: service_undefined");
204+
}
205+
206+
[Logging(Service = "Attribute Service")]
207+
public void Handler()
208+
{
209+
Logger.LogInformation("Service: Attribute Service");
210+
}
184211
}

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
namespace AWS.Lambda.Powertools.Logging.Tests
3131
{
32-
[Collection("Sequential")]
32+
[Collection("X Sequential")]
3333
public class PowertoolsLoggerTest : IDisposable
3434
{
3535
public PowertoolsLoggerTest()
@@ -1337,6 +1337,8 @@ public void Log_WhenMemoryStream_LogsBase64String_UnsafeRelaxedJsonEscaping()
13371337
[Fact]
13381338
public void Log_Set_Execution_Environment_Context()
13391339
{
1340+
var _originalValue = Environment.GetEnvironmentVariable("POWERTOOLS_SERVICE_NAME");
1341+
13401342
// Arrange
13411343
var loggerName = Guid.NewGuid().ToString();
13421344
var assemblyName = "AWS.Lambda.Powertools.Logger";
@@ -1707,6 +1709,7 @@ public void Log_Should_Use_Powertools_Log_Level_When_Set(bool willLog, LogLevel
17071709
public void Dispose()
17081710
{
17091711
PowertoolsLoggingSerializer.ClearOptions();
1712+
LoggingAspect.ResetForTest();
17101713
}
17111714
}
17121715
}

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/TestSetup.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System.Collections.Generic;
17+
using System.Linq;
1618
using Xunit;
19+
using Xunit.Abstractions;
1720

18-
[assembly: CollectionBehavior(DisableTestParallelization = true)]
21+
[assembly: TestCollectionOrderer("AWS.Lambda.Powertools.Logging.Tests.DisplayNameOrderer", "AWS.Lambda.Powertools.Logging.Tests")]
22+
[assembly: CollectionBehavior(DisableTestParallelization = true)]
23+
24+
25+
namespace AWS.Lambda.Powertools.Logging.Tests;
26+
27+
public class DisplayNameOrderer : ITestCollectionOrderer
28+
{
29+
public IEnumerable<ITestCollection> OrderTestCollections(IEnumerable<ITestCollection> testCollections)
30+
{
31+
return testCollections.OrderBy(collection => collection.DisplayName);
32+
}
33+
}

libraries/tests/e2e/functions/core/logging/Function/test/Function.Tests/FunctionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public FunctionTests(ITestOutputHelper testOutputHelper)
2323
[Trait("Category", "AOT")]
2424
[Theory]
2525
[InlineData("E2ETestLambda_X64_AOT_NET8_logging")]
26-
// [InlineData("E2ETestLambda_ARM_AOT_NET8_metrics")]
26+
[InlineData("E2ETestLambda_ARM_AOT_NET8_logging")]
2727
public async Task AotFunctionTest(string functionName)
2828
{
2929
await TestFunction(functionName);

0 commit comments

Comments
 (0)