|
| 1 | +import { addBreadcrumb, getClient } from '@sentry/core'; |
| 2 | +import type { Event as SentryEvent, HandlerDataConsole, HandlerDataFetch, Integration } from '@sentry/types'; |
| 3 | +import type { FetchBreadcrumbData, FetchBreadcrumbHint } from '@sentry/types/build/types/breadcrumb'; |
| 4 | +import { |
| 5 | + addConsoleInstrumentationHandler, |
| 6 | + addFetchInstrumentationHandler, |
| 7 | + getEventDescription, |
| 8 | + safeJoin, |
| 9 | + severityLevelFromString, |
| 10 | +} from '@sentry/utils'; |
| 11 | + |
| 12 | +interface BreadcrumbsOptions { |
| 13 | + console: boolean; |
| 14 | + fetch: boolean; |
| 15 | + sentry: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +/** maxStringLength gets capped to prevent 100 breadcrumbs exceeding 1MB event payload size */ |
| 19 | +const MAX_ALLOWED_STRING_LENGTH = 1024; |
| 20 | + |
| 21 | +/** |
| 22 | + * Default Breadcrumbs instrumentations |
| 23 | + * TODO: Deprecated - with v6, this will be renamed to `Instrument` |
| 24 | + */ |
| 25 | +export class Breadcrumbs implements Integration { |
| 26 | + /** |
| 27 | + * @inheritDoc |
| 28 | + */ |
| 29 | + public static id: string = 'Breadcrumbs'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritDoc |
| 33 | + */ |
| 34 | + public name: string; |
| 35 | + |
| 36 | + /** |
| 37 | + * Options of the breadcrumbs integration. |
| 38 | + */ |
| 39 | + // This field is public, because we use it in the browser client to check if the `sentry` option is enabled. |
| 40 | + public readonly options: Readonly<BreadcrumbsOptions>; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritDoc |
| 44 | + */ |
| 45 | + public constructor(options?: Partial<BreadcrumbsOptions>) { |
| 46 | + this.name = Breadcrumbs.id; |
| 47 | + this.options = { |
| 48 | + console: true, |
| 49 | + fetch: true, |
| 50 | + sentry: true, |
| 51 | + ...options, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Instrument browser built-ins w/ breadcrumb capturing |
| 57 | + * - Console API |
| 58 | + * - Fetch API |
| 59 | + */ |
| 60 | + public setupOnce(): void { |
| 61 | + if (this.options.console) { |
| 62 | + addConsoleInstrumentationHandler(_consoleBreadcrumb); |
| 63 | + } |
| 64 | + if (this.options.fetch) { |
| 65 | + addFetchInstrumentationHandler(_fetchBreadcrumb); |
| 66 | + } |
| 67 | + if (this.options.sentry) { |
| 68 | + const client = getClient(); |
| 69 | + client && client.on && client.on('beforeSendEvent', addSentryBreadcrumb); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Adds a breadcrumb for Sentry events or transactions if this option is enabled. |
| 76 | + */ |
| 77 | +function addSentryBreadcrumb(event: SentryEvent): void { |
| 78 | + addBreadcrumb( |
| 79 | + { |
| 80 | + category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`, |
| 81 | + event_id: event.event_id, |
| 82 | + level: event.level, |
| 83 | + message: getEventDescription(event), |
| 84 | + }, |
| 85 | + { |
| 86 | + event, |
| 87 | + }, |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Creates breadcrumbs from console API calls |
| 93 | + */ |
| 94 | +function _consoleBreadcrumb(handlerData: HandlerDataConsole): void { |
| 95 | + const breadcrumb = { |
| 96 | + category: 'console', |
| 97 | + data: { |
| 98 | + arguments: handlerData.args, |
| 99 | + logger: 'console', |
| 100 | + }, |
| 101 | + level: severityLevelFromString(handlerData.level), |
| 102 | + message: safeJoin(handlerData.args, ' '), |
| 103 | + }; |
| 104 | + |
| 105 | + if (handlerData.level === 'assert') { |
| 106 | + if (handlerData.args[0] === false) { |
| 107 | + breadcrumb.message = `Assertion failed: ${safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`; |
| 108 | + breadcrumb.data.arguments = handlerData.args.slice(1); |
| 109 | + } else { |
| 110 | + // Don't capture a breadcrumb for passed assertions |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + addBreadcrumb(breadcrumb, { |
| 116 | + input: handlerData.args, |
| 117 | + level: handlerData.level, |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Creates breadcrumbs from fetch API calls |
| 123 | + */ |
| 124 | +function _fetchBreadcrumb(handlerData: HandlerDataFetch): void { |
| 125 | + const { startTimestamp, endTimestamp } = handlerData; |
| 126 | + |
| 127 | + // We only capture complete fetch requests |
| 128 | + if (!endTimestamp) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') { |
| 133 | + // We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests) |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if (handlerData.error) { |
| 138 | + const data: FetchBreadcrumbData = handlerData.fetchData; |
| 139 | + const hint: FetchBreadcrumbHint = { |
| 140 | + data: handlerData.error, |
| 141 | + input: handlerData.args, |
| 142 | + startTimestamp, |
| 143 | + endTimestamp, |
| 144 | + }; |
| 145 | + |
| 146 | + addBreadcrumb( |
| 147 | + { |
| 148 | + category: 'fetch', |
| 149 | + data, |
| 150 | + level: 'error', |
| 151 | + type: 'http', |
| 152 | + }, |
| 153 | + hint, |
| 154 | + ); |
| 155 | + } else { |
| 156 | + const response = handlerData.response as Response | undefined; |
| 157 | + const data: FetchBreadcrumbData = { |
| 158 | + ...handlerData.fetchData, |
| 159 | + status_code: response && response.status, |
| 160 | + }; |
| 161 | + const hint: FetchBreadcrumbHint = { |
| 162 | + input: handlerData.args, |
| 163 | + response, |
| 164 | + startTimestamp, |
| 165 | + endTimestamp, |
| 166 | + }; |
| 167 | + addBreadcrumb( |
| 168 | + { |
| 169 | + category: 'fetch', |
| 170 | + data, |
| 171 | + type: 'http', |
| 172 | + }, |
| 173 | + hint, |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments