Skip to content

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

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

Merged
merged 17 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export {
startInactiveSpan,
startSpanManual,
continueTrace,
cron,
} from '@sentry/node';

// We can still leave this for the carrier init and type exports
Expand Down
2 changes: 1 addition & 1 deletion packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export {
metrics,
} from '@sentry/core';
export type { SpanStatusType } from '@sentry/core';
export { autoDiscoverNodePerformanceMonitoringIntegrations } from '@sentry/node';
export { autoDiscoverNodePerformanceMonitoringIntegrations, cron } from '@sentry/node';

export { BunClient } from './client';
export { defaultIntegrations, init } from './sdk';
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/cron/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const replacements: [string, string][] = [
*/
export function replaceCronNames(cronExpression: string): string {
return replacements.reduce(
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor
(acc, [name, replacement]) => acc.replace(new RegExp(name, 'gi'), replacement),
cronExpression,
);
Expand Down
61 changes: 61 additions & 0 deletions packages/node/src/cron/node-cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { withMonitor } from '@sentry/core';
import { replaceCronNames } from './common';

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

export interface NodeCron {
schedule: (cronExpression: string, callback: () => void, options?: NodeCronOptions) => unknown;
}

/**
* Wraps the `node-cron` library with check-in monitoring.
*
* ```ts
* import * as Sentry from "@sentry/node";
* import cron from "node-cron";
*
* const cronWithCheckIn = Sentry.cron.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. A name is required for Sentry check-in monitoring.');
}

return withMonitor(
options.name,
() => {
return target.apply(thisArg, argArray);
},
{
schedule: { type: 'crontab', value: replaceCronNames(expression) },
timezone: options?.timezone,
},
);
},
});
} else {
return target[prop];
}
},
});
}
2 changes: 2 additions & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ export { INTEGRATIONS as Integrations, Handlers };
export { hapiErrorPlugin } from './integrations/hapi';

import { instrumentCron } from './cron/cron';
import { instrumentNodeCron } from './cron/node-cron';

/** Methods to instrument cron libraries for Sentry check-ins */
export const cron = {
instrumentCron,
instrumentNodeCron,
};
48 changes: 47 additions & 1 deletion packages/node/test/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as SentryCore from '@sentry/core';

import { cron } from '../src';
import type { CronJob, CronJobParams } from '../src/cron/cron';
import type { NodeCron, NodeCronOptions } from '../src/cron/node-cron';

describe('cron', () => {
describe('cron check-ins', () => {
let withMonitorSpy: jest.SpyInstance;

beforeEach(() => {
Expand Down Expand Up @@ -78,4 +79,49 @@ describe('cron', () => {
});
});
});

describe('node-cron', () => {
test('calls withMonitor', done => {
expect.assertions(5);

const nodeCron: NodeCron = {
schedule: (expression: string, callback: () => void, options?: NodeCronOptions): unknown => {
expect(expression).toBe('* * * Jan,Sep Sun');
expect(callback).toBeInstanceOf(Function);
expect(options?.name).toBe('my-cron-job');
return callback();
},
};

const cronWithCheckIn = cron.instrumentNodeCron(nodeCron);

cronWithCheckIn.schedule(
'* * * Jan,Sep Sun',
() => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
});
done();
},
{ name: 'my-cron-job' },
);
});

test('throws without supplied name', () => {
const nodeCron: NodeCron = {
schedule: (): unknown => {
return undefined;
},
};

const cronWithCheckIn = cron.instrumentNodeCron(nodeCron);

expect(() => {
cronWithCheckIn.schedule('* * * * *', () => {
//
});
}).toThrowError('Missing "name" for scheduled job. A name is required for Sentry check-in monitoring.');
});
});
});
1 change: 1 addition & 0 deletions packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export {
deepReadDirSync,
Integrations,
Handlers,
cron,
} from '@sentry/node';

// Keeping the `*` exports for backwards compatibility and types
Expand Down
1 change: 1 addition & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export {
startInactiveSpan,
startSpanManual,
continueTrace,
cron,
} from '@sentry/node';

// We can still leave this for the carrier init and type exports
Expand Down