Skip to content

Commit 162c1d7

Browse files
committed
Exposed addResponseAsMetadata
1 parent 35c4c3d commit 162c1d7

File tree

5 files changed

+84
-37
lines changed

5 files changed

+84
-37
lines changed

docs/core/tracer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
9292
try {
9393
res = ...
9494
// Add the response as metadata
95-
tracer.putMetadata(`${context.functionName} response`, res);
95+
tracer.addResponseAsMetadata(res, context.functionName);
9696
} catch (err) {
9797
// Add the error as metadata
9898
handlerSegment.addError(err as Error, false);

packages/tracing/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const handler = async (_event: any, context: any) => {
7777
try {
7878
res = ...
7979
// Add the response as metadata
80-
tracer.putMetadata(`${context.functionName} response`, res);
80+
tracer.addResponseAsMetadata(res, context.functionName);
8181
} catch (err) {
8282
// Add the error as metadata
8383
handlerSegment.addError(err as Error, false);

packages/tracing/src/Tracer.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
9090
* try {
9191
* res = ...
9292
* // Add the response as metadata
93-
* tracer.putMetadata(`${context.functionName} response`, res);
93+
* tracer.addResponseAsMetadata(res, context.functionName);
9494
* } catch (err) {
9595
* // Add the error as metadata
9696
* handlerSegment.addError(err as Error, false);
@@ -125,6 +125,22 @@ class Tracer implements TracerInterface {
125125
this.provider = new ProviderService();
126126
}
127127

128+
/**
129+
* Add an data to the current segment or subsegment as metadata.
130+
*
131+
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-annotations
132+
*
133+
* @param data - Data to serialize as metadata
134+
* @param methodName - Name of the method that is being traced
135+
*/
136+
public addResponseAsMetadata(data?: unknown, methodName?: string): void {
137+
if (data === undefined || this.captureResponse === false || this.tracingEnabled === false) {
138+
return;
139+
}
140+
141+
this.putMetadata(`${methodName} response`, data);
142+
}
143+
128144
/**
129145
* Patch all AWS SDK v2 clients and create traces when your application makes calls to AWS services.
130146
*
@@ -382,18 +398,6 @@ class Tracer implements TracerInterface {
382398
return this.captureError;
383399
}
384400

385-
/**
386-
* Get the current value of the `captureResponse` property.
387-
*
388-
* You can use this method during manual instrumentation to determine
389-
* if tracer should be capturing function responses.
390-
*
391-
* @returns captureResponse - `true` if responses should be captured, `false` otherwise.
392-
*/
393-
public isCaptureResponseEnabled(): boolean {
394-
return this.captureResponse;
395-
}
396-
397401
/**
398402
* Retrieve the current value of `ColdStart`.
399403
*
@@ -537,23 +541,6 @@ class Tracer implements TracerInterface {
537541

538542
subsegment.addError(error, false);
539543
}
540-
541-
/**
542-
* Add an data to the current segment or subsegment as metadata.
543-
* Used internally by decoratorators and middlewares.
544-
*
545-
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-errors
546-
*
547-
* @param data - Data to serialize as metadata
548-
* @param methodName - Name of the method that is being traced
549-
*/
550-
private addResponseAsMetadata(data?: unknown, methodName?: string): void {
551-
if (data === undefined || this.captureResponse === false || this.tracingEnabled === false) {
552-
return;
553-
}
554-
555-
this.putMetadata(`${methodName} response`, data);
556-
}
557544

558545
/**
559546
* 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
@@ -41,11 +41,8 @@ const captureLambdaHandler = (target: Tracer): middy.MiddlewareObj => {
4141
const captureLambdaHandlerAfter = async (request: middy.Request): Promise<void> => {
4242
if (target.isTracingEnabled()) {
4343
const subsegment = target.getSegment();
44-
if (request.response !== undefined && target.isCaptureResponseEnabled() === true) {
45-
target.putMetadata(`${request.context.functionName} response`, request.response);
46-
}
47-
48-
subsegment?.close();
44+
target.addResponseAsMetadata(request.response, request.context.functionName);
45+
subsegment.close();
4946
}
5047
};
5148

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,69 @@ describe('Class: Tracer', () => {
2424
process.env = ENVIRONMENT_VARIABLES;
2525
});
2626

27+
describe('Method: addResponseAsMetadata', () => {
28+
29+
test('when called while tracing is disabled, it does nothing', () => {
30+
31+
// Prepare
32+
const tracer: Tracer = new Tracer({ enabled: false });
33+
const putMetadataSpy = jest.spyOn(tracer, 'putMetadata');
34+
35+
// Act
36+
tracer.addResponseAsMetadata({ foo: 'bar' }, context.functionName);
37+
38+
// Assess
39+
expect(putMetadataSpy).toBeCalledTimes(0);
40+
41+
});
42+
43+
test('when called while POWERTOOLS_TRACER_CAPTURE_RESPONSE is set to false, it does nothing', () => {
44+
45+
// Prepare
46+
process.env.POWERTOOLS_TRACER_CAPTURE_RESPONSE = 'false';
47+
const tracer: Tracer = new Tracer();
48+
const putMetadataSpy = jest.spyOn(tracer, 'putMetadata');
49+
50+
// Act
51+
tracer.addResponseAsMetadata({ foo: 'bar' }, context.functionName);
52+
53+
// Assess
54+
expect(putMetadataSpy).toBeCalledTimes(0);
55+
delete process.env.POWERTOOLS_TRACER_CAPTURE_RESPONSE;
56+
57+
});
58+
59+
test('when called with data equal to undefined, it does nothing', () => {
60+
61+
// Prepare
62+
const tracer: Tracer = new Tracer();
63+
const putMetadataSpy = jest.spyOn(tracer, 'putMetadata');
64+
65+
// Act
66+
tracer.addResponseAsMetadata(undefined, context.functionName);
67+
68+
// Assess
69+
expect(putMetadataSpy).toBeCalledTimes(0);
70+
71+
});
72+
73+
test('when called with default config, it calls tracer.putMetadata correctly', () => {
74+
75+
// Prepare
76+
const tracer: Tracer = new Tracer();
77+
const putMetadataSpy = jest.spyOn(tracer, 'putMetadata').mockImplementation(() => null);
78+
79+
// Act
80+
tracer.addResponseAsMetadata({ foo: 'bar' }, context.functionName);
81+
82+
// Assess
83+
expect(putMetadataSpy).toBeCalledTimes(1);
84+
expect(putMetadataSpy).toBeCalledWith(`${context.functionName} response`, expect.objectContaining({ foo: 'bar' }));
85+
86+
});
87+
88+
});
89+
2790
describe('Method: isColdStart', () => {
2891

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

0 commit comments

Comments
 (0)