@@ -62,7 +62,32 @@ describe('Middy middlewares', () => {
62
62
63
63
} ) ;
64
64
65
- test ( 'when used as decorator while POWERTOOLS_TRACER_CAPTURE_RESPONSE is set to false, it does not capture the response as metadata' , async ( ) => {
65
+ test ( 'when used while tracing is disabled, even if the handler throws an error, it does nothing' , async ( ) => {
66
+
67
+ // Prepare
68
+ const tracer : Tracer = new Tracer ( { enabled : false } ) ;
69
+ const setSegmentSpy = jest . spyOn ( tracer . provider , 'setSegment' ) . mockImplementation ( ) ;
70
+ const getSegmentSpy = jest . spyOn ( tracer . provider , 'getSegment' )
71
+ . mockImplementationOnce ( ( ) => new Segment ( 'facade' , process . env . _X_AMZN_TRACE_ID || null ) )
72
+ . mockImplementationOnce ( ( ) => new Subsegment ( '## foo-bar-function' ) ) ;
73
+ const lambdaHandler : Handler = async ( _event : unknown , _context : Context ) => {
74
+ throw new Error ( 'Exception thrown!' ) ;
75
+ } ;
76
+ const handler = middy ( lambdaHandler ) . use ( captureLambdaHandler ( tracer ) ) ;
77
+ const context = Object . assign ( { } , mockContext ) ;
78
+
79
+ // Act
80
+ try {
81
+ await handler ( { } , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
82
+ } catch ( error ) {
83
+ // Assess
84
+ expect ( setSegmentSpy ) . toHaveBeenCalledTimes ( 0 ) ;
85
+ expect ( getSegmentSpy ) . toHaveBeenCalledTimes ( 0 ) ;
86
+ }
87
+
88
+ } ) ;
89
+
90
+ test ( 'when used while POWERTOOLS_TRACER_CAPTURE_RESPONSE is set to false, it does not capture the response as metadata' , async ( ) => {
66
91
67
92
// Prepare
68
93
process . env . POWERTOOLS_TRACER_CAPTURE_RESPONSE = 'false' ;
@@ -87,7 +112,7 @@ describe('Middy middlewares', () => {
87
112
88
113
} ) ;
89
114
90
- test ( 'when used as decorator and with standard config, it captures the response as metadata' , async ( ) => {
115
+ test ( 'when used with standard config, it captures the response as metadata' , async ( ) => {
91
116
92
117
// Prepare
93
118
const tracer : Tracer = new Tracer ( ) ;
@@ -122,7 +147,7 @@ describe('Middy middlewares', () => {
122
147
123
148
} ) ;
124
149
125
- test ( 'when used as decorator while POWERTOOLS_TRACER_CAPTURE_ERROR is set to false, it does not capture the exceptions' , async ( ) => {
150
+ test ( 'when used while POWERTOOLS_TRACER_CAPTURE_ERROR is set to false, it does not capture the exceptions' , async ( ) => {
126
151
127
152
// Prepare
128
153
process . env . POWERTOOLS_TRACER_CAPTURE_ERROR = 'false' ;
@@ -140,20 +165,94 @@ describe('Middy middlewares', () => {
140
165
const context = Object . assign ( { } , mockContext ) ;
141
166
142
167
// Act
143
- await handler ( { } , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
168
+ try {
169
+ await handler ( { } , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
170
+ } catch ( error ) {
171
+ // Assess
172
+ expect ( setSegmentSpy ) . toHaveBeenCalledTimes ( 1 ) ;
173
+ expect ( setSegmentSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
174
+ name : '## foo-bar-function' ,
175
+ } ) ) ;
176
+ expect ( 'cause' in newSubsegment ) . toBe ( false ) ;
177
+ expect ( addErrorFlagSpy ) . toHaveBeenCalledTimes ( 1 ) ;
178
+ expect ( addErrorSpy ) . toHaveBeenCalledTimes ( 0 ) ;
179
+ }
180
+
181
+ delete process . env . POWERTOOLS_TRACER_CAPTURE_ERROR ;
182
+
183
+ } ) ;
184
+
185
+ } ) ;
144
186
187
+ test ( 'when used with standard config, it captures the exception correctly' , async ( ) => {
188
+
189
+ // Prepare
190
+ const tracer : Tracer = new Tracer ( ) ;
191
+ const newSubsegment : Segment | Subsegment | undefined = new Subsegment ( '## foo-bar-function' ) ;
192
+ const setSegmentSpy = jest . spyOn ( tracer . provider , 'setSegment' ) . mockImplementation ( ) ;
193
+ jest . spyOn ( tracer . provider , 'getSegment' ) . mockImplementation ( ( ) => newSubsegment ) ;
194
+ setContextMissingStrategy ( ( ) => null ) ;
195
+ const addErrorSpy = jest . spyOn ( newSubsegment , 'addError' ) ;
196
+ const lambdaHandler : Handler = async ( _event : unknown , _context : Context ) => {
197
+ throw new Error ( 'Exception thrown!' ) ;
198
+ } ;
199
+ const handler = middy ( lambdaHandler ) . use ( captureLambdaHandler ( tracer ) ) ;
200
+ const context = Object . assign ( { } , mockContext ) ;
201
+
202
+ // Act
203
+ try {
204
+ await handler ( { } , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
205
+ } catch ( error ) {
145
206
// Assess
146
207
expect ( setSegmentSpy ) . toHaveBeenCalledTimes ( 1 ) ;
147
208
expect ( setSegmentSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
148
209
name : '## foo-bar-function' ,
149
210
} ) ) ;
150
- expect ( 'cause' in newSubsegment ) . toBe ( false ) ;
151
- expect ( addErrorFlagSpy ) . toHaveBeenCalledTimes ( 1 ) ;
152
- expect ( addErrorSpy ) . toHaveBeenCalledTimes ( 0 ) ;
211
+ expect ( 'cause' in newSubsegment ) . toBe ( true ) ;
212
+ expect ( addErrorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
213
+ expect ( addErrorSpy ) . toHaveBeenCalledWith ( new Error ( 'Exception thrown!' ) , false ) ;
214
+ }
153
215
154
- delete process . env . POWERTOOLS_TRACER_CAPTURE_ERROR ;
216
+ } ) ;
155
217
218
+ test ( 'when used with standard config, it annotates ColdStart correctly' , async ( ) => {
219
+
220
+ // Prepare
221
+ const tracer : Tracer = new Tracer ( ) ;
222
+ const newSubsegmentFirstInvocation : Segment | Subsegment | undefined = new Subsegment ( '## foo-bar-function' ) ;
223
+ const newSubsegmentSecondInvocation : Segment | Subsegment | undefined = new Subsegment ( '## foo-bar-function' ) ;
224
+ const setSegmentSpy = jest . spyOn ( tracer . provider , 'setSegment' ) . mockImplementation ( ) ;
225
+ jest . spyOn ( tracer . provider , 'getSegment' )
226
+ . mockImplementationOnce ( ( ) => newSubsegmentFirstInvocation )
227
+ . mockImplementation ( ( ) => newSubsegmentSecondInvocation ) ;
228
+ setContextMissingStrategy ( ( ) => null ) ;
229
+ const addAnnotationSpy = jest . spyOn ( tracer , 'putAnnotation' ) ;
230
+ const lambdaHandler : Handler = async ( _event : unknown , _context : Context ) => ( {
231
+ foo : 'bar'
156
232
} ) ;
233
+ const handler = middy ( lambdaHandler ) . use ( captureLambdaHandler ( tracer ) ) ;
234
+ const context = Object . assign ( { } , mockContext ) ;
235
+
236
+ // Act
237
+ await handler ( { } , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
238
+ await handler ( { } , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
239
+
240
+ // Assess
241
+ expect ( setSegmentSpy ) . toHaveBeenCalledTimes ( 2 ) ;
242
+ expect ( setSegmentSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
243
+ name : '## foo-bar-function' ,
244
+ } ) ) ;
245
+ expect ( addAnnotationSpy ) . toHaveBeenCalledTimes ( 1 ) ;
246
+ expect ( addAnnotationSpy ) . toHaveBeenCalledWith ( 'ColdStart' , true ) ;
247
+ expect ( newSubsegmentFirstInvocation ) . toEqual ( expect . objectContaining ( {
248
+ name : '## foo-bar-function' ,
249
+ annotations : {
250
+ 'ColdStart' : true ,
251
+ }
252
+ } ) ) ;
253
+ expect ( newSubsegmentSecondInvocation ) . toEqual ( expect . objectContaining ( {
254
+ name : '## foo-bar-function'
255
+ } ) ) ;
157
256
158
257
} ) ;
159
258
0 commit comments