Skip to content

feat(ALL): Use optional callback LambdaInterface for decorator #397

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 7 commits into from
Jan 4, 2022
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
6,555 changes: 276 additions & 6,279 deletions packages/logger/npm-shrinkwrap.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"url": "https://github.com/awslabs/aws-lambda-powertools-typescript/issues"
},
"dependencies": {
"@aws-lambda-powertools/commons": "^0.0.2",
"@aws-lambda-powertools/commons": "^0.2.0-beta.18",
"@middy/core": "^2.5.3",
"@types/aws-lambda": "^8.10.72",
"lodash": "^4.17.21",
Expand Down
6 changes: 3 additions & 3 deletions packages/logger/src/types/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LambdaInterface } from '@aws-lambda-powertools/commons';
import { AsyncHandler, LambdaInterface, SyncHandler } from '@aws-lambda-powertools/commons';
import { Handler } from 'aws-lambda';
import { ConfigServiceInterface } from '../config';
import { LogFormatterInterface } from '../formatter';
Expand Down Expand Up @@ -55,8 +55,8 @@ type LogItemExtraInput = Array<Error | LogAttributes | unknown>;
type HandlerMethodDecorator = (
target: LambdaInterface,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<Handler>
) => TypedPropertyDescriptor<Handler> | void;
descriptor: TypedPropertyDescriptor<SyncHandler<Handler>> | TypedPropertyDescriptor<AsyncHandler<Handler>>
) => void;

export {
ClassThatLogs,
Expand Down
45 changes: 45 additions & 0 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,51 @@ describe('Class: Logger', () => {

});

test('when used as decorator on an async handler without context, it returns a function that captures Lambda\'s context information and adds it in the printed logs', async () => {

// Prepare
const expectedReturnValue = 'Lambda invoked!';
const logger = new Logger();
class LambdaFunction implements LambdaInterface {

@logger.injectLambdaContext()
public async handler<TEvent>(_event: TEvent, _context: Context): Promise<string> {
logger.info('This is an INFO log with some context');

return expectedReturnValue;
}
}

// Act
logger.info('An INFO log without context!');
const actualResult = await new LambdaFunction().handler(dummyEvent, dummyContext);

// Assess

expect(actualResult).toEqual(expectedReturnValue);
expect(console['info']).toBeCalledTimes(2);
expect(console['info']).toHaveBeenNthCalledWith(1, JSON.stringify({
level: 'INFO',
message: 'An INFO log without context!',
service: 'hello-world',
timestamp: '2016-06-20T12:08:10.000Z',
xray_trace_id: 'abcdef123456abcdef123456abcdef123456',
}));
expect(console['info']).toHaveBeenNthCalledWith(2, JSON.stringify({
cold_start: true,
function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
function_memory_size: 128,
function_name: 'foo-bar-function',
function_request_id: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
level: 'INFO',
message: 'This is an INFO log with some context',
service: 'hello-world',
timestamp: '2016-06-20T12:08:10.000Z',
xray_trace_id: 'abcdef123456abcdef123456abcdef123456',
}));

});

});

describe('Method: setColdStartValue', () => {
Expand Down
Loading