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): Add ability to send cron monitor check ins #8039

Merged
merged 4 commits into from
May 4, 2023

Conversation

AbhiPrasad
Copy link
Member

@AbhiPrasad AbhiPrasad commented May 4, 2023

Closes #8006

Sentry has a cron monitoring product! This PR adds cron monitoring support to the Node SDK, so you can use the SDK to easily monitor your cron jobs!

You can monitor your cron jobs by sending check-ins to Sentry whenever they start and stop! If you notice a cron hasn't checked in, or has not submitted a finish check-in, you know there is something wrong and you need to investigate!

Based on the work in #7996, we expose a top level API to send check ins to Sentry. Currently there is no hub level method to do this, that was done intentionally.

To send a check in to Sentry, simply use the captureCheckIn method exported from the SDK. First you must send an in_progress, checkin, then you can send one with status ok or error based on what happened with your cron job.

const Sentry = require('@sentry/node');

// ...

Sentry.captureCheckIn({
  // make sure this is the same slug as what you set up your
  // Sentry cron monitor with.
  monitorSlug: 'dailyEmail',
  status: 'in_progress',
});

const startTime = timeInSeconds();

runTask();

Sentry.captureCheckIn({
  monitorSlug: 'dailyEmail',
  status: 'ok',
  duration: timeInSeconds() - startTime,
});

You can also optionally add a monitor config so you can create a monitor automatically, without needing to go into Sentry and setting up a cron monitor.

Sentry.captureCheckIn(
  {
    monitorSlug: 'dailyEmail',
    status: 'in_progress',
  },
  {
    schedule: {
      type: 'crontab',
      value: '0 * * * *',
    },
    // 🇨🇦🫡
    timezone: 'Canada/Eastern',
  },
);

Added some basic unit tests, and validated sending in Sentry as well.

@AbhiPrasad AbhiPrasad requested review from gaprl, mydea and lforst May 4, 2023 10:47
@AbhiPrasad AbhiPrasad self-assigned this May 4, 2023
@cleptric
Copy link
Member

cleptric commented May 4, 2023

In Python and PHP, we opted to use Sentry.captureEvent.
Not against the introduction of Sentry.captureCheckIn, but unified API something something, maybe 🙂

@AbhiPrasad
Copy link
Member Author

captureEvent in the JS SDK also allows you to pass in hint, which I thought felt confusing in this context, so we decided to make this an explicit API.

Easy to understand - and means we don't further overload captureEvent. captureCheckIn -> just for monitors!

packages/node/src/checkin.ts Outdated Show resolved Hide resolved
packages/node/src/client.ts Outdated Show resolved Hide resolved
packages/node/src/client.ts Outdated Show resolved Hide resolved
@@ -44,7 +44,7 @@ describe('CheckIn', () => {
duration: 10.0,
release: '1.0.0',
environment: 'production',
} as CheckIn,
} as SerializedCheckIn,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

giga l: you can simply do as const instead of as SerializedCheckIn here. TS is only complaining because status is interpreted as string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna keep this anyway because I prefer the explicitness of the type.

},
checkinMargin: 2,
maxRuntime: 12333,
timezone: 'Canada/Eastern',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫡

@AbhiPrasad AbhiPrasad merged commit 9ea2bca into develop May 4, 2023
@AbhiPrasad AbhiPrasad deleted the abhi-node-checkin branch May 4, 2023 12:54
@AbhiPrasad
Copy link
Member Author

image

wooo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add cron monitoring for Node
4 participants