-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e956691
feat(node): Add wrapping of `node-cron` for check-ins
timfish 6f8a311
Export as `cron` and add test
timfish 42abf29
add another test
timfish 8759fc2
Merge branch 'develop' into feat/node-cron-wrap
timfish 37f4466
Fix broken merge
timfish e6b93cd
remove barrel file
timfish 150551e
Fix tests
timfish d4918bc
Merge remote-tracking branch 'upstream/develop' into feat/node-cron-wrap
timfish 1ec7586
check assertions
timfish 3fd5252
Merge branch 'develop' into feat/node-cron-wrap
timfish 61cc3ee
Move cron name replacement to common
timfish 8a98fc0
Merge remote-tracking branch 'origin/feat/node-cron-wrap' into feat/n…
timfish 7fcd9b8
Merge branch 'develop' into feat/node-cron-wrap
timfish 7bd57aa
Merge remote-tracking branch 'upstream/develop' into feat/node-cron-wrap
timfish f8931ca
Merge branch 'develop' into feat/node-cron-wrap
timfish 1196720
Export from downstream packages
timfish 8fdb903
disable lint
timfish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}, | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.