Skip to content

Commit 9505847

Browse files
committed
chore: fix merge conflicts
2 parents a23d869 + a574406 commit 9505847

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

packages/tracer/src/Tracer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class Tracer extends Utility implements TracerInterface {
420420
* @decorator Class
421421
*/
422422
public captureMethod(options?: HandlerOptions): MethodDecorator {
423-
return (_target, _propertyKey, descriptor) => {
423+
return (_target, propertyKey, descriptor) => {
424424
// The descriptor.value is the method this decorator decorates, it cannot be undefined.
425425
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
426426
const originalMethod = descriptor.value!;
@@ -434,12 +434,14 @@ class Tracer extends Utility implements TracerInterface {
434434
return originalMethod.apply(this, [...args]);
435435
}
436436

437-
return tracerRef.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => {
437+
const methodName = String(propertyKey);
438+
439+
return tracerRef.provider.captureAsyncFunc(`### ${methodName}`, async subsegment => {
438440
let result;
439441
try {
440442
result = await originalMethod.apply(this, [...args]);
441443
if (options?.captureResponse ?? true) {
442-
tracerRef.addResponseAsMetadata(result, originalMethod.name);
444+
tracerRef.addResponseAsMetadata(result, methodName);
443445
}
444446
} catch (error) {
445447
tracerRef.addErrorAsMetadata(error as Error);

packages/tracer/tests/unit/Tracer.test.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ describe('Class: Tracer', () => {
12941294
// Prepare
12951295
const tracer: Tracer = new Tracer();
12961296
const newSubsegment: Segment | Subsegment | undefined = new Subsegment('### dummyMethod');
1297-
1297+
12981298
jest.spyOn(tracer.provider, 'getSegment')
12991299
.mockImplementation(() => newSubsegment);
13001300
setContextMissingStrategy(() => null);
@@ -1319,23 +1319,82 @@ describe('Class: Tracer', () => {
13191319
public otherDummyMethod(): void {
13201320
return;
13211321
}
1322-
13231322
}
1324-
1323+
13251324
// Act
13261325
const lambda = new Lambda();
13271326
const otherDummyMethodSpy = jest.spyOn(lambda, 'otherDummyMethod').mockImplementation();
13281327
const handler = lambda.handler.bind(lambda);
13291328
await handler({}, context, () => console.log('Lambda invoked!'));
1330-
1331-
// Assess
1329+
13321330
// Here we assert that the subsegment.close() (inside the finally of decorator) is called before the other otherDummyMethodSpy method
13331331
// that should always be called after the handler has returned. If subsegment.close() is called after it means the
13341332
// decorator is NOT awaiting the method which would cause the test to fail.
13351333
expect(subsegmentCloseSpy.mock.invocationCallOrder[0]).toBeLessThan(otherDummyMethodSpy.mock.invocationCallOrder[0]);
13361334

13371335
});
13381336

1337+
test('when used as decorator together with another external decorator, the method name is detected properly', async () => {
1338+
1339+
// Prepare
1340+
const tracer: Tracer = new Tracer();
1341+
const newSubsegment: Segment | Subsegment | undefined = new Subsegment('### dummyMethod');
1342+
jest.spyOn(tracer.provider, 'getSegment')
1343+
.mockImplementation(() => newSubsegment);
1344+
setContextMissingStrategy(() => null);
1345+
createCaptureAsyncFuncMock(tracer.provider, newSubsegment);
1346+
1347+
// Creating custom external decorator
1348+
// eslint-disable-next-line func-style
1349+
function passThrough() {
1350+
// A decorator that calls the original method.
1351+
return (
1352+
_target: unknown,
1353+
_propertyKey: string,
1354+
descriptor: PropertyDescriptor
1355+
) => {
1356+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1357+
const originalMethod = descriptor.value!;
1358+
descriptor.value = function (...args: unknown[]) {
1359+
return originalMethod.apply(this, [...args]);
1360+
};
1361+
};
1362+
}
1363+
1364+
class Lambda implements LambdaInterface {
1365+
@tracer.captureMethod()
1366+
@passThrough()
1367+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1368+
// @ts-ignore
1369+
public async dummyMethod(): Promise<string> {
1370+
return `foo`;
1371+
}
1372+
1373+
public async handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): Promise<void> {
1374+
await this.dummyMethod();
1375+
1376+
return;
1377+
}
1378+
1379+
}
1380+
1381+
// Act / Assess
1382+
const lambda = new Lambda();
1383+
const handler = lambda.handler.bind(lambda);
1384+
await handler({}, context, () => console.log('Lambda invoked!'));
1385+
1386+
// Assess
1387+
expect(newSubsegment).toEqual(expect.objectContaining({
1388+
metadata: {
1389+
'hello-world': {
1390+
// Assess that the method name is added correctly
1391+
'dummyMethod response': 'foo',
1392+
},
1393+
}
1394+
}));
1395+
1396+
});
1397+
13391398
});
13401399

13411400
describe('Method: captureAWS', () => {

0 commit comments

Comments
 (0)