|
| 1 | +import { addBreadcrumb, defineIntegration, getClient } from '@sentry/core'; |
| 2 | +import type { Client, Event as SentryEvent, HandlerDataConsole, HandlerDataFetch, IntegrationFn } 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 | +const INTEGRATION_NAME = 'Breadcrumbs'; |
| 19 | + |
| 20 | +/** |
| 21 | + * This breadcrumbsIntegration is almost the same as the one from @sentry/browser. |
| 22 | + * The Deno-version does not support browser-specific APIs like dom, xhr and history. |
| 23 | + */ |
| 24 | +const _breadcrumbsIntegration = ((options: Partial<BreadcrumbsOptions> = {}) => { |
| 25 | + const _options = { |
| 26 | + console: true, |
| 27 | + fetch: true, |
| 28 | + sentry: true, |
| 29 | + ...options, |
| 30 | + }; |
| 31 | + |
| 32 | + return { |
| 33 | + name: INTEGRATION_NAME, |
| 34 | + setup(client) { |
| 35 | + if (_options.console) { |
| 36 | + addConsoleInstrumentationHandler(_getConsoleBreadcrumbHandler(client)); |
| 37 | + } |
| 38 | + if (_options.fetch) { |
| 39 | + addFetchInstrumentationHandler(_getFetchBreadcrumbHandler(client)); |
| 40 | + } |
| 41 | + if (_options.sentry) { |
| 42 | + client.on('beforeSendEvent', _getSentryBreadcrumbHandler(client)); |
| 43 | + } |
| 44 | + }, |
| 45 | + }; |
| 46 | +}) satisfies IntegrationFn; |
| 47 | + |
| 48 | +export const breadcrumbsIntegration = defineIntegration(_breadcrumbsIntegration); |
| 49 | + |
| 50 | +/** |
| 51 | + * Adds a breadcrumb for Sentry events or transactions if this option is enabled. |
| 52 | + * |
| 53 | + */ |
| 54 | +function _getSentryBreadcrumbHandler(client: Client): (event: SentryEvent) => void { |
| 55 | + return function addSentryBreadcrumb(event: SentryEvent): void { |
| 56 | + if (getClient() !== client) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + addBreadcrumb( |
| 61 | + { |
| 62 | + category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`, |
| 63 | + event_id: event.event_id, |
| 64 | + level: event.level, |
| 65 | + message: getEventDescription(event), |
| 66 | + }, |
| 67 | + { |
| 68 | + event, |
| 69 | + }, |
| 70 | + ); |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Creates breadcrumbs from console API calls |
| 76 | + */ |
| 77 | +function _getConsoleBreadcrumbHandler(client: Client): (handlerData: HandlerDataConsole) => void { |
| 78 | + return function _consoleBreadcrumb(handlerData: HandlerDataConsole): void { |
| 79 | + if (getClient() !== client) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const breadcrumb = { |
| 84 | + category: 'console', |
| 85 | + data: { |
| 86 | + arguments: handlerData.args, |
| 87 | + logger: 'console', |
| 88 | + }, |
| 89 | + level: severityLevelFromString(handlerData.level), |
| 90 | + message: safeJoin(handlerData.args, ' '), |
| 91 | + }; |
| 92 | + |
| 93 | + if (handlerData.level === 'assert') { |
| 94 | + if (handlerData.args[0] === false) { |
| 95 | + breadcrumb.message = `Assertion failed: ${safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`; |
| 96 | + breadcrumb.data.arguments = handlerData.args.slice(1); |
| 97 | + } else { |
| 98 | + // Don't capture a breadcrumb for passed assertions |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + addBreadcrumb(breadcrumb, { |
| 104 | + input: handlerData.args, |
| 105 | + level: handlerData.level, |
| 106 | + }); |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Creates breadcrumbs from fetch API calls |
| 112 | + */ |
| 113 | +function _getFetchBreadcrumbHandler(client: Client): (handlerData: HandlerDataFetch) => void { |
| 114 | + return function _fetchBreadcrumb(handlerData: HandlerDataFetch): void { |
| 115 | + if (getClient() !== client) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const { startTimestamp, endTimestamp } = handlerData; |
| 120 | + |
| 121 | + // We only capture complete fetch requests |
| 122 | + if (!endTimestamp) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') { |
| 127 | + // We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests) |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + if (handlerData.error) { |
| 132 | + const data: FetchBreadcrumbData = handlerData.fetchData; |
| 133 | + const hint: FetchBreadcrumbHint = { |
| 134 | + data: handlerData.error, |
| 135 | + input: handlerData.args, |
| 136 | + startTimestamp, |
| 137 | + endTimestamp, |
| 138 | + }; |
| 139 | + |
| 140 | + addBreadcrumb( |
| 141 | + { |
| 142 | + category: 'fetch', |
| 143 | + data, |
| 144 | + level: 'error', |
| 145 | + type: 'http', |
| 146 | + }, |
| 147 | + hint, |
| 148 | + ); |
| 149 | + } else { |
| 150 | + const response = handlerData.response as Response | undefined; |
| 151 | + const data: FetchBreadcrumbData = { |
| 152 | + ...handlerData.fetchData, |
| 153 | + status_code: response && response.status, |
| 154 | + }; |
| 155 | + const hint: FetchBreadcrumbHint = { |
| 156 | + input: handlerData.args, |
| 157 | + response, |
| 158 | + startTimestamp, |
| 159 | + endTimestamp, |
| 160 | + }; |
| 161 | + addBreadcrumb( |
| 162 | + { |
| 163 | + category: 'fetch', |
| 164 | + data, |
| 165 | + type: 'http', |
| 166 | + }, |
| 167 | + hint, |
| 168 | + ); |
| 169 | + } |
| 170 | + }; |
| 171 | +} |
0 commit comments