1
1
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2
2
/* 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' ;
5
4
import {
6
5
addInstrumentationHandler ,
7
6
getEventDescription ,
@@ -11,6 +10,12 @@ import {
11
10
safeJoin ,
12
11
} from '@sentry/utils' ;
13
12
13
+ const CONSOLE = 'console' ;
14
+ const FETCH = 'fetch' ;
15
+ const HISTORY = 'history' ;
16
+ const DOM = 'dom' ;
17
+ const XHR = 'xhr' ;
18
+
14
19
/** JSDoc */
15
20
interface BreadcrumbsOptions {
16
21
console : boolean ;
@@ -21,9 +26,10 @@ interface BreadcrumbsOptions {
21
26
xhr : boolean ;
22
27
}
23
28
29
+ const global = getGlobalObject < Window > ( ) ;
30
+
24
31
/**
25
32
* Default Breadcrumbs instrumentations
26
- * TODO: Deprecated - with v6, this will be renamed to `Instrument`
27
33
*/
28
34
export class Breadcrumbs implements Integration {
29
35
/**
@@ -54,26 +60,6 @@ export class Breadcrumbs implements Integration {
54
60
} ;
55
61
}
56
62
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
-
77
63
/**
78
64
* Instrument browser built-ins w/ breadcrumb capturing
79
65
* - Console API
@@ -82,59 +68,55 @@ export class Breadcrumbs implements Integration {
82
68
* - Fetch API
83
69
* - History API
84
70
*/
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
+
86
91
if ( this . _options . console ) {
87
- addInstrumentationHandler ( {
88
- callback : ( ...args ) => {
89
- this . _consoleBreadcrumb ( ...args ) ;
90
- } ,
91
- type : 'console' ,
92
- } ) ;
92
+ _addConsoleBreadcrumbs ( hub ) ;
93
93
}
94
94
if ( this . _options . dom ) {
95
- addInstrumentationHandler ( {
96
- callback : ( ...args ) => {
97
- this . _domBreadcrumb ( ...args ) ;
98
- } ,
99
- type : 'dom' ,
100
- } ) ;
95
+ _addDomBreadcrumbs ( hub , this . _options . dom ) ;
101
96
}
102
97
if ( this . _options . xhr ) {
103
- addInstrumentationHandler ( {
104
- callback : ( ...args ) => {
105
- this . _xhrBreadcrumb ( ...args ) ;
106
- } ,
107
- type : 'xhr' ,
108
- } ) ;
98
+ _addXhrBreadcrumbs ( hub ) ;
109
99
}
110
100
if ( this . _options . fetch ) {
111
- addInstrumentationHandler ( {
112
- callback : ( ...args ) => {
113
- this . _fetchBreadcrumb ( ...args ) ;
114
- } ,
115
- type : 'fetch' ,
116
- } ) ;
101
+ _addFetchBreadcrumbs ( hub ) ;
117
102
}
118
103
if ( this . _options . history ) {
119
- addInstrumentationHandler ( {
120
- callback : ( ...args ) => {
121
- this . _historyBreadcrumb ( ...args ) ;
122
- } ,
123
- type : 'history' ,
124
- } ) ;
104
+ _addHistoryBreadcrumbs ( hub ) ;
125
105
}
126
106
}
107
+ }
127
108
128
- /**
129
- * Creates breadcrumbs from console API calls
130
- */
109
+ /**
110
+ * Creates breadcrumbs from console API calls
111
+ */
112
+ function _addConsoleBreadcrumbs ( hub : Hub ) : void {
131
113
// eslint-disable-next-line @typescript-eslint/no-explicit-any
132
- private _consoleBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
114
+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
133
115
const breadcrumb = {
134
- category : 'console' ,
116
+ category : CONSOLE ,
135
117
data : {
136
118
arguments : handlerData . args ,
137
- logger : 'console' ,
119
+ logger : CONSOLE ,
138
120
} ,
139
121
level : Severity . fromString ( handlerData . level ) ,
140
122
message : safeJoin ( handlerData . args , ' ' ) ,
@@ -150,19 +132,21 @@ export class Breadcrumbs implements Integration {
150
132
}
151
133
}
152
134
153
- getCurrentHub ( ) . addBreadcrumb ( breadcrumb , {
135
+ hub . addBreadcrumb ( breadcrumb , {
154
136
input : handlerData . args ,
155
137
level : handlerData . level ,
156
138
} ) ;
157
- }
139
+ } , CONSOLE ) ;
140
+ }
158
141
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 {
162
146
// eslint-disable-next-line @typescript-eslint/no-explicit-any
163
- private _domBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
147
+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
164
148
let target ;
165
- let keyAttrs = typeof this . _options . dom === 'object' ? this . _options . dom . serializeAttribute : undefined ;
149
+ let keyAttrs = typeof dom === 'object' ? dom . serializeAttribute : undefined ;
166
150
167
151
if ( typeof keyAttrs === 'string' ) {
168
152
keyAttrs = [ keyAttrs ] ;
@@ -181,7 +165,7 @@ export class Breadcrumbs implements Integration {
181
165
return ;
182
166
}
183
167
184
- getCurrentHub ( ) . addBreadcrumb (
168
+ hub . addBreadcrumb (
185
169
{
186
170
category : `ui.${ handlerData . name } ` ,
187
171
message : target ,
@@ -192,13 +176,15 @@ export class Breadcrumbs implements Integration {
192
176
global : handlerData . global ,
193
177
} ,
194
178
) ;
195
- }
179
+ } , DOM ) ;
180
+ }
196
181
197
- /**
198
- * Creates breadcrumbs from XHR API calls
199
- */
182
+ /**
183
+ * Creates breadcrumbs from XHR API calls
184
+ */
185
+ function _addXhrBreadcrumbs ( hub : Hub ) : void {
200
186
// eslint-disable-next-line @typescript-eslint/no-explicit-any
201
- private _xhrBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
187
+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
202
188
if ( handlerData . endTimestamp ) {
203
189
// We only capture complete, non-sentry requests
204
190
if ( handlerData . xhr . __sentry_own_request__ ) {
@@ -207,9 +193,9 @@ export class Breadcrumbs implements Integration {
207
193
208
194
const { method, url, status_code, body } = handlerData . xhr . __sentry_xhr__ || { } ;
209
195
210
- getCurrentHub ( ) . addBreadcrumb (
196
+ hub . addBreadcrumb (
211
197
{
212
- category : 'xhr' ,
198
+ category : XHR ,
213
199
data : {
214
200
method,
215
201
url,
@@ -222,65 +208,53 @@ export class Breadcrumbs implements Integration {
222
208
input : body ,
223
209
} ,
224
210
) ;
225
-
226
- return ;
227
211
}
228
- }
212
+ } , XHR ) ;
213
+ }
229
214
230
- /**
231
- * Creates breadcrumbs from fetch API calls
232
- */
215
+ /**
216
+ * Creates breadcrumbs from fetch API calls
217
+ */
218
+ function _addFetchBreadcrumbs ( hub : Hub ) : void {
233
219
// eslint-disable-next-line @typescript-eslint/no-explicit-any
234
- private _fetchBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
220
+ addInstrumentationHandler ( ( handlerData : { [ key : string ] : any } ) : void => {
235
221
// 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
+ ) {
237
227
return ;
238
228
}
239
229
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 ;
244
239
245
240
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 ) ;
258
243
} 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 ) ;
273
247
}
274
- }
248
+ } , FETCH ) ;
249
+ }
275
250
276
- /**
277
- * Creates breadcrumbs from history API calls
278
- */
251
+ /**
252
+ * Creates breadcrumbs from history API calls
253
+ */
254
+ function _addHistoryBreadcrumbs ( hub : Hub ) : void {
279
255
// 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 ;
284
258
const parsedLoc = parseUrl ( global . location . href ) ;
285
259
let parsedFrom = parseUrl ( from ) ;
286
260
const parsedTo = parseUrl ( to ) ;
@@ -292,19 +266,20 @@ export class Breadcrumbs implements Integration {
292
266
293
267
// Use only the path component of the URL if the URL matches the current
294
268
// 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 ) {
296
271
to = parsedTo . relative ;
297
272
}
298
- if ( parsedLoc . protocol === parsedFrom . protocol && parsedLoc . host === parsedFrom . host ) {
273
+ if ( parsedLocProtocol === parsedFrom . protocol && parsedLoc . host === parsedFrom . host ) {
299
274
from = parsedFrom . relative ;
300
275
}
301
276
302
- getCurrentHub ( ) . addBreadcrumb ( {
277
+ hub . addBreadcrumb ( {
303
278
category : 'navigation' ,
304
279
data : {
305
280
from,
306
281
to,
307
282
} ,
308
283
} ) ;
309
- }
284
+ } , HISTORY ) ;
310
285
}
0 commit comments