@@ -121,3 +121,82 @@ test('Sends an API route transaction', async ({ baseURL }) => {
121
121
} ) ,
122
122
) ;
123
123
} ) ;
124
+
125
+ test ( 'API route transaction includes nest middleware span. Spans created in and after middleware are nested correctly' , async ( {
126
+ baseURL,
127
+ } ) => {
128
+ const pageloadTransactionEventPromise = waitForTransaction ( 'nestjs' , transactionEvent => {
129
+ return (
130
+ transactionEvent ?. contexts ?. trace ?. op === 'http.server' &&
131
+ transactionEvent ?. transaction === 'GET /test-middleware-instrumentation'
132
+ ) ;
133
+ } ) ;
134
+
135
+ await fetch ( `${ baseURL } /test-middleware-instrumentation` ) ;
136
+
137
+ const transactionEvent = await pageloadTransactionEventPromise ;
138
+
139
+ expect ( transactionEvent ) . toEqual (
140
+ expect . objectContaining ( {
141
+ spans : expect . arrayContaining ( [
142
+ {
143
+ span_id : expect . any ( String ) ,
144
+ trace_id : expect . any ( String ) ,
145
+ data : {
146
+ 'sentry.op' : 'middleware.nestjs' ,
147
+ 'sentry.origin' : 'auto.middleware.nestjs' ,
148
+ } ,
149
+ description : 'ExampleMiddleware' ,
150
+ parent_span_id : expect . any ( String ) ,
151
+ start_timestamp : expect . any ( Number ) ,
152
+ timestamp : expect . any ( Number ) ,
153
+ status : 'ok' ,
154
+ op : 'middleware.nestjs' ,
155
+ origin : 'auto.middleware.nestjs' ,
156
+ } ,
157
+ ] ) ,
158
+ } ) ,
159
+ ) ;
160
+
161
+ const exampleMiddlewareSpan = transactionEvent . spans . find ( span => span . description === 'ExampleMiddleware' ) ;
162
+ const exampleMiddlewareSpanId = exampleMiddlewareSpan ?. span_id ;
163
+
164
+ expect ( transactionEvent ) . toEqual (
165
+ expect . objectContaining ( {
166
+ spans : expect . arrayContaining ( [
167
+ {
168
+ span_id : expect . any ( String ) ,
169
+ trace_id : expect . any ( String ) ,
170
+ data : expect . any ( Object ) ,
171
+ description : 'test-controller-span' ,
172
+ parent_span_id : expect . any ( String ) ,
173
+ start_timestamp : expect . any ( Number ) ,
174
+ timestamp : expect . any ( Number ) ,
175
+ status : 'ok' ,
176
+ origin : 'manual' ,
177
+ } ,
178
+ {
179
+ span_id : expect . any ( String ) ,
180
+ trace_id : expect . any ( String ) ,
181
+ data : expect . any ( Object ) ,
182
+ description : 'test-middleware-span' ,
183
+ parent_span_id : expect . any ( String ) ,
184
+ start_timestamp : expect . any ( Number ) ,
185
+ timestamp : expect . any ( Number ) ,
186
+ status : 'ok' ,
187
+ origin : 'manual' ,
188
+ } ,
189
+ ] ) ,
190
+ } ) ,
191
+ ) ;
192
+
193
+ // verify correct span parent-child relationships
194
+ const testMiddlewareSpan = transactionEvent . spans . find ( span => span . description === 'test-middleware-span' ) ;
195
+ const testControllerSpan = transactionEvent . spans . find ( span => span . description === 'test-controller-span' ) ;
196
+
197
+ // 'ExampleMiddleware' is the parent of 'test-middleware-span'
198
+ expect ( testMiddlewareSpan . parent_span_id ) . toBe ( exampleMiddlewareSpanId ) ;
199
+
200
+ // 'ExampleMiddleware' is NOT the parent of 'test-controller-span'
201
+ expect ( testControllerSpan . parent_span_id ) . not . toBe ( exampleMiddlewareSpanId ) ;
202
+ } ) ;
0 commit comments