11/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22/* eslint-disable max-lines */
3- import { getCurrentHub } from '@sentry/core' ;
4- import { Event , Integration , Severity } from '@sentry/types' ;
3+ import { BreadcrumbHint , EventProcessor , Hub , Integration , Severity } from '@sentry/types' ;
54import {
65 addInstrumentationHandler ,
76 getEventDescription ,
@@ -11,6 +10,12 @@ import {
1110 safeJoin ,
1211} from '@sentry/utils' ;
1312
13+ const CONSOLE = 'console' ;
14+ const FETCH = 'fetch' ;
15+ const HISTORY = 'history' ;
16+ const DOM = 'dom' ;
17+ const XHR = 'xhr' ;
18+
1419/** JSDoc */
1520interface BreadcrumbsOptions {
1621 console : boolean ;
@@ -21,9 +26,10 @@ interface BreadcrumbsOptions {
2126 xhr : boolean ;
2227}
2328
29+ const global = getGlobalObject < Window > ( ) ;
30+
2431/**
2532 * Default Breadcrumbs instrumentations
26- * TODO: Deprecated - with v6, this will be renamed to `Instrument`
2733 */
2834export class Breadcrumbs implements Integration {
2935 /**
@@ -54,26 +60,6 @@ export class Breadcrumbs implements Integration {
5460 } ;
5561 }
5662
57- /**
58- * Create a breadcrumb of `sentry` from the events themselves
59- */
60- public addSentryBreadcrumb ( event : Event ) : void {
61- if ( ! this . _options . sentry ) {
62- return ;
63- }
64- getCurrentHub ( ) . addBreadcrumb (
65- {
66- category : `sentry.${ event . type === 'transaction' ? 'transaction' : 'event' } ` ,
67- event_id : event . event_id ,
68- level : event . level ,
69- message : getEventDescription ( event ) ,
70- } ,
71- {
72- event,
73- } ,
74- ) ;
75- }
76-
7763 /**
7864 * Instrument browser built-ins w/ breadcrumb capturing
7965 * - Console API
@@ -82,59 +68,55 @@ export class Breadcrumbs implements Integration {
8268 * - Fetch API
8369 * - History API
8470 */
85- public setupOnce ( ) : void {
71+ public setupOnce ( addGlobalEventProcessor : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
72+ const hub = getCurrentHub ( ) ;
73+
74+ addGlobalEventProcessor ( event => {
75+ if ( this . _options . sentry ) {
76+ hub . addBreadcrumb (
77+ {
78+ category : `sentry.${ event . type === 'transaction' ? 'transaction' : 'event' } ` ,
79+ event_id : event . event_id ,
80+ level : event . level ,
81+ message : getEventDescription ( event ) ,
82+ } ,
83+ {
84+ event,
85+ } ,
86+ ) ;
87+ }
88+ return event ;
89+ } ) ;
90+
8691 if ( this . _options . console ) {
87- addInstrumentationHandler ( {
88- callback : ( ...args ) => {
89- this . _consoleBreadcrumb ( ...args ) ;
90- } ,
91- type : 'console' ,
92- } ) ;
92+ _addConsoleBreadcrumbs ( hub ) ;
9393 }
9494 if ( this . _options . dom ) {
95- addInstrumentationHandler ( {
96- callback : ( ...args ) => {
97- this . _domBreadcrumb ( ...args ) ;
98- } ,
99- type : 'dom' ,
100- } ) ;
95+ _addDomBreadcrumbs ( hub , this . _options . dom ) ;
10196 }
10297 if ( this . _options . xhr ) {
103- addInstrumentationHandler ( {
104- callback : ( ...args ) => {
105- this . _xhrBreadcrumb ( ...args ) ;
106- } ,
107- type : 'xhr' ,
108- } ) ;
98+ _addXhrBreadcrumbs ( hub ) ;
10999 }
110100 if ( this . _options . fetch ) {
111- addInstrumentationHandler ( {
112- callback : ( ...args ) => {
113- this . _fetchBreadcrumb ( ...args ) ;
114- } ,
115- type : 'fetch' ,
116- } ) ;
101+ _addFetchBreadcrumbs ( hub ) ;
117102 }
118103 if ( this . _options . history ) {
119- addInstrumentationHandler ( {
120- callback : ( ...args ) => {
121- this . _historyBreadcrumb ( ...args ) ;
122- } ,
123- type : 'history' ,
124- } ) ;
104+ _addHistoryBreadcrumbs ( hub ) ;
125105 }
126106 }
107+ }
127108
128- /**
129- * Creates breadcrumbs from console API calls
130- */
109+ /**
110+ * Creates breadcrumbs from console API calls
111+ */
112+ function _addConsoleBreadcrumbs ( hub : Hub ) : void {
131113 // eslint-disable-next-line @typescript-eslint/no-explicit-any
132- private _consoleBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
114+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
133115 const breadcrumb = {
134- category : 'console' ,
116+ category : CONSOLE ,
135117 data : {
136118 arguments : handlerData . args ,
137- logger : 'console' ,
119+ logger : CONSOLE ,
138120 } ,
139121 level : Severity . fromString ( handlerData . level ) ,
140122 message : safeJoin ( handlerData . args , ' ' ) ,
@@ -150,19 +132,21 @@ export class Breadcrumbs implements Integration {
150132 }
151133 }
152134
153- getCurrentHub ( ) . addBreadcrumb ( breadcrumb , {
135+ hub . addBreadcrumb ( breadcrumb , {
154136 input : handlerData . args ,
155137 level : handlerData . level ,
156138 } ) ;
157- }
139+ } , CONSOLE ) ;
140+ }
158141
159- /**
160- * Creates breadcrumbs from DOM API calls
161- */
142+ /**
143+ * Creates breadcrumbs from DOM API calls
144+ */
145+ function _addDomBreadcrumbs ( hub : Hub , dom : BreadcrumbsOptions [ 'dom' ] ) : void {
162146 // eslint-disable-next-line @typescript-eslint/no-explicit-any
163- private _domBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
147+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
164148 let target ;
165- let keyAttrs = typeof this . _options . dom === 'object' ? this . _options . dom . serializeAttribute : undefined ;
149+ let keyAttrs = typeof dom === 'object' ? dom . serializeAttribute : undefined ;
166150
167151 if ( typeof keyAttrs === 'string' ) {
168152 keyAttrs = [ keyAttrs ] ;
@@ -181,7 +165,7 @@ export class Breadcrumbs implements Integration {
181165 return ;
182166 }
183167
184- getCurrentHub ( ) . addBreadcrumb (
168+ hub . addBreadcrumb (
185169 {
186170 category : `ui.${ handlerData . name } ` ,
187171 message : target ,
@@ -192,13 +176,15 @@ export class Breadcrumbs implements Integration {
192176 global : handlerData . global ,
193177 } ,
194178 ) ;
195- }
179+ } , DOM ) ;
180+ }
196181
197- /**
198- * Creates breadcrumbs from XHR API calls
199- */
182+ /**
183+ * Creates breadcrumbs from XHR API calls
184+ */
185+ function _addXhrBreadcrumbs ( hub : Hub ) : void {
200186 // eslint-disable-next-line @typescript-eslint/no-explicit-any
201- private _xhrBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
187+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
202188 if ( handlerData . endTimestamp ) {
203189 // We only capture complete, non-sentry requests
204190 if ( handlerData . xhr . __sentry_own_request__ ) {
@@ -207,9 +193,9 @@ export class Breadcrumbs implements Integration {
207193
208194 const { method, url, status_code, body } = handlerData . xhr . __sentry_xhr__ || { } ;
209195
210- getCurrentHub ( ) . addBreadcrumb (
196+ hub . addBreadcrumb (
211197 {
212- category : 'xhr' ,
198+ category : XHR ,
213199 data : {
214200 method,
215201 url,
@@ -222,65 +208,53 @@ export class Breadcrumbs implements Integration {
222208 input : body ,
223209 } ,
224210 ) ;
225-
226- return ;
227211 }
228- }
212+ } , XHR ) ;
213+ }
229214
230- /**
231- * Creates breadcrumbs from fetch API calls
232- */
215+ /**
216+ * Creates breadcrumbs from fetch API calls
217+ */
218+ function _addFetchBreadcrumbs ( hub : Hub ) : void {
233219 // eslint-disable-next-line @typescript-eslint/no-explicit-any
234- private _fetchBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
220+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
235221 // We only capture complete fetch requests
236- if ( ! handlerData . endTimestamp ) {
222+ // We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests)
223+ if (
224+ ! handlerData . endTimestamp ||
225+ ( handlerData . fetchData . url . match ( / s e n t r y _ k e y / ) && handlerData . fetchData . method === 'POST' )
226+ ) {
237227 return ;
238228 }
239229
240- if ( handlerData . fetchData . url . match ( / s e n t r y _ k e y / ) && handlerData . fetchData . method === 'POST' ) {
241- // We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests)
242- return ;
243- }
230+ const breadcrumb = {
231+ category : FETCH ,
232+ data : handlerData . fetchData ,
233+ level : Severity . Error ,
234+ type : 'http' ,
235+ } ;
236+ const breadcrumbHint = {
237+ input : handlerData . args ,
238+ } as BreadcrumbHint ;
244239
245240 if ( handlerData . error ) {
246- getCurrentHub ( ) . addBreadcrumb (
247- {
248- category : 'fetch' ,
249- data : handlerData . fetchData ,
250- level : Severity . Error ,
251- type : 'http' ,
252- } ,
253- {
254- data : handlerData . error ,
255- input : handlerData . args ,
256- } ,
257- ) ;
241+ breadcrumbHint . data = handlerData . error ;
242+ hub . addBreadcrumb ( breadcrumb , breadcrumbHint ) ;
258243 } else {
259- getCurrentHub ( ) . addBreadcrumb (
260- {
261- category : 'fetch' ,
262- data : {
263- ...handlerData . fetchData ,
264- status_code : handlerData . response . status ,
265- } ,
266- type : 'http' ,
267- } ,
268- {
269- input : handlerData . args ,
270- response : handlerData . response ,
271- } ,
272- ) ;
244+ breadcrumb . data . status_code = handlerData . response . status ;
245+ breadcrumbHint . response = handlerData . response ;
246+ hub . addBreadcrumb ( breadcrumb , breadcrumbHint ) ;
273247 }
274- }
248+ } , FETCH ) ;
249+ }
275250
276- /**
277- * Creates breadcrumbs from history API calls
278- */
251+ /**
252+ * Creates breadcrumbs from history API calls
253+ */
254+ function _addHistoryBreadcrumbs ( hub : Hub ) : void {
279255 // eslint-disable-next-line @typescript-eslint/no-explicit-any
280- private _historyBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
281- const global = getGlobalObject < Window > ( ) ;
282- let from = handlerData . from ;
283- let to = handlerData . to ;
256+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
257+ let { from, to } = handlerData ;
284258 const parsedLoc = parseUrl ( global . location . href ) ;
285259 let parsedFrom = parseUrl ( from ) ;
286260 const parsedTo = parseUrl ( to ) ;
@@ -292,19 +266,20 @@ export class Breadcrumbs implements Integration {
292266
293267 // Use only the path component of the URL if the URL matches the current
294268 // document (almost all the time when using pushState)
295- if ( parsedLoc . protocol === parsedTo . protocol && parsedLoc . host === parsedTo . host ) {
269+ const parsedLocProtocol = parsedLoc . protocol ;
270+ if ( parsedLocProtocol === parsedTo . protocol && parsedLoc . host === parsedTo . host ) {
296271 to = parsedTo . relative ;
297272 }
298- if ( parsedLoc . protocol === parsedFrom . protocol && parsedLoc . host === parsedFrom . host ) {
273+ if ( parsedLocProtocol === parsedFrom . protocol && parsedLoc . host === parsedFrom . host ) {
299274 from = parsedFrom . relative ;
300275 }
301276
302- getCurrentHub ( ) . addBreadcrumb ( {
277+ hub . addBreadcrumb ( {
303278 category : 'navigation' ,
304279 data : {
305280 from,
306281 to,
307282 } ,
308283 } ) ;
309- }
284+ } , HISTORY ) ;
310285}
0 commit comments