@@ -27,12 +27,13 @@ export interface UndiciOptions {
27
27
* Defaults to true
28
28
*/
29
29
breadcrumbs : boolean ;
30
+ /**
31
+ * Function determining whether or not to create spans to track outgoing requests to the given URL.
32
+ * By default, spans will be created for all outgoing requests.
33
+ */
34
+ shouldCreateSpanForRequest : ( url : string ) => boolean ;
30
35
}
31
36
32
- const DEFAULT_UNDICI_OPTIONS : UndiciOptions = {
33
- breadcrumbs : true ,
34
- } ;
35
-
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
@@ -60,8 +61,8 @@ export class Undici implements Integration {
60
61
61
62
public constructor ( _options : Partial < UndiciOptions > = { } ) {
62
63
this . _options = {
63
- ... DEFAULT_UNDICI_OPTIONS ,
64
- ... _options ,
64
+ breadcrumbs : _options . breadcrumbs === undefined ? true : _options . breadcrumbs ,
65
+ shouldCreateSpanForRequest : _options . shouldCreateSpanForRequest || ( ( ) => true ) ,
65
66
} ;
66
67
}
67
68
@@ -88,6 +89,11 @@ export class Undici implements Integration {
88
89
89
90
// https://github.com/nodejs/undici/blob/e6fc80f809d1217814c044f52ed40ef13f21e43c/docs/api/DiagnosticsChannel.md
90
91
ds . subscribe ( ChannelName . RequestCreate , message => {
92
+ const hub = getCurrentHub ( ) ;
93
+ if ( ! hub . getIntegration ( Undici ) ) {
94
+ return ;
95
+ }
96
+
91
97
const { request } = message as RequestCreateMessage ;
92
98
93
99
const url = new URL ( request . path , request . origin ) ;
@@ -97,20 +103,14 @@ export class Undici implements Integration {
97
103
return ;
98
104
}
99
105
100
- const hub = getCurrentHub ( ) ;
101
106
const client = hub . getClient < NodeClient > ( ) ;
102
107
const scope = hub . getScope ( ) ;
103
108
104
109
const activeSpan = scope . getSpan ( ) ;
105
110
106
111
if ( activeSpan && client ) {
107
112
const clientOptions = client . getOptions ( ) ;
108
-
109
- // eslint-disable-next-line deprecation/deprecation
110
- const shouldCreateSpan = clientOptions . shouldCreateSpanForRequest
111
- ? // eslint-disable-next-line deprecation/deprecation
112
- clientOptions . shouldCreateSpanForRequest ( stringUrl )
113
- : true ;
113
+ const shouldCreateSpan = this . _options . shouldCreateSpanForRequest ( stringUrl ) ;
114
114
115
115
if ( shouldCreateSpan ) {
116
116
const data : Record < string , unknown > = { } ;
@@ -129,10 +129,8 @@ export class Undici implements Integration {
129
129
} ) ;
130
130
request . __sentry__ = span ;
131
131
132
- // eslint-disable-next-line deprecation/deprecation
133
132
const shouldPropagate = clientOptions . tracePropagationTargets
134
- ? // eslint-disable-next-line deprecation/deprecation
135
- stringMatchesSomePattern ( stringUrl , clientOptions . tracePropagationTargets )
133
+ ? stringMatchesSomePattern ( stringUrl , clientOptions . tracePropagationTargets )
136
134
: true ;
137
135
138
136
if ( shouldPropagate ) {
@@ -150,6 +148,11 @@ export class Undici implements Integration {
150
148
} ) ;
151
149
152
150
ds . subscribe ( ChannelName . RequestEnd , message => {
151
+ const hub = getCurrentHub ( ) ;
152
+ if ( ! hub . getIntegration ( Undici ) ) {
153
+ return ;
154
+ }
155
+
153
156
const { request, response } = message as RequestEndMessage ;
154
157
155
158
const url = new URL ( request . path , request . origin ) ;
@@ -166,7 +169,7 @@ export class Undici implements Integration {
166
169
}
167
170
168
171
if ( this . _options . breadcrumbs ) {
169
- getCurrentHub ( ) . addBreadcrumb (
172
+ hub . addBreadcrumb (
170
173
{
171
174
category : 'http' ,
172
175
data : {
@@ -186,6 +189,11 @@ export class Undici implements Integration {
186
189
} ) ;
187
190
188
191
ds . subscribe ( ChannelName . RequestError , message => {
192
+ const hub = getCurrentHub ( ) ;
193
+ if ( ! hub . getIntegration ( Undici ) ) {
194
+ return ;
195
+ }
196
+
189
197
const { request } = message as RequestErrorMessage ;
190
198
191
199
const url = new URL ( request . path , request . origin ) ;
@@ -202,7 +210,7 @@ export class Undici implements Integration {
202
210
}
203
211
204
212
if ( this . _options . breadcrumbs ) {
205
- getCurrentHub ( ) . addBreadcrumb (
213
+ hub . addBreadcrumb (
206
214
{
207
215
category : 'http' ,
208
216
data : {
0 commit comments