@@ -14,7 +14,7 @@ import { LRUMap } from 'lru_map';
14
14
15
15
import type { NodeClient } from '../client' ;
16
16
import type { RequestMethod , RequestMethodArgs } from './utils/http' ;
17
- import { cleanSpanDescription , extractUrl , isSentryRequest , normalizeRequestArgs } from './utils/http' ;
17
+ import { cleanSpanDescription , extractRawUrl , extractUrl , isSentryRequest , normalizeRequestArgs } from './utils/http' ;
18
18
19
19
const NODE_VERSION = parseSemver ( process . versions . node ) ;
20
20
@@ -129,6 +129,16 @@ type OriginalRequestMethod = RequestMethod;
129
129
type WrappedRequestMethod = RequestMethod ;
130
130
type WrappedRequestMethodFactory = ( original : OriginalRequestMethod ) => WrappedRequestMethod ;
131
131
132
+ /**
133
+ * See https://develop.sentry.dev/sdk/data-handling/#structuring-data
134
+ */
135
+ type RequestSpanData = {
136
+ url : string ;
137
+ method : string ;
138
+ 'http.fragment' ?: string ;
139
+ 'http.query' ?: string ;
140
+ } ;
141
+
132
142
/**
133
143
* Function which creates a function which creates wrapped versions of internal `request` and `get` calls within `http`
134
144
* and `https` modules. (NB: Not a typo - this is a creator^2!)
@@ -180,6 +190,8 @@ function _createWrappedRequestMethodFactory(
180
190
return function wrappedMethod ( this : unknown , ...args : RequestMethodArgs ) : http . ClientRequest {
181
191
const requestArgs = normalizeRequestArgs ( httpModule , args ) ;
182
192
const requestOptions = requestArgs [ 0 ] ;
193
+ // eslint-disable-next-line deprecation/deprecation
194
+ const rawRequestUrl = extractRawUrl ( requestOptions ) ;
183
195
const requestUrl = extractUrl ( requestOptions ) ;
184
196
185
197
// we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original method
@@ -192,16 +204,30 @@ function _createWrappedRequestMethodFactory(
192
204
193
205
const scope = getCurrentHub ( ) . getScope ( ) ;
194
206
195
- if ( scope && tracingOptions && shouldCreateSpan ( requestUrl ) ) {
207
+ const requestSpanData : RequestSpanData = {
208
+ url : requestUrl ,
209
+ method : requestOptions . method || 'GET' ,
210
+ } ;
211
+ if ( requestOptions . hash ) {
212
+ // strip leading "#"
213
+ requestSpanData [ 'http.fragment' ] = requestOptions . hash . substring ( 1 ) ;
214
+ }
215
+ if ( requestOptions . search ) {
216
+ // strip leading "?"
217
+ requestSpanData [ 'http.query' ] = requestOptions . search . substring ( 1 ) ;
218
+ }
219
+
220
+ if ( scope && tracingOptions && shouldCreateSpan ( rawRequestUrl ) ) {
196
221
parentSpan = scope . getSpan ( ) ;
197
222
198
223
if ( parentSpan ) {
199
224
requestSpan = parentSpan . startChild ( {
200
- description : `${ requestOptions . method || 'GET' } ${ requestUrl } ` ,
225
+ description : `${ requestSpanData . method } ${ requestSpanData . url } ` ,
201
226
op : 'http.client' ,
227
+ data : requestSpanData ,
202
228
} ) ;
203
229
204
- if ( shouldAttachTraceData ( requestUrl ) ) {
230
+ if ( shouldAttachTraceData ( rawRequestUrl ) ) {
205
231
const sentryTraceHeader = requestSpan . toTraceparent ( ) ;
206
232
__DEBUG_BUILD__ &&
207
233
logger . log (
@@ -253,7 +279,7 @@ function _createWrappedRequestMethodFactory(
253
279
// eslint-disable-next-line @typescript-eslint/no-this-alias
254
280
const req = this ;
255
281
if ( breadcrumbsEnabled ) {
256
- addRequestBreadcrumb ( 'response' , requestUrl , req , res ) ;
282
+ addRequestBreadcrumb ( 'response' , requestSpanData , req , res ) ;
257
283
}
258
284
if ( requestSpan ) {
259
285
if ( res . statusCode ) {
@@ -268,7 +294,7 @@ function _createWrappedRequestMethodFactory(
268
294
const req = this ;
269
295
270
296
if ( breadcrumbsEnabled ) {
271
- addRequestBreadcrumb ( 'error' , requestUrl , req ) ;
297
+ addRequestBreadcrumb ( 'error' , requestSpanData , req ) ;
272
298
}
273
299
if ( requestSpan ) {
274
300
requestSpan . setHttpStatus ( 500 ) ;
@@ -283,7 +309,12 @@ function _createWrappedRequestMethodFactory(
283
309
/**
284
310
* Captures Breadcrumb based on provided request/response pair
285
311
*/
286
- function addRequestBreadcrumb ( event : string , url : string , req : http . ClientRequest , res ?: http . IncomingMessage ) : void {
312
+ function addRequestBreadcrumb (
313
+ event : string ,
314
+ requestSpanData : RequestSpanData ,
315
+ req : http . ClientRequest ,
316
+ res ?: http . IncomingMessage ,
317
+ ) : void {
287
318
if ( ! getCurrentHub ( ) . getIntegration ( Http ) ) {
288
319
return ;
289
320
}
@@ -294,7 +325,7 @@ function addRequestBreadcrumb(event: string, url: string, req: http.ClientReques
294
325
data : {
295
326
method : req . method ,
296
327
status_code : res && res . statusCode ,
297
- url ,
328
+ ... requestSpanData ,
298
329
} ,
299
330
type : 'http' ,
300
331
} ,
0 commit comments