Skip to content

Commit 014c5bd

Browse files
authored
fix(logger|metrics): properly return decorated class (#489)
* fix: logger to return decorated class * fix: metrics to return decorated class * chore: remove leftover files from logger
1 parent 988578c commit 014c5bd

File tree

6 files changed

+73
-12
lines changed

6 files changed

+73
-12
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,13 @@ class Logger implements ClassThatLogs {
282282
* @returns {HandlerMethodDecorator}
283283
*/
284284
public injectLambdaContext(): HandlerMethodDecorator {
285-
return (target, propertyKey, descriptor) => {
285+
return (target, _propertyKey, descriptor) => {
286286
const originalMethod = descriptor.value;
287287

288288
descriptor.value = (event, context, callback) => {
289289
this.addContext(context);
290290

291-
return originalMethod?.apply(this, [ event, context, callback ]);
291+
return originalMethod?.apply(target, [ event, context, callback ]);
292292
};
293293
};
294294
}

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

+39
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,45 @@ describe('Class: Logger', () => {
605605
jest.spyOn(console, 'log').mockImplementation(() => {});
606606
});
607607

608+
test('when used as decorator, it returns a function with the correct scope of the decorated class', async () => {
609+
610+
// Prepare
611+
const logger = new Logger();
612+
class LambdaFunction implements LambdaInterface {
613+
614+
@logger.injectLambdaContext()
615+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
616+
// @ts-ignore
617+
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
618+
this.myClassMethod();
619+
}
620+
621+
private myClassMethod (): void {
622+
logger.info('This is an INFO log with some context');
623+
}
624+
625+
}
626+
627+
// Act
628+
await new LambdaFunction().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
629+
630+
// Assess
631+
expect(console['info']).toBeCalledTimes(1);
632+
expect(console['info']).toHaveBeenNthCalledWith(1, JSON.stringify({
633+
cold_start: true,
634+
function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
635+
function_memory_size: 128,
636+
function_name: 'foo-bar-function',
637+
function_request_id: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
638+
level: 'INFO',
639+
message: 'This is an INFO log with some context',
640+
service: 'hello-world',
641+
timestamp: '2016-06-20T12:08:10.000Z',
642+
xray_trace_id: 'abcdef123456abcdef123456abcdef123456',
643+
}));
644+
645+
});
646+
608647
test('when used as decorator, it returns a function that captures Lambda\'s context information and adds it in the printed logs', async () => {
609648

610649
// Prepare

Diff for: packages/logger/types/LambdaInterface.ts

-9
This file was deleted.

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

Whitespace-only changes.

Diff for: packages/metrics/src/Metrics.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class Metrics implements MetricsInterface {
244244

245245
let result: unknown;
246246
try {
247-
result = await originalMethod?.apply(this, [ event, context, callback ]);
247+
result = await originalMethod?.apply(target, [ event, context, callback ]);
248248
} catch (error) {
249249
throw error;
250250
} finally {

Diff for: packages/metrics/tests/unit/Metrics.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,43 @@ describe('Class: Metrics', () => {
535535
expect(loggedData[1]._aws.CloudWatchMetrics[0].Metrics.length).toBe(0);
536536
});
537537

538+
test('Using decorator, it returns a function with the correct scope of the decorated class', async () => {
539+
540+
// Prepare
541+
const metrics = new Metrics({ namespace: 'test' });
542+
class LambdaFunction implements LambdaInterface {
543+
@metrics.logMetrics({ defaultDimensions: { default: 'defaultValue' } })
544+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
545+
// @ts-ignore
546+
public async handler<TEvent>(
547+
_event: TEvent,
548+
_context: Context): Promise<string> {
549+
this.myMethod();
550+
551+
return 'Lambda invoked!';
552+
}
553+
554+
private myMethod(): void {
555+
metrics.addMetric('test_name', MetricUnits.Seconds, 10);
556+
}
557+
}
558+
559+
// Act
560+
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext);
561+
562+
// Assess
563+
expect(console.log).toBeCalledTimes(1);
564+
565+
});
566+
538567
test('Using decorator on async handler (without callback) should work fine', async () => {
539568
const metrics = new Metrics({ namespace: 'test' });
540569
const additionalDimension = { name: 'metric2', value: 'metric2Value' };
541570

542571
class LambdaFunction implements LambdaInterface {
543572
@metrics.logMetrics({ defaultDimensions: { default: 'defaultValue' } })
573+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
574+
// @ts-ignore
544575
public async handler<TEvent>(
545576
_event: TEvent,
546577
_context: Context): Promise<string> {

0 commit comments

Comments
 (0)