Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): Instrumentation for node-cron library #9904

Merged
merged 17 commits into from
Jan 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions packages/node/src/checkin/node-cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { withMonitor } from '@sentry/core';

interface NodeCronOptions {
name?: string;
timezone?: string;
}

interface NodeCron {
schedule: (cronExpression: string, callback: () => void, options?: NodeCronOptions) => unknown;
anonrig marked this conversation as resolved.
Show resolved Hide resolved
}

const replacements: [string, string][] = [
['january', '1'],
['february', '2'],
['march', '3'],
['april', '4'],
['may', '5'],
['june', '6'],
['july', '7'],
['august', '8'],
['september', '9'],
['october', '10'],
['november', '11'],
['december', '12'],
['jan', '1'],
['feb', '2'],
['mar', '3'],
['apr', '4'],
['may', '5'],
['jun', '6'],
['jul', '7'],
['aug', '8'],
['sep', '9'],
['oct', '10'],
['nov', '11'],
['dec', '12'],
['sunday', '0'],
['monday', '1'],
['tuesday', '2'],
['wednesday', '3'],
['thursday', '4'],
['friday', '5'],
['saturday', '6'],
['sun', '0'],
['mon', '1'],
['tue', '2'],
['wed', '3'],
['thu', '4'],
['fri', '5'],
['sat', '6'],
];

function toSentryCrontab(cronExpression: string): string {
return replacements.reduce(
(acc, [name, replacement]) => acc.replace(new RegExp(name, 'gi'), replacement),
cronExpression,
);
}

/**
* Wraps the node-cron library with check-in monitoring.
*
* ```ts
* import * as Sentry from '@sentry/node';
* import cron from 'node-cron';
*
* const cronWithCheckIn = Sentry.instrumentNodeCron(cron);
*
* cronWithCheckIn.schedule('* * * * *', () => {
* console.log('running a task every minute');
* },
* { name: 'my-cron-job' },
* });
* ```
*/
export function instrumentNodeCron<T>(lib: Partial<NodeCron> & T): T {
return new Proxy(lib, {
get(target, prop: keyof NodeCron) {
if (prop === 'schedule' && target.schedule) {
// When 'get' is called for schedule, return a proxied version of the schedule function
return new Proxy(target.schedule, {
apply(target, thisArg, argArray: Parameters<NodeCron['schedule']>) {
const [expression, _, options] = argArray;

if (!options?.name) {
throw new Error('Missing "name" for scheduled job. This is required for Sentry check-in monitoring.');
}

return withMonitor(
options.name,
() => {
return target.apply(thisArg, argArray);
},
{
schedule: { type: 'crontab', value: toSentryCrontab(expression) },
timezone: options?.timezone,
},
);
},
});
} else {
return target[prop];
}
},
});
}
Loading