@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
75 lines (74 loc) • 2.73 kB
JavaScript
import { getClient, getCurrentScope, withIsolationScope } from "./currentScopes.js";
import { DEBUG_BUILD } from "./debug-build.js";
import { startNewTrace } from "./tracing/trace.js";
import { debug } from "./utils/debug-logger.js";
import { isThenable } from "./utils/is.js";
import { uuid4 } from "./utils/misc.js";
import { timestampInSeconds } from "./utils/time.js";
import { isContinuingTrace } from "./utils/tracing.js";
// src/monitor.ts
/**
* 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 callback Callback to be monitored
* @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.
*/
function withMonitor(monitorSlug, callback, upsertMonitorConfig) {
function runCallback() {
const checkInId = captureCheckIn({
monitorSlug,
status: "in_progress"
}, upsertMonitorConfig);
const now = timestampInSeconds();
function finishCheckIn(status) {
captureCheckIn({
monitorSlug,
status,
checkInId,
duration: timestampInSeconds() - now
});
}
let maybePromiseResult;
try {
maybePromiseResult = callback();
} catch (e) {
finishCheckIn("error");
throw e;
}
if (isThenable(maybePromiseResult)) return maybePromiseResult.then((r) => {
finishCheckIn("ok");
return r;
}, (e) => {
finishCheckIn("error");
throw e;
});
finishCheckIn("ok");
return maybePromiseResult;
}
const oldPropagationContext = { ...getCurrentScope().getPropagationContext() };
return withIsolationScope(() => {
if (upsertMonitorConfig?.isolateTrace) return startNewTrace(runCallback);
const scope = getCurrentScope();
if (!isContinuingTrace(scope.getPropagationContext())) scope.setPropagationContext(oldPropagationContext);
return runCallback();
});
}
/**
* Create a cron monitor check in and send it to Sentry.
*
* @param checkIn An object that describes a check in.
* @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.
*/
function captureCheckIn(checkIn, upsertMonitorConfig) {
const scope = getCurrentScope();
const client = getClient();
if (!client) DEBUG_BUILD && debug.warn("Cannot capture check-in. No client defined.");
else if (!client.captureCheckIn) DEBUG_BUILD && debug.warn("Cannot capture check-in. Client does not support sending check-ins.");
else return client.captureCheckIn(checkIn, upsertMonitorConfig, scope);
return uuid4();
}
export { captureCheckIn, withMonitor };
//# sourceMappingURL=monitor.js.map