@@ -3,8 +3,9 @@ import type { EventProcessor, Integration } from '@sentry/types';
3
3
import {
4
4
dynamicRequire ,
5
5
dynamicSamplingContextToSentryBaggageHeader ,
6
+ getSanitizedUrlString ,
7
+ parseUrl ,
6
8
stringMatchesSomePattern ,
7
- stripUrlQueryAndFragment ,
8
9
} from '@sentry/utils' ;
9
10
import { LRUMap } from 'lru_map' ;
10
11
@@ -36,6 +37,15 @@ export interface UndiciOptions {
36
37
// Please note that you cannot use `console.log` to debug the callbacks registered to the `diagnostics_channel` API.
37
38
// To debug, you can use `writeFileSync` to write to a file:
38
39
// https://nodejs.org/api/async_hooks.html#printing-in-asynchook-callbacks
40
+ //
41
+ // import { writeFileSync } from 'fs';
42
+ // import { format } from 'util';
43
+ //
44
+ // function debug(...args: any): void {
45
+ // // Use a function like this one when debugging inside an AsyncHook callback
46
+ // // @ts -ignore any
47
+ // writeFileSync('log.out', `${format(...args)}\n`, { flag: 'a' });
48
+ // }
39
49
40
50
/**
41
51
* Instruments outgoing HTTP requests made with the `undici` package via
@@ -113,8 +123,8 @@ export class Undici implements Integration {
113
123
114
124
const { request } = message as RequestCreateMessage ;
115
125
116
- const url = new URL ( request . path , request . origin ) ;
117
- const stringUrl = url . toString ( ) ;
126
+ const stringUrl = request . origin ? request . origin . toString ( ) + request . path : request . path ;
127
+ const url = parseUrl ( stringUrl ) ;
118
128
119
129
if ( isSentryRequest ( stringUrl ) || request . __sentry__ !== undefined ) {
120
130
return ;
@@ -133,16 +143,15 @@ export class Undici implements Integration {
133
143
const data : Record < string , unknown > = {
134
144
'http.method' : method ,
135
145
} ;
136
- const params = url . searchParams . toString ( ) ;
137
- if ( params ) {
138
- data [ 'http.query' ] = `?${ params } ` ;
146
+ if ( url . search ) {
147
+ data [ 'http.query' ] = url . search ;
139
148
}
140
149
if ( url . hash ) {
141
150
data [ 'http.fragment' ] = url . hash ;
142
151
}
143
152
const span = activeSpan . startChild ( {
144
153
op : 'http.client' ,
145
- description : `${ method } ${ stripUrlQueryAndFragment ( stringUrl ) } ` ,
154
+ description : `${ method } ${ getSanitizedUrlString ( url ) } ` ,
146
155
data,
147
156
} ) ;
148
157
request . __sentry__ = span ;
@@ -184,8 +193,7 @@ export class Undici implements Integration {
184
193
185
194
const { request, response } = message as RequestEndMessage ;
186
195
187
- const url = new URL ( request . path , request . origin ) ;
188
- const stringUrl = url . toString ( ) ;
196
+ const stringUrl = request . origin ? request . origin . toString ( ) + request . path : request . path ;
189
197
190
198
if ( isSentryRequest ( stringUrl ) ) {
191
199
return ;
@@ -225,8 +233,7 @@ export class Undici implements Integration {
225
233
226
234
const { request } = message as RequestErrorMessage ;
227
235
228
- const url = new URL ( request . path , request . origin ) ;
229
- const stringUrl = url . toString ( ) ;
236
+ const stringUrl = request . origin ? request . origin . toString ( ) + request . path : request . path ;
230
237
231
238
if ( isSentryRequest ( stringUrl ) ) {
232
239
return ;
0 commit comments