diff --git a/packages/astro/src/index.server.ts b/packages/astro/src/index.server.ts index 7a28bf907d48..2d174277b0af 100644 --- a/packages/astro/src/index.server.ts +++ b/packages/astro/src/index.server.ts @@ -13,6 +13,7 @@ export { captureEvent, captureMessage, captureCheckIn, + withMonitor, configureScope, createTransport, extractTraceparentData, diff --git a/packages/bun/src/index.ts b/packages/bun/src/index.ts index c8428ab8e106..1f10a0d265e8 100644 --- a/packages/bun/src/index.ts +++ b/packages/bun/src/index.ts @@ -55,6 +55,7 @@ export { trace, withScope, captureCheckIn, + withMonitor, setMeasurement, getActiveSpan, startSpan, diff --git a/packages/core/src/exports.ts b/packages/core/src/exports.ts index 1a143a2efd4e..6569bc4e4c25 100644 --- a/packages/core/src/exports.ts +++ b/packages/core/src/exports.ts @@ -7,6 +7,7 @@ import type { EventHint, Extra, Extras, + FinishedCheckIn, MonitorConfig, Primitive, Severity, @@ -14,7 +15,7 @@ import type { TransactionContext, User, } from '@sentry/types'; -import { logger, uuid4 } from '@sentry/utils'; +import { isThenable, logger, timestampInSeconds, uuid4 } from '@sentry/utils'; import type { Hub } from './hub'; import { getCurrentHub } from './hub'; @@ -210,6 +211,49 @@ export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorCo return uuid4(); } +/** + * Wraps a callback with a cron monitor check in. The check in will be sent to Sentry when the callback finishes. + * + * @param monitorSlug The distinct slug of the monitor. + * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want + * to create a monitor automatically when sending a check in. + */ +export function withMonitor( + monitorSlug: CheckIn['monitorSlug'], + callback: () => T, + upsertMonitorConfig?: MonitorConfig, +): T { + const checkInId = captureCheckIn({ monitorSlug, status: 'in_progress' }, upsertMonitorConfig); + const now = timestampInSeconds(); + + function finishCheckIn(status: FinishedCheckIn['status']): void { + captureCheckIn({ monitorSlug, status, checkInId, duration: timestampInSeconds() - now }); + } + + let maybePromiseResult: T; + try { + maybePromiseResult = callback(); + } catch (e) { + finishCheckIn('error'); + throw e; + } + + if (isThenable(maybePromiseResult)) { + Promise.resolve(maybePromiseResult).then( + () => { + finishCheckIn('ok'); + }, + () => { + finishCheckIn('error'); + }, + ); + } else { + finishCheckIn('ok'); + } + + return maybePromiseResult; +} + /** * Call `flush()` on the current client, if there is one. See {@link Client.flush}. * diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index f14f5d4aaf2f..b80c83cdfdfa 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -7,6 +7,7 @@ export * from './tracing'; export { addBreadcrumb, captureCheckIn, + withMonitor, captureException, captureEvent, captureMessage, diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index 6a92adea2513..52e881528866 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -53,6 +53,7 @@ export { trace, withScope, captureCheckIn, + withMonitor, setMeasurement, getActiveSpan, startSpan, diff --git a/packages/node-experimental/src/index.ts b/packages/node-experimental/src/index.ts index e19b1231712f..798a474702c1 100644 --- a/packages/node-experimental/src/index.ts +++ b/packages/node-experimental/src/index.ts @@ -51,6 +51,7 @@ export { trace, withScope, captureCheckIn, + withMonitor, } from '@sentry/node'; export type { diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index b0ab9dffefcb..ff49247ce337 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -55,6 +55,7 @@ export { trace, withScope, captureCheckIn, + withMonitor, setMeasurement, getActiveSpan, startSpan, diff --git a/packages/remix/src/index.server.ts b/packages/remix/src/index.server.ts index 53757b4a89f4..42034876d269 100644 --- a/packages/remix/src/index.server.ts +++ b/packages/remix/src/index.server.ts @@ -13,6 +13,7 @@ export { addGlobalEventProcessor, addBreadcrumb, captureCheckIn, + withMonitor, captureException, captureEvent, captureMessage, diff --git a/packages/serverless/src/index.ts b/packages/serverless/src/index.ts index 62fc55012719..3fc9074b224c 100644 --- a/packages/serverless/src/index.ts +++ b/packages/serverless/src/index.ts @@ -21,6 +21,7 @@ export { captureException, captureMessage, captureCheckIn, + withMonitor, configureScope, createTransport, getActiveTransaction, diff --git a/packages/sveltekit/src/server/index.ts b/packages/sveltekit/src/server/index.ts index f81cedd8444b..e3a6f0fc2be7 100644 --- a/packages/sveltekit/src/server/index.ts +++ b/packages/sveltekit/src/server/index.ts @@ -11,6 +11,7 @@ export { captureEvent, captureMessage, captureCheckIn, + withMonitor, configureScope, createTransport, extractTraceparentData, diff --git a/packages/types/src/checkin.ts b/packages/types/src/checkin.ts index b19ff7b78770..786285554495 100644 --- a/packages/types/src/checkin.ts +++ b/packages/types/src/checkin.ts @@ -43,7 +43,7 @@ export interface SerializedCheckIn { }; } -interface InProgressCheckIn { +export interface InProgressCheckIn { // The distinct slug of the monitor. monitorSlug: SerializedCheckIn['monitor_slug']; // The status of the check-in. diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 26a2a4f1328b..a8a9531a6490 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -125,4 +125,4 @@ export type { Instrumenter } from './instrumenter'; export type { HandlerDataFetch, HandlerDataXhr, SentryXhrData, SentryWrappedXMLHttpRequest } from './instrument'; export type { BrowserClientReplayOptions, BrowserClientProfilingOptions } from './browseroptions'; -export type { CheckIn, MonitorConfig, SerializedCheckIn } from './checkin'; +export type { CheckIn, MonitorConfig, FinishedCheckIn, InProgressCheckIn, SerializedCheckIn } from './checkin'; diff --git a/packages/vercel-edge/src/index.ts b/packages/vercel-edge/src/index.ts index bba58f568db0..47e1b9fd7924 100644 --- a/packages/vercel-edge/src/index.ts +++ b/packages/vercel-edge/src/index.ts @@ -54,6 +54,7 @@ export { trace, withScope, captureCheckIn, + withMonitor, setMeasurement, getActiveSpan, startSpan,