Skip to content

Commit 6413215

Browse files
flochazdreamorosi
andauthored
feat(ALL): Use optional callback LambdaInterface for decorator (#397)
* feat(metrics): Use optional callback LambdaInterface for decorator * feat(logger): Use optional callback LambdaInterface for decorator * feat(tracer): Use optional callback LambdaInterface for decorator Co-authored-by: Andrea Amorosi <[email protected]>
1 parent 83bf7f4 commit 6413215

17 files changed

+3045
-9953
lines changed

Diff for: packages/logger/npm-shrinkwrap.json

+276-6,279
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/logger/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"url": "https://github.com/awslabs/aws-lambda-powertools-typescript/issues"
6464
},
6565
"dependencies": {
66-
"@aws-lambda-powertools/commons": "^0.0.2",
66+
"@aws-lambda-powertools/commons": "^0.2.0-beta.18",
6767
"@middy/core": "^2.5.3",
6868
"@types/aws-lambda": "^8.10.72",
6969
"lodash": "^4.17.21",

Diff for: packages/logger/src/types/Logger.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LambdaInterface } from '@aws-lambda-powertools/commons';
1+
import { AsyncHandler, LambdaInterface, SyncHandler } from '@aws-lambda-powertools/commons';
22
import { Handler } from 'aws-lambda';
33
import { ConfigServiceInterface } from '../config';
44
import { LogFormatterInterface } from '../formatter';
@@ -55,8 +55,8 @@ type LogItemExtraInput = Array<Error | LogAttributes | unknown>;
5555
type HandlerMethodDecorator = (
5656
target: LambdaInterface,
5757
propertyKey: string | symbol,
58-
descriptor: TypedPropertyDescriptor<Handler>
59-
) => TypedPropertyDescriptor<Handler> | void;
58+
descriptor: TypedPropertyDescriptor<SyncHandler<Handler>> | TypedPropertyDescriptor<AsyncHandler<Handler>>
59+
) => void;
6060

6161
export {
6262
ClassThatLogs,

Diff for: packages/logger/tests/unit/Logger.test.ts

+45
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,51 @@ describe('Class: Logger', () => {
642642

643643
});
644644

645+
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 () => {
646+
647+
// Prepare
648+
const expectedReturnValue = 'Lambda invoked!';
649+
const logger = new Logger();
650+
class LambdaFunction implements LambdaInterface {
651+
652+
@logger.injectLambdaContext()
653+
public async handler<TEvent>(_event: TEvent, _context: Context): Promise<string> {
654+
logger.info('This is an INFO log with some context');
655+
656+
return expectedReturnValue;
657+
}
658+
}
659+
660+
// Act
661+
logger.info('An INFO log without context!');
662+
const actualResult = await new LambdaFunction().handler(dummyEvent, dummyContext);
663+
664+
// Assess
665+
666+
expect(actualResult).toEqual(expectedReturnValue);
667+
expect(console['info']).toBeCalledTimes(2);
668+
expect(console['info']).toHaveBeenNthCalledWith(1, JSON.stringify({
669+
level: 'INFO',
670+
message: 'An INFO log without context!',
671+
service: 'hello-world',
672+
timestamp: '2016-06-20T12:08:10.000Z',
673+
xray_trace_id: 'abcdef123456abcdef123456abcdef123456',
674+
}));
675+
expect(console['info']).toHaveBeenNthCalledWith(2, JSON.stringify({
676+
cold_start: true,
677+
function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
678+
function_memory_size: 128,
679+
function_name: 'foo-bar-function',
680+
function_request_id: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
681+
level: 'INFO',
682+
message: 'This is an INFO log with some context',
683+
service: 'hello-world',
684+
timestamp: '2016-06-20T12:08:10.000Z',
685+
xray_trace_id: 'abcdef123456abcdef123456abcdef123456',
686+
}));
687+
688+
});
689+
645690
});
646691

647692
describe('Method: setColdStartValue', () => {

0 commit comments

Comments
 (0)