UNPKG

@sentry/core

Version:
207 lines (206 loc) • 8.48 kB
import "../constants.js"; import { notifyEventProcessors } from "../eventProcessors.js"; import { Scope } from "../scope.js"; import { getFilenameToDebugIdMap } from "./debug-ids.js"; import { getDataCategoryByType } from "./envelope.js"; import { applyEscapedErrorSpanToEvent } from "./errorSpanAttribution.js"; import { isInternalException } from "./eventUtils.js"; import { addExceptionMechanismToCapturedException, uuid4 } from "./misc.js"; import { normalize } from "./normalize.js"; import { applyScopeDataToEvent, applySpanToEvent, getCombinedScopeData } from "./scopeData.js"; import { getActiveSpan } from "./spanUtils.js"; import { truncate } from "./string.js"; import { resolvedSyncPromise } from "./syncpromise.js"; import { dateTimestampInSeconds } from "./time.js"; // src/utils/prepareEvent.ts /** * Adds common information to events. * * The information includes release and environment from `options`, * breadcrumbs and context (extra, tags and user) from the scope. * * Information that is already present in the event is never overwritten. For * nested objects, such as the context, keys are merged. * * @param event The original event. * @param hint May contain additional information about the original exception. * @param scope A scope containing event metadata. * @returns A new event with more information, or `null` if an event processor dropped it (or threw). In that case the * drop has already been recorded on the client, so callers must not record it again. * @hidden */ function prepareEvent(options, event, hint, scope, client, isolationScope) { const { normalizeDepth = 3, normalizeMaxBreadth = 1e3 } = options; const prepared = { ...event, event_id: event.event_id || hint.event_id || uuid4(), timestamp: event.timestamp || dateTimestampInSeconds() }; const integrations = hint.integrations || options.integrations.map((i) => i.name); applyClientOptions(prepared, options); applyIntegrationsMetadata(prepared, integrations); if (client) client.emit("applyFrameMetadata", event); if (event.type === void 0) applyDebugIds(prepared, options.stackParser); const finalScope = getFinalScope(scope, hint.captureContext); if (hint.mechanism) addExceptionMechanismToCapturedException(prepared, hint.mechanism); const clientEventProcessors = client ? client.getEventProcessors() : []; const data = getCombinedScopeData(isolationScope, finalScope); const attachments = [...hint.attachments || [], ...data.attachments]; if (attachments.length) hint.attachments = attachments; applyScopeDataToEvent(prepared, data); const span = getActiveSpan(finalScope); if (span) applySpanToEvent(prepared, span); applyEscapedErrorSpanToEvent(prepared, hint, finalScope); const eventProcessors = [...clientEventProcessors, ...data.eventProcessors]; const isTransaction = event.type === "transaction"; const spanCountBeforeProcessing = event.spans?.length || 0; return (isInternalException(hint) ? resolvedSyncPromise(prepared) : notifyEventProcessors(eventProcessors, prepared, hint, 0, (reason) => { if (!client) return; client.recordDroppedEvent(reason, getDataCategoryByType(event.type)); if (isTransaction) client.recordDroppedEvent(reason, "span", 1 + spanCountBeforeProcessing); })).then((evt) => { if (!evt) return null; if (isTransaction && client) { const droppedSpanCount = spanCountBeforeProcessing - (evt.spans?.length || 0); if (droppedSpanCount > 0) client.recordDroppedEvent("event_processor", "span", droppedSpanCount); } applyDebugMeta(evt); if (typeof normalizeDepth === "number" && normalizeDepth > 0) return normalizeEvent(evt, normalizeDepth, normalizeMaxBreadth); return evt; }); } /** * Enhances event using the client configuration. * It takes care of all "static" values like environment, release and `dist`, * as well as truncating overly long values. * * Only exported for tests. * * @param event event instance to be enhanced */ function applyClientOptions(event, options) { const { environment, release, dist, maxValueLength } = options; event.environment = event.environment || environment || "production"; if (!event.release && release) event.release = release; if (!event.dist && dist) event.dist = dist; const request = event.request; if (request?.url && maxValueLength) request.url = truncate(request.url, maxValueLength); if (maxValueLength) event.exception?.values?.forEach((exception) => { if (exception.value) exception.value = truncate(exception.value, maxValueLength); }); } /** * Puts debug IDs into the stack frames of an error event. */ function applyDebugIds(event, stackParser) { const filenameDebugIdMap = getFilenameToDebugIdMap(stackParser); event.exception?.values?.forEach((exception) => { exception.stacktrace?.frames?.forEach((frame) => { if (frame.filename) frame.debug_id = filenameDebugIdMap[frame.filename]; }); }); } /** * Moves debug IDs from the stack frames of an error event into the debug_meta field. */ function applyDebugMeta(event) { const filenameDebugIdMap = {}; event.exception?.values?.forEach((exception) => { exception.stacktrace?.frames?.forEach((frame) => { if (frame.debug_id) { if (frame.abs_path) filenameDebugIdMap[frame.abs_path] = frame.debug_id; else if (frame.filename) filenameDebugIdMap[frame.filename] = frame.debug_id; delete frame.debug_id; } }); }); if (Object.keys(filenameDebugIdMap).length === 0) return; event.debug_meta = event.debug_meta || {}; event.debug_meta.images = event.debug_meta.images || []; const images = event.debug_meta.images; Object.entries(filenameDebugIdMap).forEach(([filename, debug_id]) => { images.push({ type: "sourcemap", code_file: filename, debug_id }); }); } /** * This function adds all used integrations to the SDK info in the event. * @param event The event that will be filled with all integrations. */ function applyIntegrationsMetadata(event, integrationNames) { if (integrationNames.length > 0) { event.sdk = event.sdk || {}; event.sdk.integrations = [...event.sdk.integrations || [], ...integrationNames]; } } /** * Applies `normalize` function on necessary `Event` attributes to make them safe for serialization. * Normalized keys: * - `breadcrumbs.data` * - `user` * - `contexts` * - `extra` * @param event Event * @returns Normalized event */ function normalizeEvent(event, depth, maxBreadth) { if (!event) return null; const normalized = { ...event, ...event.breadcrumbs && { breadcrumbs: event.breadcrumbs.map((b) => ({ ...b, ...b.data && { data: normalize(b.data, depth, maxBreadth) } })) }, ...event.user && { user: normalize(event.user, depth, maxBreadth) }, ...event.contexts && { contexts: normalize(event.contexts, depth, maxBreadth) }, ...event.extra && { extra: normalize(event.extra, depth, maxBreadth) } }; if (event.contexts?.trace && normalized.contexts) { normalized.contexts.trace = event.contexts.trace; if (event.contexts.trace.data) normalized.contexts.trace.data = normalize(event.contexts.trace.data, depth, maxBreadth); } if (event.spans) normalized.spans = event.spans.map((span) => { return { ...span, ...span.data && { data: normalize(span.data, depth, maxBreadth) } }; }); if (event.contexts?.flags && normalized.contexts) normalized.contexts.flags = normalize(event.contexts.flags, 3, maxBreadth); return normalized; } function getFinalScope(scope, captureContext) { if (!captureContext) return scope; const finalScope = scope ? scope.clone() : new Scope(); finalScope.update(captureContext); return finalScope; } /** * Parse either an `EventHint` directly, or convert a `CaptureContext` to an `EventHint`. * This is used to allow to update method signatures that used to accept a `CaptureContext` but should now accept an `EventHint`. */ function parseEventHintOrCaptureContext(hint) { if (!hint) return; if (hintIsScopeOrFunction(hint)) return { captureContext: hint }; if (hintIsScopeContext(hint)) return { captureContext: hint }; return hint; } function hintIsScopeOrFunction(hint) { return hint instanceof Scope || typeof hint === "function"; } const captureContextKeys = [ "user", "level", "extra", "contexts", "tags", "fingerprint", "propagationContext" ]; function hintIsScopeContext(hint) { return Object.keys(hint).some((key) => captureContextKeys.includes(key)); } export { applyClientOptions, applyDebugIds, applyDebugMeta, parseEventHintOrCaptureContext, prepareEvent }; //# sourceMappingURL=prepareEvent.js.map