@@ -62,7 +62,7 @@ describe('Tracer E2E tests, decorator instrumentation', () => {
62
62
) ;
63
63
testTable . grantWriteData ( fnDecorator ) ;
64
64
65
- const invocationCount = 3 ;
65
+ const invocationCount = 2 ;
66
66
let traceData : EnrichedXRayTraceDocumentParsed [ ] = [ ] ;
67
67
68
68
beforeAll ( async ( ) => {
@@ -99,89 +99,92 @@ describe('Tracer E2E tests, decorator instrumentation', () => {
99
99
'should generate all trace data correctly' ,
100
100
async ( ) => {
101
101
// Assess
102
- for ( let i = 0 ; i < invocationCount ; i ++ ) {
103
- const isColdStart = i === 0 ; // First invocation is a cold start
104
- const shouldThrowAnError = i === invocationCount - 1 ; // Last invocation should throw - we are testing error capture
105
- const mainSubsegment = traceData [ i ] ;
106
- const { subsegments, annotations, metadata } = mainSubsegment ;
107
-
108
- // Check the main segment name
109
- expect ( mainSubsegment . name ) . toBe ( '## index.handler' ) ;
110
-
111
- // Check the subsegments of the main segment
112
- expect ( subsegments . size ) . toBe ( 3 ) ;
113
-
114
- // Check remote call subsegment
115
- expect ( subsegments . has ( 'docs.powertools.aws.dev' ) ) . toBe ( true ) ;
116
- const httpSubsegment = subsegments . get ( 'docs.powertools.aws.dev' ) ;
117
- expect ( httpSubsegment ?. namespace ) . toBe ( 'remote' ) ;
118
- expect ( httpSubsegment ?. http ?. request ?. url ) . toEqual (
119
- 'https://docs.powertools.aws.dev/lambda/typescript/latest/'
120
- ) ;
121
- expect ( httpSubsegment ?. http ?. request ?. method ) . toBe ( 'GET' ) ;
122
- expect ( httpSubsegment ?. http ?. response ?. status ) . toEqual (
123
- expect . any ( Number )
124
- ) ;
125
- expect ( httpSubsegment ?. http ?. response ?. status ) . toEqual (
126
- expect . any ( Number )
127
- ) ;
128
-
129
- // Check the custom subsegment name & metadata
130
- expect ( subsegments . has ( expectedCustomSubSegmentName ) ) . toBe ( true ) ;
131
- expect (
132
- subsegments . get ( expectedCustomSubSegmentName ) ?. metadata
133
- ) . toStrictEqual ( {
134
- Decorator : {
135
- 'myMethod response' : expectedCustomResponseValue ,
136
- } ,
137
- } ) ;
138
-
139
- // Check the other custom subsegment and its subsegments
140
- expect ( subsegments . has ( '### methodNoResponse' ) ) . toBe ( true ) ;
141
- expect (
142
- subsegments . get ( '### methodNoResponse' ) ?. metadata
143
- ) . toBeUndefined ( ) ;
144
- expect (
145
- subsegments . get ( '### methodNoResponse' ) ?. subsegments ?. length
146
- ) . toBe ( 1 ) ;
147
- expect (
148
- subsegments . get ( '### methodNoResponse' ) ?. subsegments ?. [ 0 ] ?. name ===
149
- 'DynamoDB'
150
- ) . toBe ( true ) ;
151
-
152
- // Check the annotations of the main segment
153
- if ( ! annotations ) {
154
- throw new Error ( 'No annotations found on the main segment' ) ;
155
- }
156
- expect ( annotations . ColdStart ) . toEqual ( isColdStart ) ;
157
- expect ( annotations . Service ) . toEqual ( 'Decorator' ) ;
158
- expect ( annotations [ expectedCustomAnnotationKey ] ) . toEqual (
159
- expectedCustomAnnotationValue
160
- ) ;
161
-
162
- // Check the metadata of the main segment
163
- if ( ! metadata ) {
164
- throw new Error ( 'No metadata found on the main segment' ) ;
165
- }
166
- expect ( metadata . Decorator [ expectedCustomMetadataKey ] ) . toEqual (
167
- expectedCustomMetadataValue
168
- ) ;
169
-
170
- // Check the error recording (only on invocations that should throw)
171
- if ( shouldThrowAnError ) {
172
- expect ( mainSubsegment . fault ) . toBe ( true ) ;
173
- expect ( Object . hasOwn ( mainSubsegment , 'cause' ) ) . toBe ( true ) ;
174
- expect ( mainSubsegment . cause ?. exceptions [ 0 ] . message ) . toBe (
175
- expectedCustomErrorMessage
176
- ) ;
177
- // Check the response in the metadata (only on invocations that DON'T throw)
178
- } else {
179
- expect ( metadata . Decorator [ 'index.handler response' ] ) . toEqual (
180
- expectedCustomResponseValue
181
- ) ;
182
- }
102
+ const mainSubsegment = traceData [ 0 ] ;
103
+ const { subsegments, annotations, metadata } = mainSubsegment ;
104
+
105
+ // Check the main segment name
106
+ expect ( mainSubsegment . name ) . toBe ( '## index.handler' ) ;
107
+
108
+ // Check the subsegments of the main segment
109
+ expect ( subsegments . size ) . toBe ( 3 ) ;
110
+
111
+ // Check remote call subsegment
112
+ expect ( subsegments . has ( 'docs.powertools.aws.dev' ) ) . toBe ( true ) ;
113
+ const httpSubsegment = subsegments . get ( 'docs.powertools.aws.dev' ) ;
114
+ expect ( httpSubsegment ?. namespace ) . toBe ( 'remote' ) ;
115
+ expect ( httpSubsegment ?. http ?. request ?. url ) . toEqual (
116
+ 'https://docs.powertools.aws.dev/lambda/typescript/latest/'
117
+ ) ;
118
+ expect ( httpSubsegment ?. http ?. request ?. method ) . toBe ( 'GET' ) ;
119
+ expect ( httpSubsegment ?. http ?. response ?. status ) . toEqual (
120
+ expect . any ( Number )
121
+ ) ;
122
+ expect ( httpSubsegment ?. http ?. response ?. status ) . toEqual (
123
+ expect . any ( Number )
124
+ ) ;
125
+
126
+ // Check the custom subsegment name & metadata
127
+ expect ( subsegments . has ( expectedCustomSubSegmentName ) ) . toBe ( true ) ;
128
+ expect (
129
+ subsegments . get ( expectedCustomSubSegmentName ) ?. metadata
130
+ ) . toStrictEqual ( {
131
+ Decorator : {
132
+ 'myMethod response' : expectedCustomResponseValue ,
133
+ } ,
134
+ } ) ;
135
+
136
+ // Check the other custom subsegment and its subsegments
137
+ expect ( subsegments . has ( '### methodNoResponse' ) ) . toBe ( true ) ;
138
+ expect ( subsegments . get ( '### methodNoResponse' ) ?. metadata ) . toBeUndefined ( ) ;
139
+ expect ( subsegments . get ( '### methodNoResponse' ) ?. subsegments ?. length ) . toBe (
140
+ 1
141
+ ) ;
142
+ expect (
143
+ subsegments . get ( '### methodNoResponse' ) ?. subsegments ?. [ 0 ] ?. name ===
144
+ 'DynamoDB'
145
+ ) . toBe ( true ) ;
146
+
147
+ // Check the annotations of the main segment
148
+ if ( ! annotations ) {
149
+ throw new Error ( 'No annotations found on the main segment' ) ;
183
150
}
151
+ expect ( annotations . ColdStart ) . toEqual ( true ) ;
152
+ expect ( annotations . Service ) . toEqual ( 'Decorator' ) ;
153
+ expect ( annotations [ expectedCustomAnnotationKey ] ) . toEqual (
154
+ expectedCustomAnnotationValue
155
+ ) ;
156
+
157
+ // Check the metadata of the main segment
158
+ if ( ! metadata ) {
159
+ throw new Error ( 'No metadata found on the main segment' ) ;
160
+ }
161
+ expect ( metadata . Decorator [ expectedCustomMetadataKey ] ) . toEqual (
162
+ expectedCustomMetadataValue
163
+ ) ;
164
+
165
+ // Check the response is present in the metadata
166
+ expect ( metadata . Decorator [ 'index.handler response' ] ) . toEqual (
167
+ expectedCustomResponseValue
168
+ ) ;
184
169
} ,
185
170
TEST_CASE_TIMEOUT
186
171
) ;
172
+
173
+ it ( 'should annotate the trace with error data correctly' , ( ) => {
174
+ const mainSubsegment = traceData [ 1 ] ;
175
+ const { annotations } = mainSubsegment ;
176
+
177
+ // Check the annotations of the main segment
178
+ if ( ! annotations ) {
179
+ throw new Error ( 'No annotations found on the main segment' ) ;
180
+ }
181
+ expect ( annotations . ColdStart ) . toEqual ( false ) ;
182
+
183
+ // Check that the main segment has error data
184
+ expect ( mainSubsegment . fault ) . toBe ( true ) ;
185
+ expect ( Object . hasOwn ( mainSubsegment , 'cause' ) ) . toBe ( true ) ;
186
+ expect ( mainSubsegment . cause ?. exceptions [ 0 ] . message ) . toBe (
187
+ expectedCustomErrorMessage
188
+ ) ;
189
+ } ) ;
187
190
} ) ;
0 commit comments