@@ -11,7 +11,6 @@ import {
1111 normalizeRequestArgs ,
1212 RequestMethod ,
1313 RequestMethodArgs ,
14- RequestOptions ,
1514} from './utils/http' ;
1615
1716const NODE_VERSION = parseSemver ( process . versions . node ) ;
@@ -92,13 +91,16 @@ function _createWrappedRequestMethodFactory(
9291) : WrappedRequestMethodFactory {
9392 return function wrappedRequestMethodFactory ( originalRequestMethod : OriginalRequestMethod ) : WrappedRequestMethod {
9493 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+
9597 const requestArgs = normalizeRequestArgs ( args ) ;
9698 const requestOptions = requestArgs [ 0 ] ;
9799 const requestUrl = extractUrl ( requestOptions ) ;
98100
99101 // we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original method
100102 if ( isSentryRequest ( requestUrl ) ) {
101- return originalRequestMethod . apply ( this , requestArgs ) ;
103+ return originalRequestMethod . apply ( httpModule , requestArgs ) ;
102104 }
103105
104106 let span : Span | undefined ;
@@ -112,30 +114,40 @@ function _createWrappedRequestMethodFactory(
112114 description : `${ requestOptions . method || 'GET' } ${ requestUrl } ` ,
113115 op : 'request' ,
114116 } ) ;
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 } ;
116121 }
117122 }
118123
119124 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
120125 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 ;
123130 if ( breadcrumbsEnabled ) {
124- addRequestBreadcrumb ( 'response' , requestUrl , this , res ) ;
131+ addRequestBreadcrumb ( 'response' , requestUrl , req , res ) ;
125132 }
126133 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 ) ;
129138 span . finish ( ) ;
130139 }
131140 } )
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+
133145 if ( breadcrumbsEnabled ) {
134- addRequestBreadcrumb ( 'error' , requestUrl , this ) ;
146+ addRequestBreadcrumb ( 'error' , requestUrl , req ) ;
135147 }
136148 if ( tracingEnabled && span ) {
137149 span . setHttpStatus ( 500 ) ;
138- cleanSpanDescription ( requestOptions , this , span ) ;
150+ span . description = cleanSpanDescription ( span . description , requestOptions , req ) ;
139151 span . finish ( ) ;
140152 }
141153 } ) ;
@@ -146,7 +158,7 @@ function _createWrappedRequestMethodFactory(
146158/**
147159 * Captures Breadcrumb based on provided request/response pair
148160 */
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 {
150162 if ( ! getCurrentHub ( ) . getIntegration ( Http ) ) {
151163 return ;
152164 }
@@ -168,16 +180,3 @@ function addRequestBreadcrumb(event: string, url: string, req: http.IncomingMess
168180 } ,
169181 ) ;
170182}
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