Skip to content
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

chore: add @middy/core v6 as peer dependency #3368

Merged
merged 7 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 22 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"markdownlint-cli2": "^0.15.0",
"middy4": "npm:@middy/core@^4.7.0",
"middy5": "npm:@middy/core@^5.4.3",
"middy6": "npm:@middy/core@^6.0.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typedoc": "^0.26.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/idempotency/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"peerDependencies": {
"@aws-sdk/client-dynamodb": ">=3.x",
"@aws-sdk/lib-dynamodb": ">=3.x",
"@middy/core": "4.x || 5.x"
"@middy/core": "4.x || 5.x || 6.x"
},
"peerDependenciesMeta": {
"@aws-sdk/client-dynamodb": {
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@types/lodash.merge": "^4.6.9"
},
"peerDependencies": {
"@middy/core": "4.x || 5.x"
"@middy/core": "4.x || 5.x || 6.x"
},
"peerDependenciesMeta": {
"@middy/core": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { APIGatewayAuthorizerResult, Context } from 'aws-lambda';
import middy from 'middy6';
import { Logger } from '../../src/index.js';
import { injectLambdaContext } from '../../src/middleware/middy.js';
import type { TestEvent, TestOutput } from '../helpers/types.js';

const PERSISTENT_KEY = process.env.PERSISTENT_KEY || 'persistentKey';
const PERSISTENT_VALUE = process.env.PERSISTENT_VALUE || 'persistentValue';
const REMOVABLE_KEY = process.env.REMOVABLE_KEY || 'removableKey';
const REMOVABLE_VALUE = process.env.REMOVABLE_VALUE || 'remvovableValue';
const ERROR_MSG = process.env.ERROR_MSG || 'error';
const RUNTIME_ADDED_KEY = process.env.RUNTIME_ADDED_KEY || 'runtimeAddedKey';
const SINGLE_LOG_ITEM_KEY =
process.env.SINGLE_LOG_ITEM_KEY || 'keyForSingleLogItem';
const SINGLE_LOG_ITEM_VALUE =
process.env.SINGLE_LOG_ITEM_VALUE || 'valueForSingleLogItem';
const ARBITRARY_OBJECT_KEY =
process.env.ARBITRARY_OBJECT_KEY || 'keyForArbitraryObject';
const ARBITRARY_OBJECT_DATA =
process.env.ARBITRARY_OBJECT_DATA || 'arbitrary object data';

const logger = new Logger({
persistentLogAttributes: {
[PERSISTENT_KEY]: PERSISTENT_VALUE, // This key-value pair will be added to every log
[REMOVABLE_KEY]: REMOVABLE_VALUE, // This other one will be removed at runtime and not displayed in any log
},
});

const testFunction = async (event: TestEvent, context: Context): TestOutput => {
// Test feature 1: Context data injection (all logs should have the same context data)
// Test feature 2: Event log (this log should have the event data)
// Test feature 3: Log level filtering (log level is set to INFO)
logger.debug('##### This should not appear');

// Test feature 4: Add and remove persistent additional log keys and value
logger.removeKeys([REMOVABLE_KEY]); // This key should not appear in any log (except the event log)
logger.appendKeys({
// This key-value pair should appear in every log (except the event log)
[RUNTIME_ADDED_KEY]: 'bar',
});

// Test feature 5: One-time additional log keys and values
logger.info('This is an one-time log with an additional key-value', {
[SINGLE_LOG_ITEM_KEY]: SINGLE_LOG_ITEM_VALUE,
});

// Test feature 6: Error logging
try {
throw new Error(ERROR_MSG);
} catch (e) {
logger.error(ERROR_MSG, e as Error);
}

// Test feature 7: Arbitrary object logging
const obj: APIGatewayAuthorizerResult = {
principalId: ARBITRARY_OBJECT_DATA,
policyDocument: {
Version: 'Version 1',
Statement: [
{
Effect: 'Allow',
Action: 'geo:*',
Resource: '*',
},
],
},
};
logger.info('A log entry with an object', { [ARBITRARY_OBJECT_KEY]: obj });

// Test feature 8: X-Ray Trace ID injection (all logs should have the same X-Ray Trace ID)

return {
requestId: context.awsRequestId,
};
};

export const handler = middy(testFunction).use(
injectLambdaContext(logger, { clearState: true, logEvent: true })
);
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Logger E2E tests, log event via env var setting with middy', () => {
// Location of the lambda function code
const lambdaFunctionCodeFilePath = join(
__dirname,
'logEventEnvVarSetting.middy.test.FunctionCode.ts'
'logEventEnvVarSetting.middy4.test.FunctionCode.ts'
);

const invocationCount = 3;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Context } from 'aws-lambda';
import middy from 'middy5';
import { Logger } from '../../src/index.js';
import { injectLambdaContext } from '../../src/middleware/middy.js';
import type { TestEvent, TestOutput } from '../helpers/types.js';

const logger = new Logger();

const testFunction = async (
_event: TestEvent,
context: Context
): TestOutput => ({
requestId: context.awsRequestId,
});

export const handler = middy(testFunction)
// The event should be logged because POWERTOOLS_LOGGER_LOG_EVENT is set to true
.use(injectLambdaContext(logger));
100 changes: 100 additions & 0 deletions packages/logger/tests/e2e/logEventEnvVarSetting.middy5.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { join } from 'node:path';
import {
TestInvocationLogs,
TestStack,
invokeFunction,
} from '@aws-lambda-powertools/testing-utils';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { LoggerTestNodejsFunction } from '../helpers/resources.js';
import {
RESOURCE_NAME_PREFIX,
SETUP_TIMEOUT,
STACK_OUTPUT_LOG_GROUP,
TEARDOWN_TIMEOUT,
TEST_CASE_TIMEOUT,
} from './constants.js';

describe('Logger E2E tests, log event via env var setting with middy', () => {
const testStack = new TestStack({
stackNameProps: {
stackNamePrefix: RESOURCE_NAME_PREFIX,
testName: 'LogEventFromEnv-Middy',
},
});

// Location of the lambda function code
const lambdaFunctionCodeFilePath = join(
__dirname,
'logEventEnvVarSetting.middy5.test.FunctionCode.ts'
);

const invocationCount = 3;
let invocationLogs: TestInvocationLogs[];
let logGroupName: string;

beforeAll(async () => {
// Prepare
new LoggerTestNodejsFunction(
testStack,
{
entry: lambdaFunctionCodeFilePath,
environment: {
POWERTOOLS_LOGGER_LOG_EVENT: 'true',
},
},
{
logGroupOutputKey: STACK_OUTPUT_LOG_GROUP,
nameSuffix: 'LogEventFromEnv',
}
);

await testStack.deploy();
logGroupName = testStack.findAndGetStackOutputValue(STACK_OUTPUT_LOG_GROUP);
const functionName =
testStack.findAndGetStackOutputValue('LogEventFromEnv');

invocationLogs = await invokeFunction({
functionName,
invocationMode: 'SEQUENTIAL',
times: invocationCount,
payload: {
foo: 'bar',
},
});

console.log('logGroupName', logGroupName);
}, SETUP_TIMEOUT);

describe('Log event', () => {
it(
'should log the event as the first log of each invocation only',
async () => {
for (let i = 0; i < invocationCount; i++) {
// Get log messages of the invocation
const logMessages = invocationLogs[i].getFunctionLogs();

for (const [index, message] of logMessages.entries()) {
const log = TestInvocationLogs.parseFunctionLog(message);
// Check that the event is logged on the first log
if (index === 0) {
expect(log).toHaveProperty('event');
expect(log.event).toStrictEqual(
expect.objectContaining({ foo: 'bar' })
);
// Check that the event is not logged again on the rest of the logs
} else {
expect(log).not.toHaveProperty('event');
}
}
}
},
TEST_CASE_TIMEOUT
);
});

afterAll(async () => {
if (!process.env.DISABLE_TEARDOWN) {
await testStack.destroy();
}
}, TEARDOWN_TIMEOUT);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Context } from 'aws-lambda';
import middy from 'middy6';
import { Logger } from '../../src/index.js';
import { injectLambdaContext } from '../../src/middleware/middy.js';
import type { TestEvent, TestOutput } from '../helpers/types.js';

const logger = new Logger();

const testFunction = async (
_event: TestEvent,
context: Context
): TestOutput => ({
requestId: context.awsRequestId,
});

export const handler = middy(testFunction)
// The event should be logged because POWERTOOLS_LOGGER_LOG_EVENT is set to true
.use(injectLambdaContext(logger));
Loading