-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathwrapApiHandlerWithSentryVercelCrons.ts
108 lines (93 loc) · 3.24 KB
/
wrapApiHandlerWithSentryVercelCrons.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { captureCheckIn } from '@sentry/core';
import type { NextApiRequest } from 'next';
import type { VercelCronsConfig } from '../types';
type EdgeRequest = {
nextUrl: URL;
headers: Headers;
};
/**
* Wraps a function with Sentry crons instrumentation by automatically sending check-ins for the given Vercel crons config.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function wrapApiHandlerWithSentryVercelCrons<F extends (...args: any[]) => any>(
handler: F,
vercelCronsConfig: VercelCronsConfig,
): F {
return new Proxy(handler, {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
apply: (originalFunction, thisArg, args: any[]) => {
if (!args?.[0]) {
return originalFunction.apply(thisArg, args);
}
const [req] = args as [NextApiRequest | EdgeRequest];
let maybePromiseResult;
const cronsKey = 'nextUrl' in req ? req.nextUrl.pathname : req.url;
const userAgentHeader = 'nextUrl' in req ? req.headers.get('user-agent') : req.headers['user-agent'];
if (
!vercelCronsConfig || // do nothing if vercel crons config is missing
!userAgentHeader?.includes('vercel-cron') // do nothing if endpoint is not called from vercel crons
) {
return originalFunction.apply(thisArg, args);
}
const vercelCron = vercelCronsConfig.find(vercelCron => vercelCron.path === cronsKey);
if (!vercelCron?.path || !vercelCron.schedule) {
return originalFunction.apply(thisArg, args);
}
const monitorSlug = vercelCron.path;
const checkInId = captureCheckIn(
{
monitorSlug,
status: 'in_progress',
},
{
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
schedule: {
type: 'crontab',
value: vercelCron.schedule,
},
},
);
const startTime = Date.now() / 1000;
const handleErrorCase = (): void => {
captureCheckIn({
checkInId,
monitorSlug,
status: 'error',
duration: Date.now() / 1000 - startTime,
});
};
try {
maybePromiseResult = originalFunction.apply(thisArg, args);
} catch (e) {
handleErrorCase();
throw e;
}
if (typeof maybePromiseResult === 'object' && maybePromiseResult !== null && 'then' in maybePromiseResult) {
Promise.resolve(maybePromiseResult).then(
() => {
captureCheckIn({
checkInId,
monitorSlug,
status: 'ok',
duration: Date.now() / 1000 - startTime,
});
},
() => {
handleErrorCase();
},
);
// It is very important that we return the original promise here, because Next.js attaches various properties
// to that promise and will throw if they are not on the returned value.
return maybePromiseResult;
} else {
captureCheckIn({
checkInId,
monitorSlug,
status: 'ok',
duration: Date.now() / 1000 - startTime,
});
return maybePromiseResult;
}
},
});
}