Skip to content

Commit 57bf02d

Browse files
committed
Exposed addErrorAsMetadata
1 parent 162c1d7 commit 57bf02d

File tree

5 files changed

+84
-39
lines changed

5 files changed

+84
-39
lines changed

docs/core/tracer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
9595
tracer.addResponseAsMetadata(res, context.functionName);
9696
} catch (err) {
9797
// Add the error as metadata
98-
handlerSegment.addError(err as Error, false);
98+
tracer.addErrorAsMetadata(err as Error);
9999
}
100100

101101
// Close subsegment (the AWS Lambda one is closed automatically)

packages/tracing/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const handler = async (_event: any, context: any) => {
8080
tracer.addResponseAsMetadata(res, context.functionName);
8181
} catch (err) {
8282
// Add the error as metadata
83-
handlerSegment.addError(err as Error, false);
83+
tracer.addErrorAsMetadata(err as Error);
8484
}
8585

8686
// Close subsegment (the AWS Lambda one is closed automatically)

packages/tracing/src/Tracer.ts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
9393
* tracer.addResponseAsMetadata(res, context.functionName);
9494
* } catch (err) {
9595
* // Add the error as metadata
96-
* handlerSegment.addError(err as Error, false);
96+
* tracer.addErrorAsMetadata(err as Error);
9797
* }
9898
*
9999
* // Close subsegment (the AWS Lambda one is closed automatically)
@@ -125,6 +125,28 @@ class Tracer implements TracerInterface {
125125
this.provider = new ProviderService();
126126
}
127127

128+
/**
129+
* Add an error to the current segment or subsegment as metadata.
130+
*
131+
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-errors
132+
*
133+
* @param error - Error to serialize as metadata
134+
*/
135+
public addErrorAsMetadata(error: Error): void {
136+
if (this.tracingEnabled === false) {
137+
return;
138+
}
139+
140+
const subsegment = this.getSegment();
141+
if (this.captureError === false) {
142+
subsegment.addErrorFlag();
143+
144+
return;
145+
}
146+
147+
subsegment.addError(error, false);
148+
}
149+
128150
/**
129151
* Add an data to the current segment or subsegment as metadata.
130152
*
@@ -385,18 +407,6 @@ class Tracer implements TracerInterface {
385407

386408
return segment;
387409
}
388-
389-
/**
390-
* Get the current value of the `captureError` property.
391-
*
392-
* You can use this method during manual instrumentation to determine
393-
* if tracer should be capturing errors.
394-
*
395-
* @returns captureError - `true` if errors should be captured, `false` otherwise.
396-
*/
397-
public isCaptureErrorEnabled(): boolean {
398-
return this.captureError;
399-
}
400410

401411
/**
402412
* Retrieve the current value of `ColdStart`.
@@ -522,25 +532,6 @@ class Tracer implements TracerInterface {
522532
public setSegment(segment: Segment | Subsegment): void {
523533
return this.provider.setSegment(segment);
524534
}
525-
526-
/**
527-
* Add an error to the current segment or subsegment as metadata.
528-
* Used internally by decoratorators and middlewares.
529-
*
530-
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-errors
531-
*
532-
* @param error - Error to serialize as metadata
533-
*/
534-
private addErrorAsMetadata(error: Error): void {
535-
const subsegment = this.getSegment();
536-
if (this.captureError === false) {
537-
subsegment.addErrorFlag();
538-
539-
return;
540-
}
541-
542-
subsegment.addError(error, false);
543-
}
544535

545536
/**
546537
* Add ColdStart annotation to the current segment or subsegment.

packages/tracing/src/middleware/middy.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@ const captureLambdaHandler = (target: Tracer): middy.MiddlewareObj => {
4949
const captureLambdaHandlerError = async (request: middy.Request): Promise<void> => {
5050
if (target.isTracingEnabled()) {
5151
const subsegment = target.getSegment();
52-
if (target.isCaptureErrorEnabled() === false) {
53-
subsegment?.addErrorFlag();
54-
} else {
55-
subsegment?.addError(request.error as Error, false);
56-
}
52+
target.addErrorAsMetadata(request.error as Error);
53+
5754
// TODO: should this error be thrown?? I.e. should we stop the event flow & return?
5855
// throw request.error;
5956

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,63 @@ describe('Class: Tracer', () => {
8787

8888
});
8989

90+
describe('Method: addErrorAsMetadata', () => {
91+
92+
test('when called while tracing is disabled, it does nothing', () => {
93+
94+
// Prepare
95+
const tracer: Tracer = new Tracer({ enabled: false });
96+
const getSegmentSpy = jest.spyOn(tracer, 'getSegment');
97+
98+
// Act
99+
tracer.addErrorAsMetadata(new Error('foo'));
100+
101+
// Assess
102+
expect(getSegmentSpy).toBeCalledTimes(0);
103+
104+
});
105+
106+
test('when called while POWERTOOLS_TRACER_CAPTURE_ERROR is set to false, it does not capture the error', () => {
107+
108+
// Prepare
109+
process.env.POWERTOOLS_TRACER_CAPTURE_ERROR = 'false';
110+
const tracer: Tracer = new Tracer();
111+
const subsegment = new Subsegment(`## ${context.functionName}`);
112+
jest.spyOn(tracer, 'getSegment').mockImplementation(() => subsegment);
113+
const addErrorFlagSpy = jest.spyOn(subsegment, 'addErrorFlag');
114+
const addErrorSpy = jest.spyOn(subsegment, 'addError');
115+
116+
// Act
117+
tracer.addErrorAsMetadata(new Error('foo'));
118+
119+
// Assess
120+
expect(addErrorFlagSpy).toBeCalledTimes(1);
121+
expect(addErrorSpy).toBeCalledTimes(0);
122+
delete process.env.POWERTOOLS_TRACER_CAPTURE_ERROR;
123+
124+
});
125+
126+
test('when called with default config, it calls subsegment.addError correctly', () => {
127+
128+
// Prepare
129+
const tracer: Tracer = new Tracer();
130+
const subsegment = new Subsegment(`## ${context.functionName}`);
131+
jest.spyOn(tracer, 'getSegment').mockImplementation(() => subsegment);
132+
const addErrorFlagSpy = jest.spyOn(subsegment, 'addErrorFlag');
133+
const addErrorSpy = jest.spyOn(subsegment, 'addError');
134+
135+
// Act
136+
tracer.addErrorAsMetadata(new Error('foo'));
137+
138+
// Assess
139+
expect(addErrorFlagSpy).toBeCalledTimes(0);
140+
expect(addErrorSpy).toBeCalledTimes(1);
141+
expect(addErrorSpy).toBeCalledWith(new Error('foo'), false);
142+
143+
});
144+
145+
});
146+
90147
describe('Method: isColdStart', () => {
91148

92149
test('when called, it returns false the first time and always true after that', () => {

0 commit comments

Comments
 (0)