|
| 1 | +import { type Event, captureEvent } from '@sentry/cloudflare'; |
| 2 | + |
| 3 | +import { SentryTailWorkerOptions } from '../index'; |
| 4 | +import { |
| 5 | + consoleLogLevelToSentryLevel, |
| 6 | + consoleLogToString, |
| 7 | + determineSeverityLevel, |
| 8 | + workerOutcomeToEventMessage, |
| 9 | +} from './sentry'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Extracts relevant data from the provided {@link TraceItem} & sends it to |
| 13 | + * Sentry if determined to be something worthy of reporting |
| 14 | + */ |
| 15 | +export function processTraceItem( |
| 16 | + options: SentryTailWorkerOptions, |
| 17 | + item: TraceItem |
| 18 | +): void { |
| 19 | + const severityLevel = determineSeverityLevel(item); |
| 20 | + if (!severityLevel) { |
| 21 | + // Not an error |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + if ( |
| 26 | + options.samplingRate !== 1 && |
| 27 | + !shouldSampleTraceItem(options.samplingRate) |
| 28 | + ) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const event: Event = { |
| 33 | + level: severityLevel, |
| 34 | + timestamp: item.eventTimestamp ?? Date.now(), |
| 35 | + logger: '@node-core/cloudflare-sentry-tail', |
| 36 | + message: workerOutcomeToEventMessage(item.outcome), |
| 37 | + fingerprint: [], |
| 38 | + breadcrumbs: [], |
| 39 | + exception: { |
| 40 | + values: [], |
| 41 | + }, |
| 42 | + tags: { |
| 43 | + outcome: item.outcome, |
| 44 | + script_name: item.scriptName, |
| 45 | + script_version: item.scriptVersion?.tag, |
| 46 | + cpu_time: item.cpuTime, |
| 47 | + wall_time: item.wallTime, |
| 48 | + }, |
| 49 | + }; |
| 50 | + |
| 51 | + // Populate data specific to the type of trace event we got |
| 52 | + handleTraceItemEvent(options, item, event); |
| 53 | + |
| 54 | + // Populate breadcrumbs with any relevant data |
| 55 | + addRemainingBreadcrumbs(item, event); |
| 56 | + |
| 57 | + // Sort breadcrumbs by their timestamps |
| 58 | + event.breadcrumbs?.sort((a, b) => { |
| 59 | + if (!a.timestamp || !b.timestamp) { |
| 60 | + return 0; |
| 61 | + } |
| 62 | + |
| 63 | + return a.timestamp - b.timestamp; |
| 64 | + }); |
| 65 | + |
| 66 | + captureEvent(event); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Determines what kind of trace item event we received and adds any |
| 71 | + * event-specific properties to the Sentry event to be reported. |
| 72 | + */ |
| 73 | +function handleTraceItemEvent( |
| 74 | + options: SentryTailWorkerOptions, |
| 75 | + item: TraceItem, |
| 76 | + sentryEvent: Event |
| 77 | +): void { |
| 78 | + if (!item.event) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if ('request' in item.event) { |
| 83 | + const request = item.event.request; |
| 84 | + const response = item.event.response; |
| 85 | + |
| 86 | + const redactedHeaders: Record<string, string> = {}; |
| 87 | + for (let [key, value] of Object.entries(request.headers)) { |
| 88 | + key = key.toLowerCase(); |
| 89 | + |
| 90 | + if (options.headersToRedact && options.headersToRedact.includes(key)) { |
| 91 | + value = 'redacted'; |
| 92 | + } |
| 93 | + |
| 94 | + redactedHeaders[key] = value; |
| 95 | + } |
| 96 | + |
| 97 | + sentryEvent.request = { |
| 98 | + method: request.method, |
| 99 | + url: request.url, |
| 100 | + headers: redactedHeaders, |
| 101 | + env: { |
| 102 | + asn: request.cf?.asn, |
| 103 | + colo: request.cf?.colo, |
| 104 | + continent: request.cf?.continent, |
| 105 | + country: request.cf?.country, |
| 106 | + timezone: request.cf?.timezone, |
| 107 | + httpProtocol: request.cf?.httpProtocol, |
| 108 | + requestPriority: request.cf?.requestPriority, |
| 109 | + tlsCipher: request.cf?.tlsCipher, |
| 110 | + tlsClientAuth: request.cf?.tlsClientAuth, |
| 111 | + tlsExportedAuthenticator: request.cf?.tlsExportedAuthenticator, |
| 112 | + tlsVersion: request.cf?.tlsVersion, |
| 113 | + }, |
| 114 | + }; |
| 115 | + |
| 116 | + const responseStatusCode = response?.status ?? 'Unknown'; |
| 117 | + |
| 118 | + sentryEvent.message = response |
| 119 | + ? `${responseStatusCode} Response` |
| 120 | + : 'No response'; |
| 121 | + |
| 122 | + sentryEvent.breadcrumbs?.push({ |
| 123 | + type: 'http', |
| 124 | + category: 'request', |
| 125 | + timestamp: item.eventTimestamp ?? Date.now(), |
| 126 | + data: { |
| 127 | + url: request.url, |
| 128 | + method: request.method, |
| 129 | + status_code: responseStatusCode, |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + const requestUrl = new URL(request.url); |
| 134 | + sentryEvent.fingerprint?.push( |
| 135 | + requestUrl.origin, |
| 136 | + requestUrl.pathname, |
| 137 | + request.method, |
| 138 | + `${responseStatusCode}` |
| 139 | + ); |
| 140 | + |
| 141 | + sentryEvent.tags!.event = 'fetch'; |
| 142 | + sentryEvent.tags!.ray_id = redactedHeaders['cf-ray']; |
| 143 | + } else if ('rpcMethod' in item.event) { |
| 144 | + sentryEvent.tags!.event = 'js-rpc'; |
| 145 | + sentryEvent.tags!.rpc_method = item.event.rpcMethod; |
| 146 | + } else if ('scheduledTime' in item.event) { |
| 147 | + if ('cron' in item.event) { |
| 148 | + sentryEvent.tags!.event = 'scheduled'; |
| 149 | + sentryEvent.tags!.scheduled_time = item.event.scheduledTime; |
| 150 | + sentryEvent.tags!.cron = item.event.cron; |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + sentryEvent.tags!.event = 'alarm'; |
| 155 | + sentryEvent.tags!.scheduled_time = item.event.scheduledTime.toUTCString(); |
| 156 | + } else if ('queue' in item.event) { |
| 157 | + sentryEvent.tags!.event = 'queue'; |
| 158 | + sentryEvent.tags!.queue = item.event.queue; |
| 159 | + sentryEvent.tags!.batchSize = item.event.batchSize; |
| 160 | + } else if ('mailFrom' in item.event) { |
| 161 | + sentryEvent.tags!.event = 'email'; |
| 162 | + sentryEvent.tags!.rawSize = item.event.rawSize; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +function addRemainingBreadcrumbs(item: TraceItem, sentryEvent: Event) { |
| 167 | + if (!sentryEvent.breadcrumbs) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + let breadcrumbsIdx = sentryEvent.breadcrumbs.length; |
| 172 | + |
| 173 | + // Preallocate space for the elements we're gonna add |
| 174 | + sentryEvent.breadcrumbs.length += |
| 175 | + item.logs.length + |
| 176 | + item.diagnosticsChannelEvents.length + |
| 177 | + item.exceptions.length; |
| 178 | + |
| 179 | + for (const log of item.logs) { |
| 180 | + sentryEvent.breadcrumbs[breadcrumbsIdx++] = { |
| 181 | + type: 'debug', |
| 182 | + category: `console.${log.level}`, |
| 183 | + message: consoleLogToString(log.message), |
| 184 | + level: consoleLogLevelToSentryLevel(log.level), |
| 185 | + timestamp: log.timestamp, |
| 186 | + }; |
| 187 | + } |
| 188 | + |
| 189 | + for (const payload of item.diagnosticsChannelEvents) { |
| 190 | + sentryEvent.breadcrumbs[breadcrumbsIdx++] = { |
| 191 | + type: 'debug', |
| 192 | + category: `channel.${payload.channel}`, |
| 193 | + message: consoleLogToString(payload.message), |
| 194 | + level: 'debug', |
| 195 | + timestamp: payload.timestamp, |
| 196 | + }; |
| 197 | + } |
| 198 | + |
| 199 | + let fingerprintIdx = sentryEvent.fingerprint!.length; |
| 200 | + let exceptionValueIdx = sentryEvent.exception!.values!.length; |
| 201 | + |
| 202 | + sentryEvent.fingerprint!.length += item.exceptions.length; |
| 203 | + sentryEvent.exception!.values!.length += item.exceptions.length; |
| 204 | + |
| 205 | + for (const exception of item.exceptions) { |
| 206 | + sentryEvent.breadcrumbs[breadcrumbsIdx++] = { |
| 207 | + type: 'error', |
| 208 | + level: 'error', |
| 209 | + category: exception.name, |
| 210 | + message: exception.message, |
| 211 | + timestamp: exception.timestamp, |
| 212 | + data: { |
| 213 | + stack: exception.stack, |
| 214 | + }, |
| 215 | + }; |
| 216 | + |
| 217 | + sentryEvent.fingerprint![fingerprintIdx++] = exception.name; |
| 218 | + sentryEvent.exception!.values![exceptionValueIdx++] = { |
| 219 | + type: exception.name, |
| 220 | + value: exception.message, |
| 221 | + }; |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +function shouldSampleTraceItem(sampleRate: number) { |
| 226 | + const buffer = new Uint32Array(1); |
| 227 | + crypto.getRandomValues(buffer); |
| 228 | + |
| 229 | + const UINT_32_LIMIT = 2 ** 32; |
| 230 | + const random = buffer[0] / UINT_32_LIMIT; |
| 231 | + |
| 232 | + return random <= sampleRate; |
| 233 | +} |
0 commit comments