@@ -11,7 +11,6 @@ import {
11
11
normalizeRequestArgs ,
12
12
RequestMethod ,
13
13
RequestMethodArgs ,
14
- RequestOptions ,
15
14
} from './utils/http' ;
16
15
17
16
const NODE_VERSION = parseSemver ( process . versions . node ) ;
@@ -92,13 +91,16 @@ function _createWrappedRequestMethodFactory(
92
91
) : WrappedRequestMethodFactory {
93
92
return function wrappedRequestMethodFactory ( originalRequestMethod : OriginalRequestMethod ) : WrappedRequestMethod {
94
93
return function wrappedMethod ( this : typeof http | typeof https , ...args : RequestMethodArgs ) : http . ClientRequest {
94
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
95
+ const httpModule = this ;
96
+
95
97
const requestArgs = normalizeRequestArgs ( args ) ;
96
98
const requestOptions = requestArgs [ 0 ] ;
97
99
const requestUrl = extractUrl ( requestOptions ) ;
98
100
99
101
// we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original method
100
102
if ( isSentryRequest ( requestUrl ) ) {
101
- return originalRequestMethod . apply ( this , requestArgs ) ;
103
+ return originalRequestMethod . apply ( httpModule , requestArgs ) ;
102
104
}
103
105
104
106
let span : Span | undefined ;
@@ -112,30 +114,40 @@ function _createWrappedRequestMethodFactory(
112
114
description : `${ requestOptions . method || 'GET' } ${ requestUrl } ` ,
113
115
op : 'request' ,
114
116
} ) ;
115
- addSentryTraceHeader ( span , requestOptions ) ;
117
+
118
+ const sentryTraceHeader = span . toTraceparent ( ) ;
119
+ logger . log ( `[Tracing] Adding sentry-trace header to outgoing request: ${ sentryTraceHeader } ` ) ;
120
+ requestOptions . headers = { ...requestOptions . headers , 'sentry-trace' : sentryTraceHeader } ;
116
121
}
117
122
}
118
123
119
124
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
120
125
return originalRequestMethod
121
- . apply ( this , requestArgs )
122
- . once ( 'response' , function ( this : http . IncomingMessage , res : http . ServerResponse ) : void {
126
+ . apply ( httpModule , requestArgs )
127
+ . once ( 'response' , function ( this : http . ClientRequest , res : http . IncomingMessage ) : void {
128
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
129
+ const req = this ;
123
130
if ( breadcrumbsEnabled ) {
124
- addRequestBreadcrumb ( 'response' , requestUrl , this , res ) ;
131
+ addRequestBreadcrumb ( 'response' , requestUrl , req , res ) ;
125
132
}
126
133
if ( tracingEnabled && span ) {
127
- span . setHttpStatus ( res . statusCode ) ;
128
- cleanSpanDescription ( requestOptions , this , span ) ;
134
+ if ( res . statusCode ) {
135
+ span . setHttpStatus ( res . statusCode ) ;
136
+ }
137
+ span . description = cleanSpanDescription ( span . description , requestOptions , req ) ;
129
138
span . finish ( ) ;
130
139
}
131
140
} )
132
- . once ( 'error' , function ( this : http . IncomingMessage ) : void {
141
+ . once ( 'error' , function ( this : http . ClientRequest ) : void {
142
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
143
+ const req = this ;
144
+
133
145
if ( breadcrumbsEnabled ) {
134
- addRequestBreadcrumb ( 'error' , requestUrl , this ) ;
146
+ addRequestBreadcrumb ( 'error' , requestUrl , req ) ;
135
147
}
136
148
if ( tracingEnabled && span ) {
137
149
span . setHttpStatus ( 500 ) ;
138
- cleanSpanDescription ( requestOptions , this , span ) ;
150
+ span . description = cleanSpanDescription ( span . description , requestOptions , req ) ;
139
151
span . finish ( ) ;
140
152
}
141
153
} ) ;
@@ -146,7 +158,7 @@ function _createWrappedRequestMethodFactory(
146
158
/**
147
159
* Captures Breadcrumb based on provided request/response pair
148
160
*/
149
- function addRequestBreadcrumb ( event : string , url : string , req : http . IncomingMessage , res ?: http . ServerResponse ) : void {
161
+ function addRequestBreadcrumb ( event : string , url : string , req : http . ClientRequest , res ?: http . IncomingMessage ) : void {
150
162
if ( ! getCurrentHub ( ) . getIntegration ( Http ) ) {
151
163
return ;
152
164
}
@@ -168,16 +180,3 @@ function addRequestBreadcrumb(event: string, url: string, req: http.IncomingMess
168
180
} ,
169
181
) ;
170
182
}
171
-
172
- /**
173
- * Add the `sentry-trace` header to the request options passed to `http(s).request()` and `http(s).get()`.
174
- *
175
- * @param span The span representing this outgoing request.
176
- * @param requestArgs The arguments passed to `http(s).request()` or `http(s).get()`
177
- *
178
- */
179
- function addSentryTraceHeader ( span : Span , requestOptions : RequestOptions ) : void {
180
- const headerValue = span . toTraceparent ( ) ;
181
- logger . log ( `[Tracing] Adding sentry-trace header to outgoing request: ${ headerValue } ` ) ;
182
- requestOptions . headers = { ...requestOptions . headers , 'sentry-trace' : headerValue } ;
183
- }
0 commit comments