@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
489 lines (488 loc) • 13.5 kB
JavaScript
import { DEBUG_BUILD } from "./debug-build.js";
import { updateSession } from "./session.js";
import { debug } from "./utils/debug-logger.js";
import { isPlainObject } from "./utils/is.js";
import { addNonEnumerableProperty } from "./utils/object.js";
import { merge } from "./utils/merge.js";
import { uuid4 } from "./utils/misc.js";
import { generateTraceId } from "./utils/propagationContext.js";
import { safeMathRandom } from "./utils/randomSafeContext.js";
import { truncate } from "./utils/string.js";
import { dateTimestampInSeconds } from "./utils/time.js";
// src/scope.ts
/**
* Default value for maximum number of breadcrumbs added to an event.
*/
const DEFAULT_MAX_BREADCRUMBS = 100;
/**
* Holds additional event information.
*/
var Scope = class Scope {
constructor() {
this._notifyingListeners = false;
this._scopeListeners = [];
this._eventProcessors = [];
this._breadcrumbs = [];
this._attachments = [];
this._user = {};
this._tags = {};
this._attributes = {};
this._extra = {};
this._contexts = {};
this._sdkProcessingMetadata = {};
addNonEnumerableProperty(this, "refs", {});
this._propagationContext = {
traceId: generateTraceId(),
sampleRand: safeMathRandom()
};
}
/**
* Clone all data from this scope into a new scope.
*/
clone() {
const newScope = new Scope();
newScope._breadcrumbs = [...this._breadcrumbs];
newScope._tags = { ...this._tags };
newScope._attributes = { ...this._attributes };
newScope._extra = { ...this._extra };
newScope._contexts = { ...this._contexts };
if (this._contexts.flags) newScope._contexts.flags = { values: [...this._contexts.flags.values] };
newScope._user = this._user;
newScope._level = this._level;
newScope._session = this._session;
newScope._transactionName = this._transactionName;
newScope._fingerprint = this._fingerprint;
newScope._eventProcessors = [...this._eventProcessors];
newScope._attachments = [...this._attachments];
newScope._sdkProcessingMetadata = { ...this._sdkProcessingMetadata };
newScope._propagationContext = { ...this._propagationContext };
newScope._client = this._client;
newScope._lastEventId = this._lastEventId;
newScope._conversationId = this._conversationId;
newScope.refs = { ...this.refs };
return newScope;
}
/**
* Update the client assigned to this scope.
* Note that not every scope will have a client assigned - isolation scopes & the global scope will generally not have a client,
* as well as manually created scopes.
*/
setClient(client) {
this._client = client;
}
/**
* Set the ID of the last captured error event.
* This is generally only captured on the isolation scope.
*/
setLastEventId(lastEventId) {
this._lastEventId = lastEventId;
}
/**
* Get the client assigned to this scope.
*/
getClient() {
return this._client;
}
/**
* Get the ID of the last captured error event.
* This is generally only available on the isolation scope.
*/
lastEventId() {
return this._lastEventId;
}
/**
* @inheritDoc
*/
addScopeListener(callback) {
this._scopeListeners.push(callback);
}
/**
* Add an event processor that will be called before an event is sent.
*/
addEventProcessor(callback) {
this._eventProcessors.push(callback);
return this;
}
/**
* Set the user for this scope.
* Set to `null` to unset the user.
*/
setUser(user) {
this._user = user || {
email: void 0,
id: void 0,
ip_address: void 0,
username: void 0
};
if (this._session) updateSession(this._session, { user });
this._notifyScopeListeners();
return this;
}
/**
* Get the user from this scope.
*/
getUser() {
return this._user;
}
/**
* Set the conversation ID for this scope.
* Set to `null` to unset the conversation ID.
*/
setConversationId(conversationId) {
this._conversationId = conversationId || void 0;
this._notifyScopeListeners();
return this;
}
/**
* Set an object that will be merged into existing tags on the scope,
* and will be sent as tags data with the event.
*/
setTags(tags) {
this._tags = {
...this._tags,
...tags
};
this._notifyScopeListeners();
return this;
}
/**
* Set a single tag that will be sent as tags data with the event.
*/
setTag(key, value) {
return this.setTags({ [key]: value });
}
/**
* Sets attributes onto the scope.
*
* These attributes are applied to logs, metrics and streamed spans.
*
* Supported attribute value types are `string`, `number`, `boolean`, `string[]`, `number[]` and `boolean[]`.
*
* @param newAttributes - The attributes to set on the scope, as key-value pairs.
*
* @example
* ```typescript
* scope.setAttributes({
* is_admin: true,
* payment_selection: 'credit_card',
* render_duration: 150,
* });
* ```
*/
setAttributes(newAttributes) {
this._attributes = {
...this._attributes,
...newAttributes
};
this._notifyScopeListeners();
return this;
}
/**
* Sets an attribute onto the scope.
*
* These attributes are applied to logs, metrics and streamed spans.
*
* Supported attribute value types are `string`, `number`, `boolean`, `string[]`, `number[]` and `boolean[]`.
*
* @param key - The attribute key.
* @param value - The attribute value.
*
* @example
* ```typescript
* scope.setAttribute('is_admin', true);
* scope.setAttribute('render_duration', 150);
* ```
*/
setAttribute(key, value) {
return this.setAttributes({ [key]: value });
}
/**
* Removes the attribute with the given key from the scope.
*
* @param key - The attribute key.
*
* @example
* ```typescript
* scope.removeAttribute('is_admin');
* ```
*/
removeAttribute(key) {
if (key in this._attributes) {
delete this._attributes[key];
this._notifyScopeListeners();
}
return this;
}
/**
* Set an object that will be merged into existing extra on the scope,
* and will be sent as extra data with the event.
*/
setExtras(extras) {
this._extra = {
...this._extra,
...extras
};
this._notifyScopeListeners();
return this;
}
/**
* Set a single key:value extra entry that will be sent as extra data with the event.
*/
setExtra(key, extra) {
this._extra = {
...this._extra,
[key]: extra
};
this._notifyScopeListeners();
return this;
}
/**
* Sets the fingerprint on the scope to send with the events.
* @param {string[]} fingerprint Fingerprint to group events in Sentry.
*/
setFingerprint(fingerprint) {
this._fingerprint = fingerprint;
this._notifyScopeListeners();
return this;
}
/**
* Sets the level on the scope for future events.
*/
setLevel(level) {
this._level = level;
this._notifyScopeListeners();
return this;
}
/**
* Sets the transaction name on the scope so that the name of e.g. taken server route or
* the page location is attached to future events.
*
* IMPORTANT: Calling this function does NOT change the name of the currently active
* root span. If you want to change the name of the active root span, use
* `Sentry.updateSpanName(rootSpan, 'new name')` instead.
*
* By default, the SDK updates the scope's transaction name automatically on sensible
* occasions, such as a page navigation or when handling a new request on the server.
*/
setTransactionName(name) {
this._transactionName = name;
this._notifyScopeListeners();
return this;
}
/**
* Sets context data with the given name.
* Data passed as context will be normalized. You can also pass `null` to unset the context.
* Note that context data will not be merged - calling `setContext` will overwrite an existing context with the same key.
*/
setContext(key, context) {
if (context === null) delete this._contexts[key];
else this._contexts[key] = context;
this._notifyScopeListeners();
return this;
}
/**
* Set the session for the scope.
*/
setSession(session) {
if (!session) delete this._session;
else this._session = session;
this._notifyScopeListeners();
return this;
}
/**
* Get the session from the scope.
*/
getSession() {
return this._session;
}
/**
* Updates the scope with provided data. Can work in three variations:
* - plain object containing updatable attributes
* - Scope instance that'll extract the attributes from
* - callback function that'll receive the current scope as an argument and allow for modifications
*/
update(captureContext) {
if (!captureContext) return this;
const scopeToMerge = typeof captureContext === "function" ? captureContext(this) : captureContext;
const { tags, attributes, extra, user, contexts, level, fingerprint = [], propagationContext, conversationId } = (scopeToMerge instanceof Scope ? scopeToMerge.getScopeData() : isPlainObject(scopeToMerge) ? captureContext : void 0) || {};
this._tags = {
...this._tags,
...tags
};
this._attributes = {
...this._attributes,
...attributes
};
this._extra = {
...this._extra,
...extra
};
this._contexts = {
...this._contexts,
...contexts
};
if (user && Object.keys(user).length) this._user = user;
if (level) this._level = level;
if (fingerprint.length) this._fingerprint = fingerprint;
if (propagationContext) this._propagationContext = propagationContext;
if (conversationId) this._conversationId = conversationId;
return this;
}
/**
* Adds a breadcrumb to the scope.
* By default, the last 100 breadcrumbs are kept.
*/
addBreadcrumb(breadcrumb, maxBreadcrumbs) {
const maxCrumbs = typeof maxBreadcrumbs === "number" ? maxBreadcrumbs : DEFAULT_MAX_BREADCRUMBS;
if (maxCrumbs <= 0) return this;
const mergedBreadcrumb = {
timestamp: dateTimestampInSeconds(),
...breadcrumb,
message: breadcrumb.message ? truncate(breadcrumb.message, 2048) : breadcrumb.message
};
this._breadcrumbs.push(mergedBreadcrumb);
if (this._breadcrumbs.length > maxCrumbs) this._breadcrumbs = this._breadcrumbs.slice(-maxCrumbs);
this._notifyScopeListeners();
return this;
}
/**
* Get the last breadcrumb of the scope.
*/
getLastBreadcrumb() {
return this._breadcrumbs[this._breadcrumbs.length - 1];
}
/**
* Clear all breadcrumbs from the scope.
*/
clearBreadcrumbs() {
this._breadcrumbs = [];
this._notifyScopeListeners();
return this;
}
/**
* Add an attachment to the scope.
*/
addAttachment(attachment) {
this._attachments.push(attachment);
return this;
}
/**
* Clear all attachments from the scope.
*/
clearAttachments() {
this._attachments = [];
return this;
}
/**
* Get the data of this scope, which should be applied to an event during processing.
*/
getScopeData() {
return {
breadcrumbs: this._breadcrumbs,
attachments: this._attachments,
contexts: this._contexts,
tags: this._tags,
attributes: this._attributes,
extra: this._extra,
user: this._user,
level: this._level,
fingerprint: this._fingerprint || [],
eventProcessors: this._eventProcessors,
propagationContext: this._propagationContext,
sdkProcessingMetadata: this._sdkProcessingMetadata,
transactionName: this._transactionName,
conversationId: this._conversationId
};
}
/**
* Add data which will be accessible during event processing but won't get sent to Sentry.
*/
setSDKProcessingMetadata(newData) {
this._sdkProcessingMetadata = merge(this._sdkProcessingMetadata, newData, 2);
return this;
}
/**
* Add propagation context to the scope, used for distributed tracing
*/
setPropagationContext(context) {
this._propagationContext = context;
return this;
}
/**
* Get propagation context from the scope, used for distributed tracing
*/
getPropagationContext() {
return this._propagationContext;
}
/**
* Capture an exception for this scope.
*
* @returns {string} The id of the captured Sentry event.
*/
captureException(exception, hint) {
const eventId = hint?.event_id || uuid4();
if (!this._client) {
DEBUG_BUILD && debug.warn("No client configured on scope - will not capture exception!");
return eventId;
}
const syntheticException = /* @__PURE__ */ new Error("Sentry syntheticException");
this._client.captureException(exception, {
originalException: exception,
syntheticException,
...hint,
event_id: eventId
}, this);
return eventId;
}
/**
* Capture a message for this scope.
*
* @returns {string} The id of the captured message.
*/
captureMessage(message, level, hint) {
const eventId = hint?.event_id || uuid4();
if (!this._client) {
DEBUG_BUILD && debug.warn("No client configured on scope - will not capture message!");
return eventId;
}
const syntheticException = hint?.syntheticException ?? new Error(message);
this._client.captureMessage(message, level, {
originalException: message,
syntheticException,
...hint,
event_id: eventId
}, this);
return eventId;
}
/**
* Capture a Sentry event for this scope.
*
* @returns {string} The id of the captured event.
*/
captureEvent(event, hint) {
const eventId = event.event_id || hint?.event_id || uuid4();
if (!this._client) {
DEBUG_BUILD && debug.warn("No client configured on scope - will not capture event!");
return eventId;
}
this._client.captureEvent(event, {
...hint,
event_id: eventId
}, this);
return eventId;
}
/**
* This will be called on every set call.
*/
_notifyScopeListeners() {
if (!this._notifyingListeners) {
this._notifyingListeners = true;
this._scopeListeners.forEach((callback) => {
callback(this);
});
this._notifyingListeners = false;
}
}
};
export { Scope };
//# sourceMappingURL=scope.js.map