package.build.cjs.envelope.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Base implementation for all Sentry JavaScript SDKs
Object.defineProperty(exports, '__esModule', { value: true });
const utils = require('@sentry/utils');
const dynamicSamplingContext = require('./tracing/dynamicSamplingContext.js');
const spanUtils = require('./utils/spanUtils.js');
/**
* Apply SdkInfo (name, version, packages, integrations) to the corresponding event key.
* Merge with existing data if any.
**/
function enhanceEventWithSdkInfo(event, sdkInfo) {
if (!sdkInfo) {
return event;
}
event.sdk = event.sdk || {};
event.sdk.name = event.sdk.name || sdkInfo.name;
event.sdk.version = event.sdk.version || sdkInfo.version;
event.sdk.integrations = [...(event.sdk.integrations || []), ...(sdkInfo.integrations || [])];
event.sdk.packages = [...(event.sdk.packages || []), ...(sdkInfo.packages || [])];
return event;
}
/** Creates an envelope from a Session */
function createSessionEnvelope(
session,
dsn,
metadata,
tunnel,
) {
const sdkInfo = utils.getSdkMetadataForEnvelopeHeader(metadata);
const envelopeHeaders = {
sent_at: new Date().toISOString(),
...(sdkInfo && { sdk: sdkInfo }),
...(!!tunnel && dsn && { dsn: utils.dsnToString(dsn) }),
};
const envelopeItem =
'aggregates' in session ? [{ type: 'sessions' }, session] : [{ type: 'session' }, session.toJSON()];
return utils.createEnvelope(envelopeHeaders, [envelopeItem]);
}
/**
* Create an Envelope from an event.
*/
function createEventEnvelope(
event,
dsn,
metadata,
tunnel,
) {
const sdkInfo = utils.getSdkMetadataForEnvelopeHeader(metadata);
/*
Note: Due to TS, event.type may be `replay_event`, theoretically.
In practice, we never call `createEventEnvelope` with `replay_event` type,
and we'd have to adjut a looot of types to make this work properly.
We want to avoid casting this around, as that could lead to bugs (e.g. when we add another type)
So the safe choice is to really guard against the replay_event type here.
*/
const eventType = event.type && event.type !== 'replay_event' ? event.type : 'event';
enhanceEventWithSdkInfo(event, metadata && metadata.sdk);
const envelopeHeaders = utils.createEventEnvelopeHeaders(event, sdkInfo, tunnel, dsn);
// Prevent this data (which, if it exists, was used in earlier steps in the processing pipeline) from being sent to
// sentry. (Note: Our use of this property comes and goes with whatever we might be debugging, whatever hacks we may
// have temporarily added, etc. Even if we don't happen to be using it at some point in the future, let's not get rid
// of this `delete`, lest we miss putting it back in the next time the property is in use.)
delete event.sdkProcessingMetadata;
const eventItem = [{ type: eventType }, event];
return utils.createEnvelope(envelopeHeaders, [eventItem]);
}
/**
* Create envelope from Span item.
*
* Takes an optional client and runs spans through `beforeSendSpan` if available.
*/
function createSpanEnvelope(spans, client) {
function dscHasRequiredProps(dsc) {
return !!dsc.trace_id && !!dsc.public_key;
}
// For the moment we'll obtain the DSC from the first span in the array
// This might need to be changed if we permit sending multiple spans from
// different segments in one envelope
const dsc = dynamicSamplingContext.getDynamicSamplingContextFromSpan(spans[0]);
const dsn = client && client.getDsn();
const tunnel = client && client.getOptions().tunnel;
const headers = {
sent_at: new Date().toISOString(),
...(dscHasRequiredProps(dsc) && { trace: dsc }),
...(!!tunnel && dsn && { dsn: utils.dsnToString(dsn) }),
};
const beforeSendSpan = client && client.getOptions().beforeSendSpan;
const convertToSpanJSON = beforeSendSpan
? (span) => beforeSendSpan(spanUtils.spanToJSON(span) )
: (span) => spanUtils.spanToJSON(span);
const items = [];
for (const span of spans) {
const spanJson = convertToSpanJSON(span);
if (spanJson) {
items.push(utils.createSpanEnvelopeItem(spanJson));
}
}
return utils.createEnvelope(headers, items);
}
exports.createEventEnvelope = createEventEnvelope;
exports.createSessionEnvelope = createSessionEnvelope;
exports.createSpanEnvelope = createSpanEnvelope;
//# sourceMappingURL=envelope.js.map