@sentry/node
Version:
Sentry Node SDK using OpenTelemetry for performance instrumentation
51 lines (48 loc) • 1.69 kB
JavaScript
import { withMonitor, captureException } from '@sentry/core';
import { replaceCronNames } from './common.js';
function instrumentNodeCron(lib, monitorConfig = {}) {
return new Proxy(lib, {
get(target, prop) {
if (prop === "schedule" && target.schedule) {
return new Proxy(target.schedule, {
apply(target2, thisArg, argArray) {
const [expression, callback, options] = argArray;
const name = options?.name;
const timezone = options?.timezone;
if (!name) {
throw new Error('Missing "name" for scheduled job. A name is required for Sentry check-in monitoring.');
}
const monitoredCallback = async (...args) => {
return withMonitor(
name,
async () => {
try {
return await callback(...args);
} catch (e) {
captureException(e, {
mechanism: {
handled: false,
type: "auto.function.node-cron.instrumentNodeCron"
}
});
throw e;
}
},
{
schedule: { type: "crontab", value: replaceCronNames(expression) },
timezone,
...monitorConfig
}
);
};
return target2.apply(thisArg, [expression, monitoredCallback, options]);
}
});
} else {
return target[prop];
}
}
});
}
export { instrumentNodeCron };
//# sourceMappingURL=node-cron.js.map