Skip to content

Commit 697f406

Browse files
authored
feat(core): Add cron monitor wrapper helper (#9395)
This PR adds `Sentry.withMonitor`, a wrapping function similar to `Sentry.startSpan` that wraps a callback with a cron monitor. Under the hood it uses `Sentry.captureCheckIn`, but having this as a callback means that users don't have to think about passing `checkInId` around. ```ts import * as Sentry from '@sentry/node'; // with monitor will send checkin when callback is started/finished // works with async and sync callbacks. const result = Sentry.withMonitor( 'dailyEmail', () => { // withCheckIn return value is same return value here return sendEmail(); }, { schedule: { type: 'crontab', value: '0 * * * *', }, // 🇨🇦🫡 timezone: 'Canada/Eastern', }, ); ```
1 parent 3545fd5 commit 697f406

File tree

13 files changed

+57
-3
lines changed

13 files changed

+57
-3
lines changed

packages/astro/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export {
1313
captureEvent,
1414
captureMessage,
1515
captureCheckIn,
16+
withMonitor,
1617
configureScope,
1718
createTransport,
1819
extractTraceparentData,

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export {
5555
trace,
5656
withScope,
5757
captureCheckIn,
58+
withMonitor,
5859
setMeasurement,
5960
getActiveSpan,
6061
startSpan,

packages/core/src/exports.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import type {
77
EventHint,
88
Extra,
99
Extras,
10+
FinishedCheckIn,
1011
MonitorConfig,
1112
Primitive,
1213
Severity,
1314
SeverityLevel,
1415
TransactionContext,
1516
User,
1617
} from '@sentry/types';
17-
import { logger, uuid4 } from '@sentry/utils';
18+
import { isThenable, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
1819

1920
import type { Hub } from './hub';
2021
import { getCurrentHub } from './hub';
@@ -210,6 +211,49 @@ export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorCo
210211
return uuid4();
211212
}
212213

214+
/**
215+
* Wraps a callback with a cron monitor check in. The check in will be sent to Sentry when the callback finishes.
216+
*
217+
* @param monitorSlug The distinct slug of the monitor.
218+
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
219+
* to create a monitor automatically when sending a check in.
220+
*/
221+
export function withMonitor<T>(
222+
monitorSlug: CheckIn['monitorSlug'],
223+
callback: () => T,
224+
upsertMonitorConfig?: MonitorConfig,
225+
): T {
226+
const checkInId = captureCheckIn({ monitorSlug, status: 'in_progress' }, upsertMonitorConfig);
227+
const now = timestampInSeconds();
228+
229+
function finishCheckIn(status: FinishedCheckIn['status']): void {
230+
captureCheckIn({ monitorSlug, status, checkInId, duration: timestampInSeconds() - now });
231+
}
232+
233+
let maybePromiseResult: T;
234+
try {
235+
maybePromiseResult = callback();
236+
} catch (e) {
237+
finishCheckIn('error');
238+
throw e;
239+
}
240+
241+
if (isThenable(maybePromiseResult)) {
242+
Promise.resolve(maybePromiseResult).then(
243+
() => {
244+
finishCheckIn('ok');
245+
},
246+
() => {
247+
finishCheckIn('error');
248+
},
249+
);
250+
} else {
251+
finishCheckIn('ok');
252+
}
253+
254+
return maybePromiseResult;
255+
}
256+
213257
/**
214258
* Call `flush()` on the current client, if there is one. See {@link Client.flush}.
215259
*

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from './tracing';
77
export {
88
addBreadcrumb,
99
captureCheckIn,
10+
withMonitor,
1011
captureException,
1112
captureEvent,
1213
captureMessage,

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export {
5353
trace,
5454
withScope,
5555
captureCheckIn,
56+
withMonitor,
5657
setMeasurement,
5758
getActiveSpan,
5859
startSpan,

packages/node-experimental/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export {
5151
trace,
5252
withScope,
5353
captureCheckIn,
54+
withMonitor,
5455
} from '@sentry/node';
5556

5657
export type {

packages/node/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export {
5555
trace,
5656
withScope,
5757
captureCheckIn,
58+
withMonitor,
5859
setMeasurement,
5960
getActiveSpan,
6061
startSpan,

packages/remix/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export {
1313
addGlobalEventProcessor,
1414
addBreadcrumb,
1515
captureCheckIn,
16+
withMonitor,
1617
captureException,
1718
captureEvent,
1819
captureMessage,

packages/serverless/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
captureException,
2222
captureMessage,
2323
captureCheckIn,
24+
withMonitor,
2425
configureScope,
2526
createTransport,
2627
getActiveTransaction,

packages/sveltekit/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
captureEvent,
1212
captureMessage,
1313
captureCheckIn,
14+
withMonitor,
1415
configureScope,
1516
createTransport,
1617
extractTraceparentData,

packages/types/src/checkin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface SerializedCheckIn {
4343
};
4444
}
4545

46-
interface InProgressCheckIn {
46+
export interface InProgressCheckIn {
4747
// The distinct slug of the monitor.
4848
monitorSlug: SerializedCheckIn['monitor_slug'];
4949
// The status of the check-in.

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,4 @@ export type { Instrumenter } from './instrumenter';
125125
export type { HandlerDataFetch, HandlerDataXhr, SentryXhrData, SentryWrappedXMLHttpRequest } from './instrument';
126126

127127
export type { BrowserClientReplayOptions, BrowserClientProfilingOptions } from './browseroptions';
128-
export type { CheckIn, MonitorConfig, SerializedCheckIn } from './checkin';
128+
export type { CheckIn, MonitorConfig, FinishedCheckIn, InProgressCheckIn, SerializedCheckIn } from './checkin';

packages/vercel-edge/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export {
5454
trace,
5555
withScope,
5656
captureCheckIn,
57+
withMonitor,
5758
setMeasurement,
5859
getActiveSpan,
5960
startSpan,

0 commit comments

Comments
 (0)