|
| 1 | +import * as Sentry from "@sentry/node"; |
| 2 | +import { rejectedSyncPromise } from "@sentry/utils"; |
| 3 | +import { fetch } from "undici"; |
| 4 | +import { version as wranglerVersion } from "../../package.json"; |
| 5 | +import { confirm } from "../dialogs"; |
| 6 | +import { logger } from "../logger"; |
| 7 | +import type { BaseTransportOptions, TransportRequest } from "@sentry/types"; |
| 8 | +import type { RequestInit } from "undici"; |
| 9 | + |
| 10 | +let sentryReportingAllowed = false; |
| 11 | + |
| 12 | +// The SENTRY_DSN is provided at esbuild time as a `define` for production and beta releases. |
| 13 | +// Otherwise it is left undefined, which disables reporting. |
| 14 | +declare const SENTRY_DSN: string; |
| 15 | + |
| 16 | +/* Returns a Sentry transport for the Sentry proxy Worker. */ |
| 17 | +export const makeSentry10Transport = (options: BaseTransportOptions) => { |
| 18 | + let eventQueue: [string, RequestInit][] = []; |
| 19 | + |
| 20 | + const transportSentry10 = async (request: TransportRequest) => { |
| 21 | + /* Adds helpful properties to the request body before we send it to our |
| 22 | + proxy Worker. These properties can be parsed out from the NDJSON in |
| 23 | + `request.body`, but it's easier and safer to just attach them here. */ |
| 24 | + const sentryWorkerPayload = { |
| 25 | + envelope: request.body, |
| 26 | + url: options.url, |
| 27 | + }; |
| 28 | + |
| 29 | + try { |
| 30 | + if (sentryReportingAllowed) { |
| 31 | + const eventsToSend = [...eventQueue]; |
| 32 | + eventQueue = []; |
| 33 | + for (const event of eventsToSend) { |
| 34 | + await fetch(event[0], event[1]); |
| 35 | + } |
| 36 | + |
| 37 | + const response = await fetch( |
| 38 | + `https://platform.dash.cloudflare.com/sentry/envelope`, |
| 39 | + { |
| 40 | + method: "POST", |
| 41 | + headers: { |
| 42 | + Accept: "*/*", |
| 43 | + "Content-Type": "application/json", |
| 44 | + }, |
| 45 | + body: JSON.stringify(sentryWorkerPayload), |
| 46 | + } |
| 47 | + ); |
| 48 | + |
| 49 | + return { |
| 50 | + statusCode: response.status, |
| 51 | + headers: { |
| 52 | + "x-sentry-rate-limits": response.headers.get( |
| 53 | + "X-Sentry-Rate-Limits" |
| 54 | + ), |
| 55 | + "retry-after": response.headers.get("Retry-After"), |
| 56 | + }, |
| 57 | + }; |
| 58 | + } else { |
| 59 | + // We don't currently have permission to send this event, but maybe we will in the future. |
| 60 | + // Add to an in-memory just in case |
| 61 | + eventQueue.push([ |
| 62 | + `https://platform.dash.cloudflare.com/sentry/envelope`, |
| 63 | + { |
| 64 | + method: "POST", |
| 65 | + headers: { |
| 66 | + Accept: "*/*", |
| 67 | + "Content-Type": "application/json", |
| 68 | + }, |
| 69 | + body: JSON.stringify(sentryWorkerPayload), |
| 70 | + }, |
| 71 | + ]); |
| 72 | + return { |
| 73 | + statusCode: 200, |
| 74 | + }; |
| 75 | + } |
| 76 | + } catch (err) { |
| 77 | + console.log(err); |
| 78 | + |
| 79 | + return rejectedSyncPromise(err); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + return Sentry.createTransport(options, transportSentry10); |
| 84 | +}; |
| 85 | + |
| 86 | +export function setupSentry() { |
| 87 | + if (typeof SENTRY_DSN !== "undefined") { |
| 88 | + Sentry.init({ |
| 89 | + release: `wrangler@${wranglerVersion}`, |
| 90 | + dsn: SENTRY_DSN, |
| 91 | + transport: makeSentry10Transport, |
| 92 | + }); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export function addBreadcrumb( |
| 97 | + message: string, |
| 98 | + level: Sentry.SeverityLevel = "log" |
| 99 | +) { |
| 100 | + if (typeof SENTRY_DSN !== "undefined") { |
| 101 | + Sentry.addBreadcrumb({ |
| 102 | + message, |
| 103 | + level, |
| 104 | + }); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Capture top-level Wrangler errors. Also take this opportunity to ask the user for |
| 109 | +// consent if not already granted. |
| 110 | +export async function captureGlobalException(e: unknown) { |
| 111 | + if (typeof SENTRY_DSN !== "undefined") { |
| 112 | + sentryReportingAllowed = await confirm( |
| 113 | + "Would you like to report this error to Cloudflare?", |
| 114 | + { fallbackValue: false } |
| 115 | + ); |
| 116 | + |
| 117 | + if (!sentryReportingAllowed) { |
| 118 | + logger.debug(`Sentry: Reporting disabled - would have sent ${e}.`); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + logger.debug(`Sentry: Capturing exception ${e}`); |
| 123 | + Sentry.captureException(e); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// Ensure we send Sentry events before Wrangler exits |
| 128 | +export async function closeSentry() { |
| 129 | + if (typeof SENTRY_DSN !== "undefined") { |
| 130 | + await Sentry.close(); |
| 131 | + } |
| 132 | +} |
0 commit comments