@@ -27,12 +27,13 @@ export interface UndiciOptions {
2727 * Defaults to true
2828 */
2929 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 ;
3035}
3136
32- const DEFAULT_UNDICI_OPTIONS : UndiciOptions = {
33- breadcrumbs : true ,
34- } ;
35-
3637// Please note that you cannot use `console.log` to debug the callbacks registered to the `diagnostics_channel` API.
3738// To debug, you can use `writeFileSync` to write to a file:
3839// https://nodejs.org/api/async_hooks.html#printing-in-asynchook-callbacks
@@ -60,8 +61,8 @@ export class Undici implements Integration {
6061
6162 public constructor ( _options : Partial < UndiciOptions > = { } ) {
6263 this . _options = {
63- ... DEFAULT_UNDICI_OPTIONS ,
64- ... _options ,
64+ breadcrumbs : _options . breadcrumbs === undefined ? true : _options . breadcrumbs ,
65+ shouldCreateSpanForRequest : _options . shouldCreateSpanForRequest || ( ( ) => true ) ,
6566 } ;
6667 }
6768
@@ -88,6 +89,11 @@ export class Undici implements Integration {
8889
8990 // https://github.com/nodejs/undici/blob/e6fc80f809d1217814c044f52ed40ef13f21e43c/docs/api/DiagnosticsChannel.md
9091 ds . subscribe ( ChannelName . RequestCreate , message => {
92+ const hub = getCurrentHub ( ) ;
93+ if ( ! hub . getIntegration ( Undici ) ) {
94+ return ;
95+ }
96+
9197 const { request } = message as RequestCreateMessage ;
9298
9399 const url = new URL ( request . path , request . origin ) ;
@@ -97,20 +103,14 @@ export class Undici implements Integration {
97103 return ;
98104 }
99105
100- const hub = getCurrentHub ( ) ;
101106 const client = hub . getClient < NodeClient > ( ) ;
102107 const scope = hub . getScope ( ) ;
103108
104109 const activeSpan = scope . getSpan ( ) ;
105110
106111 if ( activeSpan && client ) {
107112 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 ) ;
114114
115115 if ( shouldCreateSpan ) {
116116 const data : Record < string , unknown > = { } ;
@@ -129,10 +129,8 @@ export class Undici implements Integration {
129129 } ) ;
130130 request . __sentry__ = span ;
131131
132- // eslint-disable-next-line deprecation/deprecation
133132 const shouldPropagate = clientOptions . tracePropagationTargets
134- ? // eslint-disable-next-line deprecation/deprecation
135- stringMatchesSomePattern ( stringUrl , clientOptions . tracePropagationTargets )
133+ ? stringMatchesSomePattern ( stringUrl , clientOptions . tracePropagationTargets )
136134 : true ;
137135
138136 if ( shouldPropagate ) {
@@ -150,6 +148,11 @@ export class Undici implements Integration {
150148 } ) ;
151149
152150 ds . subscribe ( ChannelName . RequestEnd , message => {
151+ const hub = getCurrentHub ( ) ;
152+ if ( ! hub . getIntegration ( Undici ) ) {
153+ return ;
154+ }
155+
153156 const { request, response } = message as RequestEndMessage ;
154157
155158 const url = new URL ( request . path , request . origin ) ;
@@ -166,7 +169,7 @@ export class Undici implements Integration {
166169 }
167170
168171 if ( this . _options . breadcrumbs ) {
169- getCurrentHub ( ) . addBreadcrumb (
172+ hub . addBreadcrumb (
170173 {
171174 category : 'http' ,
172175 data : {
@@ -186,6 +189,11 @@ export class Undici implements Integration {
186189 } ) ;
187190
188191 ds . subscribe ( ChannelName . RequestError , message => {
192+ const hub = getCurrentHub ( ) ;
193+ if ( ! hub . getIntegration ( Undici ) ) {
194+ return ;
195+ }
196+
189197 const { request } = message as RequestErrorMessage ;
190198
191199 const url = new URL ( request . path , request . origin ) ;
@@ -202,7 +210,7 @@ export class Undici implements Integration {
202210 }
203211
204212 if ( this . _options . breadcrumbs ) {
205- getCurrentHub ( ) . addBreadcrumb (
213+ hub . addBreadcrumb (
206214 {
207215 category : 'http' ,
208216 data : {
0 commit comments