UNPKG

@sentry/core

Version:
311 lines (283 loc) 10.2 kB
Object.defineProperty(exports, '__esModule', { value: true }); const utils = require('@sentry/utils'); const hub = require('./hub.js'); const prepareEvent = require('./utils/prepareEvent.js'); // Note: All functions in this file are typed with a return value of `ReturnType<Hub[HUB_FUNCTION]>`, // where HUB_FUNCTION is some method on the Hub class. // // This is done to make sure the top level SDK methods stay in sync with the hub methods. // Although every method here has an explicit return type, some of them (that map to void returns) do not // contain `return` keywords. This is done to save on bundle size, as `return` is not minifiable. /** * Captures an exception event and sends it to Sentry. * This accepts an event hint as optional second parameter. * Alternatively, you can also pass a CaptureContext directly as second parameter. */ function captureException( // eslint-disable-next-line @typescript-eslint/no-explicit-any exception, hint, ) { return hub.getCurrentHub().captureException(exception, prepareEvent.parseEventHintOrCaptureContext(hint)); } /** * Captures a message event and sends it to Sentry. * * @param message The message to send to Sentry. * @param Severity Define the level of the message. * @returns The generated eventId. */ function captureMessage( message, // eslint-disable-next-line deprecation/deprecation captureContext, ) { // This is necessary to provide explicit scopes upgrade, without changing the original // arity of the `captureMessage(message, level)` method. const level = typeof captureContext === 'string' ? captureContext : undefined; const context = typeof captureContext !== 'string' ? { captureContext } : undefined; return hub.getCurrentHub().captureMessage(message, level, context); } /** * Captures a manually created event and sends it to Sentry. * * @param event The event to send to Sentry. * @returns The generated eventId. */ function captureEvent(event, hint) { return hub.getCurrentHub().captureEvent(event, hint); } /** * Callback to set context information onto the scope. * @param callback Callback function that receives Scope. */ function configureScope(callback) { hub.getCurrentHub().configureScope(callback); } /** * Records a new breadcrumb which will be attached to future events. * * Breadcrumbs will be added to subsequent events to provide more context on * user's actions prior to an error or crash. * * @param breadcrumb The breadcrumb to record. */ function addBreadcrumb(breadcrumb) { hub.getCurrentHub().addBreadcrumb(breadcrumb); } /** * Sets context data with the given name. * @param name of the context * @param context Any kind of data. This data will be normalized. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any function setContext(name, context) { hub.getCurrentHub().setContext(name, context); } /** * Set an object that will be merged sent as extra data with the event. * @param extras Extras object to merge into current context. */ function setExtras(extras) { hub.getCurrentHub().setExtras(extras); } /** * Set key:value that will be sent as extra data with the event. * @param key String of extra * @param extra Any kind of data. This data will be normalized. */ function setExtra(key, extra) { hub.getCurrentHub().setExtra(key, extra); } /** * Set an object that will be merged sent as tags data with the event. * @param tags Tags context object to merge into current context. */ function setTags(tags) { hub.getCurrentHub().setTags(tags); } /** * Set key:value that will be sent as tags data with the event. * * Can also be used to unset a tag, by passing `undefined`. * * @param key String key of tag * @param value Value of tag */ function setTag(key, value) { hub.getCurrentHub().setTag(key, value); } /** * Updates user context information for future events. * * @param user User context object to be set in the current context. Pass `null` to unset the user. */ function setUser(user) { hub.getCurrentHub().setUser(user); } /** * Creates a new scope with and executes the given operation within. * The scope is automatically removed once the operation * finishes or throws. * * This is essentially a convenience function for: * * pushScope(); * callback(); * popScope(); * * @param callback that will be enclosed into push/popScope. */ function withScope(callback) { hub.getCurrentHub().withScope(callback); } /** * Starts a new `Transaction` and returns it. This is the entry point to manual tracing instrumentation. * * A tree structure can be built by adding child spans to the transaction, and child spans to other spans. To start a * new child span within the transaction or any span, call the respective `.startChild()` method. * * Every child span must be finished before the transaction is finished, otherwise the unfinished spans are discarded. * * The transaction must be finished with a call to its `.finish()` method, at which point the transaction with all its * finished child spans will be sent to Sentry. * * NOTE: This function should only be used for *manual* instrumentation. Auto-instrumentation should call * `startTransaction` directly on the hub. * * @param context Properties of the new `Transaction`. * @param customSamplingContext Information given to the transaction sampling function (along with context-dependent * default values). See {@link Options.tracesSampler}. * * @returns The transaction which was just started */ function startTransaction( context, customSamplingContext, ) { return hub.getCurrentHub().startTransaction({ ...context }, customSamplingContext); } /** * 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 hub$1 = hub.getCurrentHub(); const scope = hub$1.getScope(); const client = hub$1.getClient(); if (!client) { (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Cannot capture check-in. No client defined.'); } else if (!client.captureCheckIn) { (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Cannot capture check-in. Client does not support sending check-ins.'); } else { return client.captureCheckIn(checkIn, upsertMonitorConfig, scope); } return utils.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. */ function withMonitor( monitorSlug, callback, upsertMonitorConfig, ) { const checkInId = captureCheckIn({ monitorSlug, status: 'in_progress' }, upsertMonitorConfig); const now = utils.timestampInSeconds(); function finishCheckIn(status) { captureCheckIn({ monitorSlug, status, checkInId, duration: utils.timestampInSeconds() - now }); } let maybePromiseResult; try { maybePromiseResult = callback(); } catch (e) { finishCheckIn('error'); throw e; } if (utils.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}. * * @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause * the client to wait until all events are sent before resolving the promise. * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it * doesn't (or if there's no client defined). */ async function flush(timeout) { const client = getClient(); if (client) { return client.flush(timeout); } (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Cannot flush events. No client defined.'); return Promise.resolve(false); } /** * Call `close()` on the current client, if there is one. See {@link Client.close}. * * @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this * parameter will cause the client to wait until all events are sent before disabling itself. * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it * doesn't (or if there's no client defined). */ async function close(timeout) { const client = getClient(); if (client) { return client.close(timeout); } (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Cannot flush events and disable SDK. No client defined.'); return Promise.resolve(false); } /** * This is the getter for lastEventId. * * @returns The last event id of a captured event. */ function lastEventId() { return hub.getCurrentHub().lastEventId(); } /** * Get the currently active client. */ function getClient() { return hub.getCurrentHub().getClient(); } exports.addBreadcrumb = addBreadcrumb; exports.captureCheckIn = captureCheckIn; exports.captureEvent = captureEvent; exports.captureException = captureException; exports.captureMessage = captureMessage; exports.close = close; exports.configureScope = configureScope; exports.flush = flush; exports.getClient = getClient; exports.lastEventId = lastEventId; exports.setContext = setContext; exports.setExtra = setExtra; exports.setExtras = setExtras; exports.setTag = setTag; exports.setTags = setTags; exports.setUser = setUser; exports.startTransaction = startTransaction; exports.withMonitor = withMonitor; exports.withScope = withScope; //# sourceMappingURL=exports.js.map