|
| 1 | +import { captureCheckIn, runWithAsyncContext } from '@sentry/core'; |
| 2 | +import type { NextApiRequest } from 'next'; |
| 3 | + |
| 4 | +import type { VercelCronsConfig } from './types'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Wraps a function with Sentry crons instrumentation by automaticaly sending check-ins for the given Vercel crons config. |
| 8 | + */ |
| 9 | +export function wrapApiHandlerWithSentryVercelCrons<F extends (...args: any[]) => any>( |
| 10 | + handler: F, |
| 11 | + vercelCronsConfig: VercelCronsConfig, |
| 12 | +): F { |
| 13 | + return new Proxy(handler, { |
| 14 | + apply: (originalFunction, thisArg, args: [NextApiRequest | undefined] | undefined) => { |
| 15 | + return runWithAsyncContext(() => { |
| 16 | + if (!args || !args[0]) { |
| 17 | + return originalFunction.apply(thisArg, args); |
| 18 | + } |
| 19 | + const [req] = args; |
| 20 | + |
| 21 | + let maybePromiseResult; |
| 22 | + const cronsKey = req.url; |
| 23 | + |
| 24 | + if ( |
| 25 | + !vercelCronsConfig || // do nothing if vercel crons config is missing |
| 26 | + !req.headers['user-agent']?.includes('vercel-cron') // do nothing if endpoint is not called from vercel crons |
| 27 | + ) { |
| 28 | + return originalFunction.apply(thisArg, args); |
| 29 | + } |
| 30 | + |
| 31 | + const vercelCron = vercelCronsConfig.find(vercelCron => vercelCron.path === cronsKey); |
| 32 | + |
| 33 | + if (!vercelCron || !vercelCron.path || !vercelCron.schedule) { |
| 34 | + return originalFunction.apply(thisArg, args); |
| 35 | + } |
| 36 | + |
| 37 | + const monitorSlug = vercelCron.path; |
| 38 | + |
| 39 | + const checkInId = captureCheckIn( |
| 40 | + { |
| 41 | + monitorSlug, |
| 42 | + status: 'in_progress', |
| 43 | + }, |
| 44 | + { |
| 45 | + checkinMargin: 2, // two minutes - in case Vercel has a blip |
| 46 | + maxRuntime: 60 * 12, // (minutes) so 12 hours - just a very high arbitrary number since we don't know the actual duration of the users cron job |
| 47 | + schedule: { |
| 48 | + type: 'crontab', |
| 49 | + value: vercelCron.schedule, |
| 50 | + }, |
| 51 | + }, |
| 52 | + ); |
| 53 | + |
| 54 | + const startTime = Date.now() / 1000; |
| 55 | + |
| 56 | + const handleErrorCase = (): void => { |
| 57 | + captureCheckIn({ |
| 58 | + checkInId, |
| 59 | + monitorSlug, |
| 60 | + status: 'error', |
| 61 | + duration: Date.now() / 1000 - startTime, |
| 62 | + }); |
| 63 | + }; |
| 64 | + |
| 65 | + try { |
| 66 | + maybePromiseResult = originalFunction.apply(thisArg, args); |
| 67 | + } catch (e) { |
| 68 | + handleErrorCase(); |
| 69 | + throw e; |
| 70 | + } |
| 71 | + |
| 72 | + if (typeof maybePromiseResult === 'object' && maybePromiseResult !== null && 'then' in maybePromiseResult) { |
| 73 | + Promise.resolve(maybePromiseResult).then( |
| 74 | + () => { |
| 75 | + captureCheckIn({ |
| 76 | + checkInId, |
| 77 | + monitorSlug, |
| 78 | + status: 'ok', |
| 79 | + duration: Date.now() / 1000 - startTime, |
| 80 | + }); |
| 81 | + }, |
| 82 | + () => { |
| 83 | + handleErrorCase(); |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + // It is very important that we return the original promise here, because Next.js attaches various properties |
| 88 | + // to that promise and will throw if they are not on the returned value. |
| 89 | + return maybePromiseResult; |
| 90 | + } else { |
| 91 | + captureCheckIn({ |
| 92 | + checkInId, |
| 93 | + monitorSlug, |
| 94 | + status: 'ok', |
| 95 | + duration: Date.now() / 1000 - startTime, |
| 96 | + }); |
| 97 | + return maybePromiseResult; |
| 98 | + } |
| 99 | + }); |
| 100 | + }, |
| 101 | + }); |
| 102 | +} |
0 commit comments