@@ -114,14 +114,25 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
114
114
this . _onOutgoingRequestFinish ( request , response ) ;
115
115
} ) ;
116
116
117
+ // When an error happens, we still want to have a breadcrumb
118
+ // In this case, `http.client.response.finish` is not triggered
119
+ subscribe ( 'http.client.request.error' , data => {
120
+ const request = ( data as { request : http . ClientRequest } ) . request ;
121
+ // there is no response object here, we only have access to request & error :(
122
+
123
+ this . _onOutgoingRequestFinish ( request , undefined ) ;
124
+ } ) ;
125
+
117
126
return [ ] ;
118
127
}
119
128
120
129
/**
121
130
* This is triggered when an outgoing request finishes.
122
131
* It has access to the final request and response objects.
123
132
*/
124
- private _onOutgoingRequestFinish ( request : http . ClientRequest , response : http . IncomingMessage ) : void {
133
+ private _onOutgoingRequestFinish ( request : http . ClientRequest , response ?: http . IncomingMessage ) : void {
134
+ DEBUG_BUILD && logger . log ( INSTRUMENTATION_NAME , 'Handling outgoing request finish' ) ;
135
+
125
136
const _breadcrumbs = this . getConfig ( ) . breadcrumbs ;
126
137
const breadCrumbsEnabled = typeof _breadcrumbs === 'undefined' ? true : _breadcrumbs ;
127
138
const options = getRequestOptions ( request ) ;
@@ -148,6 +159,8 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
148
159
return ;
149
160
}
150
161
162
+ DEBUG_BUILD && logger . log ( INSTRUMENTATION_NAME , 'Patching server.emit' ) ;
163
+
151
164
// eslint-disable-next-line @typescript-eslint/no-this-alias
152
165
const instrumentation = this ;
153
166
const { ignoreIncomingRequestBody } = instrumentation . getConfig ( ) ;
@@ -159,7 +172,7 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
159
172
return target . apply ( thisArg , args ) ;
160
173
}
161
174
162
- DEBUG_BUILD && logger . log ( 'http instrumentation for incoming request') ;
175
+ DEBUG_BUILD && logger . log ( INSTRUMENTATION_NAME , 'isolating incoming request') ;
163
176
164
177
const isolationScope = getIsolationScope ( ) . clone ( ) ;
165
178
const request = args [ 1 ] as http . IncomingMessage ;
@@ -222,10 +235,10 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
222
235
}
223
236
224
237
/** Add a breadcrumb for outgoing requests. */
225
- function addRequestBreadcrumb ( request : http . ClientRequest , response : http . IncomingMessage ) : void {
238
+ function addRequestBreadcrumb ( request : http . ClientRequest , response : http . IncomingMessage | undefined ) : void {
226
239
const data = getBreadcrumbData ( request ) ;
227
240
228
- const statusCode = response . statusCode ;
241
+ const statusCode = response ? .statusCode ;
229
242
const level = getBreadcrumbLogLevelFromHttpStatusCode ( statusCode ) ;
230
243
231
244
addBreadcrumb (
@@ -281,6 +294,8 @@ function patchRequestToCaptureBody(req: http.IncomingMessage, isolationScope: Sc
281
294
let bodyByteLength = 0 ;
282
295
const chunks : Buffer [ ] = [ ] ;
283
296
297
+ DEBUG_BUILD && logger . log ( INSTRUMENTATION_NAME , 'Patching request.on' ) ;
298
+
284
299
/**
285
300
* We need to keep track of the original callbacks, in order to be able to remove listeners again.
286
301
* Since `off` depends on having the exact same function reference passed in, we need to be able to map
@@ -294,11 +309,8 @@ function patchRequestToCaptureBody(req: http.IncomingMessage, isolationScope: Sc
294
309
apply : ( target , thisArg , args : Parameters < typeof req . on > ) => {
295
310
const [ event , listener , ...restArgs ] = args ;
296
311
297
- if ( DEBUG_BUILD ) {
298
- logger . log ( INSTRUMENTATION_NAME , 'Patching request.on' , event ) ;
299
- }
300
-
301
312
if ( event === 'data' ) {
313
+ DEBUG_BUILD && logger . log ( INSTRUMENTATION_NAME , 'Handling request.on' , event ) ;
302
314
const callback = new Proxy ( listener , {
303
315
apply : ( target , thisArg , args : Parameters < typeof listener > ) => {
304
316
try {
0 commit comments