|
| 1 | +import { join } from 'node:path'; |
| 2 | +import { |
| 3 | + TestInvocationLogs, |
| 4 | + TestStack, |
| 5 | + invokeFunction, |
| 6 | +} from '@aws-lambda-powertools/testing-utils'; |
| 7 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 8 | +import { LoggerTestNodejsFunction } from '../helpers/resources.js'; |
| 9 | +import { |
| 10 | + RESOURCE_NAME_PREFIX, |
| 11 | + SETUP_TIMEOUT, |
| 12 | + STACK_OUTPUT_LOG_GROUP, |
| 13 | + TEARDOWN_TIMEOUT, |
| 14 | + TEST_CASE_TIMEOUT, |
| 15 | +} from './constants.js'; |
| 16 | + |
| 17 | +/** |
| 18 | + * In this e2e test for Logger, we test a number of advanced use cases: |
| 19 | + * - Log buffering enabled with flush on error (both manually on logger.error and automatically on uncaught error) |
| 20 | + * - Correlation ID injection (both manually and automatically) |
| 21 | + * - Cold start detection for provisioned concurrency (always false) |
| 22 | + * |
| 23 | + * The test is split into three cases: |
| 24 | + * - Manual instrumentation |
| 25 | + * - Middy middleware |
| 26 | + * - Decorator |
| 27 | + */ |
| 28 | +describe('Logger E2E - Advanced uses', () => { |
| 29 | + const testStack = new TestStack({ |
| 30 | + stackNameProps: { |
| 31 | + stackNamePrefix: RESOURCE_NAME_PREFIX, |
| 32 | + testName: 'Advanced', |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + // Location of the lambda function code |
| 37 | + const lambdaFunctionCodeFilePath = join( |
| 38 | + __dirname, |
| 39 | + 'advancedUses.test.FunctionCode.ts' |
| 40 | + ); |
| 41 | + |
| 42 | + const invocationCount = 2; |
| 43 | + const invocationLogs = new Map<string, TestInvocationLogs[]>(); |
| 44 | + const manualCase = 'Manual'; |
| 45 | + const middyCase = 'Middy'; |
| 46 | + const decoratorCase = 'Decorator'; |
| 47 | + |
| 48 | + beforeAll(async () => { |
| 49 | + invocationLogs.set(manualCase, []); |
| 50 | + invocationLogs.set(middyCase, []); |
| 51 | + invocationLogs.set(decoratorCase, []); |
| 52 | + for (const caseKey of invocationLogs.keys()) { |
| 53 | + new LoggerTestNodejsFunction( |
| 54 | + testStack, |
| 55 | + { |
| 56 | + entry: lambdaFunctionCodeFilePath, |
| 57 | + handler: `handler${caseKey}`, |
| 58 | + }, |
| 59 | + { |
| 60 | + logGroupOutputKey: STACK_OUTPUT_LOG_GROUP, |
| 61 | + nameSuffix: caseKey, |
| 62 | + createAlias: true, |
| 63 | + } |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + await testStack.deploy(); |
| 68 | + |
| 69 | + for (const caseKey of invocationLogs.keys()) { |
| 70 | + const functionArn = testStack.findAndGetStackOutputValue(caseKey); |
| 71 | + const logs = await invokeFunction({ |
| 72 | + functionName: functionArn, |
| 73 | + times: invocationCount, |
| 74 | + invocationMode: 'SEQUENTIAL', |
| 75 | + payload: [ |
| 76 | + { |
| 77 | + id: 1, |
| 78 | + }, |
| 79 | + { |
| 80 | + id: 2, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }); |
| 84 | + invocationLogs.set(caseKey, logs); |
| 85 | + } |
| 86 | + }, SETUP_TIMEOUT); |
| 87 | + |
| 88 | + it.each([ |
| 89 | + { |
| 90 | + caseKey: manualCase, |
| 91 | + }, |
| 92 | + { |
| 93 | + caseKey: middyCase, |
| 94 | + }, |
| 95 | + { |
| 96 | + caseKey: decoratorCase, |
| 97 | + }, |
| 98 | + ])( |
| 99 | + '$caseKey instrumentation', |
| 100 | + ({ caseKey }) => { |
| 101 | + for (let i = 0; i < invocationCount; i++) { |
| 102 | + const isFirstInvocation = i === 0; |
| 103 | + // Get log messages of the i-th invocation |
| 104 | + const fnLogs = invocationLogs.get(caseKey)?.at(i)?.getFunctionLogs(); |
| 105 | + if (!fnLogs || fnLogs.length === 0) { |
| 106 | + throw new Error(`Failed to get logs for ${caseKey} invocation ${i}`); |
| 107 | + } |
| 108 | + // When using decorator & middleware, we are actually throwing an error |
| 109 | + // which is logged by the runtime, so we need to filter out the logs that are |
| 110 | + // not JSON formatted |
| 111 | + const logs = fnLogs.filter((log) => { |
| 112 | + try { |
| 113 | + JSON.parse(log); |
| 114 | + return true; |
| 115 | + } catch (error) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + if (isFirstInvocation) { |
| 121 | + // Logs outside of the function handler are only present on the first invocation |
| 122 | + expect(TestInvocationLogs.parseFunctionLog(logs[0])).toEqual( |
| 123 | + expect.objectContaining({ |
| 124 | + level: 'DEBUG', |
| 125 | + message: 'a never buffered debug log', |
| 126 | + }) |
| 127 | + ); |
| 128 | + } |
| 129 | + // Since we have an extra log (above) on the first invocation, we need to |
| 130 | + // adjust the index of the logs we are checking |
| 131 | + const logIndexOffset = isFirstInvocation ? 1 : 0; |
| 132 | + const correlationId = i + 1; |
| 133 | + expect( |
| 134 | + TestInvocationLogs.parseFunctionLog(logs[0 + logIndexOffset]) |
| 135 | + ).toEqual( |
| 136 | + expect.objectContaining({ |
| 137 | + level: 'INFO', |
| 138 | + message: 'an info log', |
| 139 | + cold_start: false, |
| 140 | + correlation_id: correlationId, |
| 141 | + }) |
| 142 | + ); |
| 143 | + expect( |
| 144 | + TestInvocationLogs.parseFunctionLog(logs[1 + logIndexOffset]) |
| 145 | + ).toEqual( |
| 146 | + expect.objectContaining({ |
| 147 | + level: 'DEBUG', |
| 148 | + message: 'a buffered debug log', |
| 149 | + cold_start: false, |
| 150 | + correlation_id: correlationId, |
| 151 | + }) |
| 152 | + ); |
| 153 | + expect( |
| 154 | + TestInvocationLogs.parseFunctionLog(logs.at(-1) as string) |
| 155 | + ).toEqual( |
| 156 | + expect.objectContaining({ |
| 157 | + level: 'ERROR', |
| 158 | + message: 'Uncaught error detected, flushing log buffer before exit', |
| 159 | + cold_start: false, |
| 160 | + correlation_id: correlationId, |
| 161 | + error: expect.objectContaining({ |
| 162 | + name: 'Error', |
| 163 | + message: 'ops', |
| 164 | + }), |
| 165 | + }) |
| 166 | + ); |
| 167 | + } |
| 168 | + }, |
| 169 | + TEST_CASE_TIMEOUT |
| 170 | + ); |
| 171 | + |
| 172 | + afterAll(async () => { |
| 173 | + if (!process.env.DISABLE_TEARDOWN) { |
| 174 | + await testStack.destroy(); |
| 175 | + } |
| 176 | + }, TEARDOWN_TIMEOUT); |
| 177 | +}); |
0 commit comments