Repository navigation
Expand file tree
/
Copy pathPowertoolsLoggingTest.java
More file actions
545 lines (444 loc) · 19.4 KB
/
Copy pathPowertoolsLoggingTest.java
File metadata and controls
545 lines (444 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
* Copyright 2023 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.logging;
import static org.apache.commons.lang3.reflect.FieldUtils.writeStaticField;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.contentOf;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.channels.FileChannel;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import com.amazonaws.services.lambda.runtime.Context;
import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor;
import software.amazon.lambda.powertools.common.stubs.TestLambdaContext;
import software.amazon.lambda.powertools.logging.internal.LoggingConstants;
import software.amazon.lambda.powertools.logging.internal.LoggingManagerRegistry;
import software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields;
import software.amazon.lambda.powertools.logging.internal.TestLoggingManager;
class PowertoolsLoggingTest {
private static final Logger LOG = LoggerFactory.getLogger(PowertoolsLoggingTest.class);
private TestLoggingManager testManager;
private Context context;
@BeforeEach
void setUp() throws IllegalAccessException, IOException {
// Get the TestLoggingManager instance from registry
testManager = (TestLoggingManager) LoggingManagerRegistry.getLoggingManager();
testManager.resetBufferState();
context = new TestLambdaContext();
// Reset environment variables for clean test isolation
writeStaticField(LoggingConstants.class, "LAMBDA_LOG_LEVEL", null, true);
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", null, true);
writeStaticField(LoggingConstants.class, "POWERTOOLS_SAMPLING_RATE", null, true);
// Clear MDC for clean test isolation
MDC.clear();
// Reset cold start state
writeStaticField(LambdaHandlerProcessor.class, "isColdStart", null, true);
writeStaticField(PowertoolsLogging.class, "hasBeenInitialized", new AtomicBoolean(false), true);
try {
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
} catch (NoSuchFileException e) {
// may not be there in the first run
}
}
@AfterEach
void cleanUp() throws IOException {
// Make sure file is cleaned up
try {
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
} catch (NoSuchFileException e) {
// may not be there in the first run
}
}
@Test
void testFlushBuffer_shouldCallBufferManager() {
// WHEN
PowertoolsLogging.flushBuffer();
// THEN
assertThat(testManager.isBufferFlushed()).isTrue();
}
@Test
void testClearBuffer_shouldCallBufferManager() {
// WHEN
PowertoolsLogging.clearBuffer();
// THEN
assertThat(testManager.isBufferCleared()).isTrue();
}
@Test
void shouldLogDebugWhenPowertoolsLevelEnvVarIsDebug() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "DEBUG", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isTrue();
}
@Test
void shouldLogInfoWhenPowertoolsLevelEnvVarIsInfo() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "INFO", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isFalse();
assertThat(LOG.isInfoEnabled()).isTrue();
}
@Test
void shouldLogInfoWhenPowertoolsLevelEnvVarIsInvalid() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "INVALID", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isFalse();
assertThat(LOG.isInfoEnabled()).isTrue();
}
@Test
void shouldLogWarnWhenPowertoolsLevelEnvVarIsWarn() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "WARN", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isFalse();
assertThat(LOG.isInfoEnabled()).isFalse();
assertThat(LOG.isWarnEnabled()).isTrue();
}
@Test
void shouldLogErrorWhenPowertoolsLevelEnvVarIsError() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "ERROR", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isFalse();
assertThat(LOG.isInfoEnabled()).isFalse();
assertThat(LOG.isWarnEnabled()).isFalse();
assertThat(LOG.isErrorEnabled()).isTrue();
}
@Test
void shouldLogErrorWhenPowertoolsLevelEnvVarIsFatal() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "FATAL", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isFalse();
assertThat(LOG.isInfoEnabled()).isFalse();
assertThat(LOG.isWarnEnabled()).isFalse();
assertThat(LOG.isErrorEnabled()).isTrue();
}
@Test
void shouldLogWarnWhenPowertoolsLevelEnvVarIsWarnAndLambdaLevelVarIsInfo() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "WARN", true);
writeStaticField(LoggingConstants.class, "LAMBDA_LOG_LEVEL", "INFO", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isFalse();
assertThat(LOG.isInfoEnabled()).isFalse();
assertThat(LOG.isWarnEnabled()).isTrue();
File logFile = new File("target/logfile.json");
assertThat(contentOf(logFile))
.doesNotContain(" does not match AWS Lambda Advanced Logging Controls minimum log level");
}
@Test
void shouldLogInfoWhenPowertoolsLevelEnvVarIsInfoAndLambdaLevelVarIsWarn() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "INFO", true);
writeStaticField(LoggingConstants.class, "LAMBDA_LOG_LEVEL", "WARN", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isFalse();
assertThat(LOG.isInfoEnabled()).isTrue();
File logFile = new File("target/logfile.json");
// should log a warning as powertools level is lower than lambda level
assertThat(contentOf(logFile)).contains(
"Current log level (INFO) does not match AWS Lambda Advanced Logging Controls minimum log level (WARN). This can lead to data loss, consider adjusting them.");
}
@Test
void shouldLogWarnWhenPowertoolsLevelEnvVarINotSetAndLambdaLevelVarIsWarn() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", null, true);
writeStaticField(LoggingConstants.class, "LAMBDA_LOG_LEVEL", "WARN", true);
// WHEN
reinitializeLogLevel();
// THEN
assertThat(LOG.isDebugEnabled()).isFalse();
assertThat(LOG.isInfoEnabled()).isFalse();
assertThat(LOG.isWarnEnabled()).isTrue();
}
@Test
void initializeLogging_withContextOnly_shouldSetLambdaFields() {
// WHEN
PowertoolsLogging.initializeLogging(context);
// THEN
Map<String, String> mdcMap = MDC.getCopyOfContextMap();
assertThat(mdcMap)
.containsEntry(PowertoolsLoggedFields.FUNCTION_NAME.getName(), "test-function")
.containsEntry(PowertoolsLoggedFields.FUNCTION_VERSION.getName(), "1")
.containsEntry(PowertoolsLoggedFields.FUNCTION_COLD_START.getName(), "true")
.containsEntry(PowertoolsLoggedFields.SERVICE.getName(), "testService");
}
@Test
void initializeLogging_withSamplingRate_shouldSetSamplingRateInMdc() {
// WHEN
PowertoolsLogging.initializeLogging(context, 0.5);
// THEN
assertThat(MDC.get(PowertoolsLoggedFields.SAMPLING_RATE.getName())).isEqualTo("0.5");
}
@Test
void initializeLogging_withCorrelationId_shouldExtractFromEvent() {
// GIVEN
Map<String, Object> event = Map.of("requestContext", Map.of("requestId", "test-correlation-id"));
// WHEN
PowertoolsLogging.initializeLogging(context, "requestContext.requestId", event);
// THEN
assertThat(MDC.get(PowertoolsLoggedFields.CORRELATION_ID.getName())).isEqualTo("test-correlation-id");
}
@Test
void initializeLogging_withFullConfiguration_shouldSetAllFields() {
// GIVEN
Map<String, Object> event = Map.of("id", "correlation-123");
// WHEN
PowertoolsLogging.initializeLogging(context, 0.5, "id", event);
// THEN
Map<String, String> mdcMap = MDC.getCopyOfContextMap();
assertThat(mdcMap)
.containsEntry(PowertoolsLoggedFields.FUNCTION_NAME.getName(), "test-function")
.containsEntry(PowertoolsLoggedFields.CORRELATION_ID.getName(), "correlation-123")
.containsEntry(PowertoolsLoggedFields.SAMPLING_RATE.getName(), "0.5");
}
@Test
void initializeLogging_withInvalidSamplingRate_shouldSkipSampling() {
// WHEN
PowertoolsLogging.initializeLogging(context, 2.0);
// THEN
assertThat(MDC.get(PowertoolsLoggedFields.SAMPLING_RATE.getName())).isNull();
}
@Test
void initializeLogging_withSamplingRate_whenLogLevelIsTrace_shouldNotLowerLevelToDebug() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "TRACE", true);
reinitializeLogLevel();
assertThat(LOG.isTraceEnabled()).isTrue();
// WHEN
PowertoolsLogging.initializeLogging(context, 1.0);
// THEN
assertThat(LOG.isTraceEnabled()).isTrue();
}
@Test
void initializeLogging_withSamplingRate_whenLogLevelIsInfo_shouldRaiseLevelToDebug() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "INFO", true);
reinitializeLogLevel();
assertThat(LOG.isDebugEnabled()).isFalse();
// WHEN
PowertoolsLogging.initializeLogging(context, 1.0);
// THEN
assertThat(LOG.isDebugEnabled()).isTrue();
}
@Test
void initializeLogging_withEnvVarAndParameter_shouldUseEnvVarPrecedence() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_SAMPLING_RATE", "0.8", true);
// WHEN
PowertoolsLogging.initializeLogging(context, 0.3);
// THEN
assertThat(MDC.get(PowertoolsLoggedFields.SAMPLING_RATE.getName())).isEqualTo("0.8");
}
@Test
void initializeLogging_calledTwice_shouldMarkColdStartDoneOnSecondCall() throws IllegalAccessException {
// GIVEN
writeStaticField(PowertoolsLogging.class, "hasBeenInitialized", new AtomicBoolean(false), true);
// WHEN - First call
PowertoolsLogging.initializeLogging(context);
String firstCallColdStart = MDC.get(PowertoolsLoggedFields.FUNCTION_COLD_START.getName());
// WHEN - Second call
PowertoolsLogging.initializeLogging(context);
String secondCallColdStart = MDC.get(PowertoolsLoggedFields.FUNCTION_COLD_START.getName());
// THEN
assertThat(firstCallColdStart).isEqualTo("true");
assertThat(secondCallColdStart).isEqualTo("false");
}
@Test
void initializeLogging_withNullContext_shouldNotThrow() {
// WHEN & THEN
assertThatNoException().isThrownBy(() -> {
PowertoolsLogging.initializeLogging(null);
PowertoolsLogging.initializeLogging(null, 0.5);
PowertoolsLogging.initializeLogging(null, "path", Map.of());
PowertoolsLogging.initializeLogging(null, 0.5, "path", Map.of());
});
}
@Test
void clearState_shouldClearMdcAndBuffer() {
// GIVEN
MDC.put("test", "value");
// WHEN
PowertoolsLogging.clearState(true);
// THEN
assertThat(MDC.getCopyOfContextMap()).isNull();
assertThat(testManager.isBufferCleared()).isTrue();
}
@Test
void clearState_withoutMdcClear_shouldOnlyClearBuffer() {
// GIVEN
MDC.put("test", "value");
// WHEN
PowertoolsLogging.clearState(false);
// THEN
assertThat(MDC.get("test")).isEqualTo("value");
assertThat(testManager.isBufferCleared()).isTrue();
}
@Test
void initializeLogging_concurrentCalls_shouldBeThreadSafe() throws InterruptedException {
// GIVEN
int threadCount = 10;
Thread[] threads = new Thread[threadCount];
String[] samplingRates = new String[threadCount];
boolean[] coldStarts = new boolean[threadCount];
boolean[] success = new boolean[threadCount];
// WHEN - Multiple threads call initializeLogging with alternating sampling rates
for (int i = 0; i < threadCount; i++) {
final int threadIndex = i;
final double samplingRate = (i % 2 == 0) ? 1.0 : 0.0; // Alternate between 1.0 and 0.0
threads[i] = new Thread(() -> {
try {
PowertoolsLogging.initializeLogging(context, samplingRate);
// Capture the sampling rate and cold start values set in MDC (thread-local)
samplingRates[threadIndex] = MDC.get(PowertoolsLoggedFields.SAMPLING_RATE.getName());
coldStarts[threadIndex] = Boolean
.parseBoolean(MDC.get(PowertoolsLoggedFields.FUNCTION_COLD_START.getName()));
success[threadIndex] = true;
// Clean up thread-local state
PowertoolsLogging.clearState(true);
} catch (Exception e) {
success[threadIndex] = false;
}
});
}
// Start all threads
for (Thread thread : threads) {
thread.start();
}
// Wait for all threads to complete
for (Thread thread : threads) {
thread.join();
}
// THEN - All threads should complete successfully
for (boolean result : success) {
assertThat(result).isTrue();
}
// THEN - Each thread should have its own sampling rate in MDC and exactly one invocation was a cold start
int coldStartCount = 0;
for (int i = 0; i < threadCount; i++) {
String expectedSamplingRate = (i % 2 == 0) ? "1.0" : "0.0";
assertThat(samplingRates[i]).as("Thread %d should have sampling rate %s", i, expectedSamplingRate)
.isEqualTo(expectedSamplingRate);
coldStartCount += coldStarts[i] ? 1 : 0;
}
assertThat(coldStartCount).isEqualTo(1);
}
@Test
void withLogging_basicUsage_shouldInitializeAndCleanup() {
// WHEN
String result = PowertoolsLogging.withLogging(context, () -> {
assertThat(MDC.get(PowertoolsLoggedFields.FUNCTION_NAME.getName())).isEqualTo("test-function");
return "test-result";
});
// THEN
assertThat(result).isEqualTo("test-result");
assertThat(MDC.getCopyOfContextMap()).isNull();
assertThat(testManager.isBufferCleared()).isTrue();
}
@Test
void withLogging_withSamplingRate_shouldSetSamplingRateAndCleanup() {
// WHEN
String result = PowertoolsLogging.withLogging(context, 0.5, () -> {
assertThat(MDC.get(PowertoolsLoggedFields.SAMPLING_RATE.getName())).isEqualTo("0.5");
return "sampled-result";
});
// THEN
assertThat(result).isEqualTo("sampled-result");
assertThat(MDC.getCopyOfContextMap()).isNull();
}
@Test
void withLogging_withCorrelationId_shouldExtractCorrelationIdAndCleanup() {
// GIVEN
Map<String, Object> event = Map.of("requestId", "correlation-123");
// WHEN
Integer result = PowertoolsLogging.withLogging(context, "requestId", event, () -> {
assertThat(MDC.get(PowertoolsLoggedFields.CORRELATION_ID.getName())).isEqualTo("correlation-123");
return 42;
});
// THEN
assertThat(result).isEqualTo(42);
assertThat(MDC.getCopyOfContextMap()).isNull();
}
@Test
void withLogging_withFullConfiguration_shouldSetAllFieldsAndCleanup() {
// GIVEN
Map<String, Object> event = Map.of("id", "full-correlation");
// WHEN
Boolean result = PowertoolsLogging.withLogging(context, 0.8, "id", event, () -> {
Map<String, String> mdcMap = MDC.getCopyOfContextMap();
assertThat(mdcMap)
.containsEntry(PowertoolsLoggedFields.FUNCTION_NAME.getName(), "test-function")
.containsEntry(PowertoolsLoggedFields.CORRELATION_ID.getName(), "full-correlation")
.containsEntry(PowertoolsLoggedFields.SAMPLING_RATE.getName(), "0.8");
return true;
});
// THEN
assertThat(result).isTrue();
assertThat(MDC.getCopyOfContextMap()).isNull();
}
@Test
void withLogging_whenSupplierThrowsException_shouldStillCleanup() {
// WHEN & THEN
try {
PowertoolsLogging.withLogging(context, () -> {
assertThat(MDC.get(PowertoolsLoggedFields.FUNCTION_NAME.getName())).isEqualTo("test-function");
throw new RuntimeException("test exception");
});
} catch (RuntimeException e) {
assertThat(e.getMessage()).isEqualTo("test exception");
}
// THEN - cleanup should still happen
assertThat(MDC.getCopyOfContextMap()).isNull();
assertThat(testManager.isBufferCleared()).isTrue();
}
private void reinitializeLogLevel() {
try {
Method initializeLogLevel = PowertoolsLogging.class.getDeclaredMethod("initializeLogLevel");
initializeLogLevel.setAccessible(true);
initializeLogLevel.invoke(null);
} catch (Exception e) {
throw new RuntimeException("Failed to reinitialize log level", e);
}
}
}