Skip to content

Commit fea7f0d

Browse files
committed
Typing & Tests
1 parent 8b525e0 commit fea7f0d

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

packages/tracing/src/Tracer.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ class Tracer implements ClassThatTraces {
2525
this.provider = new ProviderService();
2626
}
2727

28-
public captureAWS<T>(aws: T): void | T {
29-
if (this.tracingEnabled === false) return;
28+
public captureAWS<T>(aws: T): T {
29+
if (this.tracingEnabled === false) return aws;
3030

3131
return this.provider.captureAWS(aws);
3232
}
3333

34-
public captureAWSClient<T>(service: T): void | T {
35-
if (this.tracingEnabled === false) return;
34+
public captureAWSClient<T>(service: T): T {
35+
if (this.tracingEnabled === false) return service;
3636

3737
return this.provider.captureAWSClient(service);
3838
}
3939

40-
public captureAWSv3Client<T>(service: T): void | T {
41-
if (this.tracingEnabled === false) return;
40+
public captureAWSv3Client<T>(service: T): T {
41+
if (this.tracingEnabled === false) return service;
4242

4343
return this.provider.captureAWSv3Client(service);
4444
}
@@ -59,7 +59,7 @@ class Tracer implements ClassThatTraces {
5959
result = await originalMethod?.apply(this, [ event, context, callback ]);
6060
this.addResponseAsMetadata(result, context.functionName);
6161
} catch (error) {
62-
this.addErrorAsMetadata(error);
62+
this.addErrorAsMetadata(error as Error);
6363
// TODO: should this error be thrown?? If thrown we get a ERR_UNHANDLED_REJECTION. If not aren't we are basically catching a Customer error?
6464
// throw error;
6565
} finally {
@@ -85,8 +85,13 @@ class Tracer implements ClassThatTraces {
8585
};
8686
}
8787

88-
public getSegment(): Segment | Subsegment | undefined {
89-
return this.provider.getSegment();
88+
public getSegment(): Segment | Subsegment {
89+
const segment = this.provider.getSegment();
90+
if (segment === undefined) {
91+
throw new Error('Failed to get the current sub/segment from the context.');
92+
}
93+
94+
return segment;
9095
}
9196

9297
public static isColdStart(): boolean {
@@ -131,7 +136,9 @@ class Tracer implements ClassThatTraces {
131136

132137
private addErrorAsMetadata(error: Error): void {
133138
const subsegment = this.getSegment();
134-
if (this.captureError === false || subsegment === undefined || this.tracingEnabled === false) {
139+
if (this.captureError === false) {
140+
subsegment.addErrorFlag();
141+
135142
return;
136143
}
137144

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ describe('Class: Tracer', () => {
5252
}).toThrow('Failed to get the current sub/segment from the context.');
5353

5454
});
55+
56+
test('when called outside of a namespace or without parent segment, it throws an error', () => {
57+
58+
// Prepare
59+
const tracer: Tracer = new Tracer();
60+
jest.spyOn(tracer.provider, 'getSegment').mockImplementation(() => undefined);
61+
62+
// Act / Assess
63+
expect(() => {
64+
tracer.getSegment();
65+
}).toThrow('Failed to get the current sub/segment from the context.');
66+
67+
});
5568

5669
test('when called within a namespace, it returns the parent segment', () => {
5770

@@ -426,6 +439,7 @@ describe('Class: Tracer', () => {
426439
setContextMissingStrategy(() => null);
427440
const captureAsyncFuncSpy = jest.spyOn(tracer.provider, 'captureAsyncFunc');
428441
const addErrorSpy = jest.spyOn(newSubsegment, 'addError');
442+
const addErrorFlagSpy = jest.spyOn(newSubsegment, 'addErrorFlag');
429443
class Lambda implements LambdaInterface {
430444

431445
@tracer.captureLambdaHanlder()
@@ -446,6 +460,7 @@ describe('Class: Tracer', () => {
446460
name: '## foo-bar-function',
447461
}));
448462
expect('cause' in newSubsegment).toBe(false);
463+
expect(addErrorFlagSpy).toHaveBeenCalledTimes(1);
449464
expect(addErrorSpy).toHaveBeenCalledTimes(0);
450465

451466
delete process.env.POWERTOOLS_TRACER_CAPTURE_ERROR;
@@ -494,7 +509,7 @@ describe('Class: Tracer', () => {
494509
const newSubsegmentSecondInvocation: Segment | Subsegment | undefined = new Subsegment('## foo-bar-function');
495510
jest.spyOn(tracer.provider, 'getSegment')
496511
.mockImplementationOnce(() => newSubsegmentFirstInvocation)
497-
.mockImplementationOnce(() => newSubsegmentSecondInvocation);
512+
.mockImplementation(() => newSubsegmentSecondInvocation);
498513
setContextMissingStrategy(() => null);
499514
const captureAsyncFuncSpy = jest.spyOn(tracer.provider, 'captureAsyncFunc');
500515
const addAnnotationSpy = jest.spyOn(tracer, 'putAnnotation');
@@ -668,5 +683,4 @@ describe('Class: Tracer', () => {
668683
});
669684

670685
});
671-
672686
});

0 commit comments

Comments
 (0)