Skip to content

Commit 6be91da

Browse files
Luca ForstnerAbhiPrasad
Luca Forstner
andauthored
feat(nextjs): Add automatic monitors for Vercel Cron Jobs (#8088)
Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 48ef411 commit 6be91da

File tree

6 files changed

+65
-5
lines changed

6 files changed

+65
-5
lines changed

packages/nextjs/src/common/wrapApiHandlerWithSentryVercelCrons.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import type { NextApiRequest } from 'next';
33

44
import type { VercelCronsConfig } from './types';
55

6+
type EdgeRequest = {
7+
nextUrl: URL;
8+
headers: Headers;
9+
};
10+
611
/**
712
* Wraps a function with Sentry crons instrumentation by automaticaly sending check-ins for the given Vercel crons config.
813
*/
@@ -11,19 +16,21 @@ export function wrapApiHandlerWithSentryVercelCrons<F extends (...args: any[]) =
1116
vercelCronsConfig: VercelCronsConfig,
1217
): F {
1318
return new Proxy(handler, {
14-
apply: (originalFunction, thisArg, args: [NextApiRequest | undefined] | undefined) => {
19+
apply: (originalFunction, thisArg, args: [NextApiRequest | EdgeRequest | undefined] | undefined) => {
1520
return runWithAsyncContext(() => {
1621
if (!args || !args[0]) {
1722
return originalFunction.apply(thisArg, args);
1823
}
24+
1925
const [req] = args;
2026

2127
let maybePromiseResult;
22-
const cronsKey = req.url;
28+
const cronsKey = 'nextUrl' in req ? req.nextUrl.pathname : req.url;
29+
const userAgentHeader = 'nextUrl' in req ? req.headers.get('user-agent') : req.headers['user-agent'];
2330

2431
if (
2532
!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
33+
!userAgentHeader?.includes('vercel-cron') // do nothing if endpoint is not called from vercel crons
2734
) {
2835
return originalFunction.apply(thisArg, args);
2936
}
@@ -42,7 +49,6 @@ export function wrapApiHandlerWithSentryVercelCrons<F extends (...args: any[]) =
4249
status: 'in_progress',
4350
},
4451
{
45-
checkinMargin: 2, // two minutes - in case Vercel has a blip
4652
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
4753
schedule: {
4854
type: 'crontab',

packages/nextjs/src/config/loaders/wrappingLoader.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as fs from 'fs';
55
import * as path from 'path';
66
import { rollup } from 'rollup';
77

8+
import type { VercelCronsConfig } from '../../common/types';
89
import type { LoaderThis } from './types';
910

1011
// Just a simple placeholder to make referencing module consistent
@@ -44,6 +45,7 @@ type LoaderOptions = {
4445
excludeServerRoutes: Array<RegExp | string>;
4546
wrappingTargetKind: 'page' | 'api-route' | 'middleware' | 'server-component';
4647
sentryConfigFilePath?: string;
48+
vercelCronsConfig?: VercelCronsConfig;
4749
};
4850

4951
function moduleExists(id: string): boolean {
@@ -74,6 +76,7 @@ export default function wrappingLoader(
7476
excludeServerRoutes = [],
7577
wrappingTargetKind,
7678
sentryConfigFilePath,
79+
vercelCronsConfig,
7780
} = 'getOptions' in this ? this.getOptions() : this.query;
7881

7982
this.async();
@@ -113,6 +116,8 @@ export default function wrappingLoader(
113116
throw new Error(`Invariant: Could not get template code of unknown kind "${wrappingTargetKind}"`);
114117
}
115118

119+
templateCode = templateCode.replace(/__VERCEL_CRONS_CONFIGURATION__/g, JSON.stringify(vercelCronsConfig));
120+
116121
// Inject the route and the path to the file we're wrapping into the template
117122
templateCode = templateCode.replace(/__ROUTE__/g, parameterizedPagesRoute.replace(/\\/g, '\\\\'));
118123
} else if (wrappingTargetKind === 'server-component') {

packages/nextjs/src/config/templates/apiWrapperTemplate.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as origModule from '__SENTRY_WRAPPING_TARGET_FILE__';
1313
import * as Sentry from '@sentry/nextjs';
1414
import type { PageConfig } from 'next';
1515

16+
import type { VercelCronsConfig } from '../../common/types';
1617
// We import this from `wrappers` rather than directly from `next` because our version can work simultaneously with
1718
// multiple versions of next. See note in `wrappers/types` for more.
1819
import type { NextApiHandler } from '../../server/types';
@@ -54,7 +55,19 @@ export const config = {
5455
},
5556
};
5657

57-
export default userProvidedHandler ? Sentry.wrapApiHandlerWithSentry(userProvidedHandler, '__ROUTE__') : undefined;
58+
declare const __VERCEL_CRONS_CONFIGURATION__: VercelCronsConfig;
59+
60+
let wrappedHandler = userProvidedHandler;
61+
62+
if (wrappedHandler) {
63+
wrappedHandler = Sentry.wrapApiHandlerWithSentry(wrappedHandler, '__ROUTE__');
64+
}
65+
66+
if (wrappedHandler && __VERCEL_CRONS_CONFIGURATION__) {
67+
wrappedHandler = Sentry.wrapApiHandlerWithSentryVercelCrons(wrappedHandler, __VERCEL_CRONS_CONFIGURATION__);
68+
}
69+
70+
export default wrappedHandler;
5871

5972
// Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to
6073
// not include anything whose name matchs something we've explicitly exported above.

packages/nextjs/src/config/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ export type UserSentryOptions = {
133133
* Tree shakes Sentry SDK logger statements from the bundle.
134134
*/
135135
disableLogger?: boolean;
136+
137+
/**
138+
* Automatically create cron monitors in Sentry for your Vercel Cron Jobs if configured via `vercel.json`.
139+
*
140+
* Defaults to `true`.
141+
*/
142+
automaticVercelMonitors?: boolean;
136143
};
137144

138145
export type NextConfigFunction = (phase: string, defaults: { defaultConfig: NextConfigObject }) => NextConfigObject;

packages/nextjs/src/config/webpack.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as chalk from 'chalk';
77
import * as fs from 'fs';
88
import * as path from 'path';
99

10+
import type { VercelCronsConfig } from '../common/types';
1011
// Note: If you need to import a type from Webpack, do it in `types.ts` and export it from there. Otherwise, our
1112
// circular dependency check thinks this file is importing from itself. See https://github.com/pahen/madge/issues/306.
1213
import type {
@@ -163,6 +164,31 @@ export function constructWebpackConfigFunction(
163164
],
164165
});
165166

167+
let vercelCronsConfig: VercelCronsConfig = undefined;
168+
try {
169+
if (process.env.VERCEL && userSentryOptions.automaticVercelMonitors !== false) {
170+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
171+
vercelCronsConfig = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'vercel.json'), 'utf8')).crons;
172+
if (vercelCronsConfig) {
173+
logger.info(
174+
`${chalk.cyan(
175+
'info',
176+
)} - Creating Sentry cron monitors for your Vercel Cron Jobs. You can disable this feature by setting the ${chalk.bold.cyan(
177+
'automaticVercelMonitors',
178+
)} option to false in you Next.js config.`,
179+
);
180+
}
181+
}
182+
} catch (e) {
183+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
184+
if (e.code === 'ENOENT') {
185+
// noop if file does not exist
186+
} else {
187+
// log but noop
188+
logger.error(`${chalk.red('error')} - Sentry failed to read vercel.json`, e);
189+
}
190+
}
191+
166192
// Wrap api routes
167193
newConfig.module.rules.unshift({
168194
test: resourcePath => {
@@ -177,6 +203,7 @@ export function constructWebpackConfigFunction(
177203
loader: path.resolve(__dirname, 'loaders', 'wrappingLoader.js'),
178204
options: {
179205
...staticWrappingLoaderOptions,
206+
vercelCronsConfig,
180207
wrappingTargetKind: 'api-route',
181208
},
182209
},

packages/nextjs/src/edge/edgeclient.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export class EdgeClient extends BaseClient<EdgeClientOptions> {
104104
}
105105

106106
const envelope = createCheckInEnvelope(serializedCheckIn, this.getSdkMetadata(), tunnel, this.getDsn());
107+
108+
__DEBUG_BUILD__ && logger.info('Sending checkin:', checkIn.monitorSlug, checkIn.status);
107109
void this._sendEnvelope(envelope);
108110
return id;
109111
}

0 commit comments

Comments
 (0)