Deprecated utilities package for Sentry JavaScript SDKs - all functionality moved to @sentry/core
—
DEPRECATED: Import all functions from @sentry/core instead of @sentry/utils.
Data packaging system for structuring and transmitting telemetry data to Sentry servers with metadata, headers, and efficient serialization.
Core envelope data structures and type definitions.
/**
* Envelope item types supported by Sentry
*/
type EnvelopeItemType =
| 'event'
| 'transaction'
| 'session'
| 'attachment'
| 'user_report'
| 'profile'
| 'replay_event'
| 'client_report'
| 'span';
/**
* Header information for envelope items
*/
interface EnvelopeItemHeader {
type: EnvelopeItemType;
length?: number;
filename?: string;
content_type?: string;
attachment_type?: string;
}
/**
* Envelope item containing header and payload
*/
interface EnvelopeItem<T = any> {
0: EnvelopeItemHeader;
1: T;
}
/**
* Envelope header with metadata
*/
interface EnvelopeHeader {
event_id?: string;
sent_at?: string;
sdk?: {
name: string;
version: string;
};
trace?: {
trace_id: string;
public_key?: string;
};
}
/**
* Complete envelope structure
*/
interface Envelope {
0: EnvelopeHeader;
1: EnvelopeItem[];
}Functions for creating and assembling envelopes.
/**
* Creates a new envelope with headers and optional items
* @param headers - Envelope headers
* @param items - Optional array of envelope items
* @returns New envelope
*/
function createEnvelope<E extends Envelope>(
headers: E[0],
items?: E[1]
): E;
/**
* Creates headers for event envelopes
* @param event - Event data
* @param tunnel - Optional tunnel configuration
* @param dsn - Data source name
* @returns Envelope headers
*/
function createEventEnvelopeHeaders(
event: Event,
tunnel?: string,
dsn?: string
): EnvelopeHeader;
/**
* Gets SDK metadata for envelope headers
* @param sdk - SDK information
* @returns SDK metadata object
*/
function getSdkMetadataForEnvelopeHeader(sdk: {
name: string;
version: string;
integrations?: string[];
packages?: Array<{ name: string; version: string }>;
}): { name: string; version: string };Functions for creating specific types of envelope items.
/**
* Creates an attachment envelope item
* @param attachment - Attachment data
* @returns Envelope item for the attachment
*/
function createAttachmentEnvelopeItem(
attachment: {
data: string | Uint8Array;
filename: string;
content_type?: string;
attachment_type?: string;
}
): EnvelopeItem;
/**
* Creates a span envelope item for performance monitoring
* @param span - Span data
* @returns Envelope item for the span
*/
function createSpanEnvelopeItem(span: any): EnvelopeItem;
/**
* Creates a client report envelope for SDK metrics
* @param discardedEvents - Array of discarded event information
* @param dsn - Data source name
* @param timestamp - Report timestamp
* @returns Complete client report envelope
*/
function createClientReportEnvelope(
discardedEvents: Array<{
reason: string;
category: string;
quantity: number;
}>,
dsn: string,
timestamp?: number
): Envelope;Functions for modifying and querying envelopes.
/**
* Adds an item to an existing envelope
* @param envelope - Target envelope
* @param newItem - Item to add
* @returns Modified envelope
*/
function addItemToEnvelope<E extends Envelope>(
envelope: E,
newItem: E[1][number]
): E;
/**
* Iterates over all items in an envelope
* @param envelope - Envelope to iterate
* @param callback - Function called for each item
* @returns True if iteration completed, false if callback returned false
*/
function forEachEnvelopeItem<E extends Envelope>(
envelope: E,
callback: (item: E[1][number], type?: EnvelopeItemType) => boolean | void,
): boolean;
/**
* Checks if envelope contains items of a specific type
* @param envelope - Envelope to check
* @param itemType - Item type to look for
* @returns True if envelope contains the specified item type
*/
function envelopeContainsItemType(
envelope: Envelope,
itemType: EnvelopeItemType
): boolean;
/**
* Converts envelope item type to data category for rate limiting
* @param itemType - Envelope item type
* @returns Corresponding data category
*/
function envelopeItemTypeToDataCategory(
itemType: EnvelopeItemType
): string;Functions for serializing and parsing envelope data for transmission.
/**
* Serializes an envelope to string format for transmission
* @param envelope - Envelope to serialize
* @returns Serialized envelope string
*/
function serializeEnvelope(envelope: Envelope): string;
/**
* Parses a serialized envelope string back to envelope object
* @param env - Serialized envelope string
* @returns Parsed envelope object
*/
function parseEnvelope(env: string): Envelope;Usage Examples:
import {
createEnvelope,
createEventEnvelopeHeaders,
addItemToEnvelope,
createAttachmentEnvelopeItem,
serializeEnvelope,
parseEnvelope
} from "@sentry/core";
// Create an envelope for an error event
const event = {
event_id: 'abc123',
message: 'Something went wrong',
level: 'error',
timestamp: Date.now() / 1000
};
const headers = createEventEnvelopeHeaders(event, undefined, 'https://key@sentry.io/project');
const envelope = createEnvelope(headers, [
[{ type: 'event' }, event]
]);
// Add an attachment to the envelope
const attachment = createAttachmentEnvelopeItem({
data: 'log file contents',
filename: 'app.log',
content_type: 'text/plain',
attachment_type: 'event.attachment'
});
const envelopeWithAttachment = addItemToEnvelope(envelope, attachment);
// Serialize for transmission
const serialized = serializeEnvelope(envelopeWithAttachment);
console.log('Serialized envelope:', serialized);
// Parse received envelope
const parsed = parseEnvelope(serialized);
console.log('Parsed envelope:', parsed);Complete pipeline for processing events into envelopes:
import {
createEnvelope,
createEventEnvelopeHeaders,
addItemToEnvelope,
createAttachmentEnvelopeItem,
serializeEnvelope,
forEachEnvelopeItem
} from "@sentry/core";
class EventProcessor {
processEvent(event: any, attachments: any[] = []): string {
// Create envelope headers
const headers = createEventEnvelopeHeaders(event);
// Create initial envelope with event
let envelope = createEnvelope(headers, [
[{ type: 'event' }, event]
]);
// Add attachments if present
for (const attachment of attachments) {
const attachmentItem = createAttachmentEnvelopeItem(attachment);
envelope = addItemToEnvelope(envelope, attachmentItem);
}
// Log envelope contents for debugging
forEachEnvelopeItem(envelope, (item, type) => {
console.log(`Envelope contains: ${type}`);
});
// Serialize for transmission
return serializeEnvelope(envelope);
}
}Processing multiple events in a single envelope:
import {
createEnvelope,
addItemToEnvelope,
envelopeContainsItemType,
serializeEnvelope
} from "@sentry/core";
class BatchProcessor {
createBatchEnvelope(events: any[]): Envelope {
// Create envelope with first event's headers
const firstEvent = events[0];
const headers = createEventEnvelopeHeaders(firstEvent);
let envelope = createEnvelope(headers);
// Add all events to the envelope
for (const event of events) {
envelope = addItemToEnvelope(envelope, [
{ type: 'event' },
event
]);
}
return envelope;
}
validateEnvelope(envelope: Envelope): boolean {
// Check that envelope has at least one event
if (!envelopeContainsItemType(envelope, 'event')) {
console.warn('Envelope contains no events');
return false;
}
// Additional validation logic
return true;
}
}Creating client reports for SDK metrics:
import { createClientReportEnvelope, serializeEnvelope } from "@sentry/core";
class MetricsReporter {
private discardedEvents: Array<{
reason: string;
category: string;
quantity: number;
}> = [];
recordDiscardedEvent(reason: string, category: string, quantity = 1) {
const existing = this.discardedEvents.find(
e => e.reason === reason && e.category === category
);
if (existing) {
existing.quantity += quantity;
} else {
this.discardedEvents.push({ reason, category, quantity });
}
}
generateReport(dsn: string): string {
const envelope = createClientReportEnvelope(
this.discardedEvents,
dsn,
Date.now()
);
// Clear the recorded events
this.discardedEvents.length = 0;
return serializeEnvelope(envelope);
}
}
// Usage
const reporter = new MetricsReporter();
reporter.recordDiscardedEvent('rate_limit', 'error', 5);
reporter.recordDiscardedEvent('invalid_data', 'transaction', 2);
const report = reporter.generateReport('https://key@sentry.io/project');Filtering envelope contents based on criteria:
import {
forEachEnvelopeItem,
createEnvelope,
envelopeItemTypeToDataCategory
} from "@sentry/core";
class EnvelopeFilter {
filterByCategory(envelope: Envelope, allowedCategories: string[]): Envelope {
const filteredItems: EnvelopeItem[] = [];
forEachEnvelopeItem(envelope, (item, type) => {
if (type) {
const category = envelopeItemTypeToDataCategory(type);
if (allowedCategories.includes(category)) {
filteredItems.push(item);
}
}
return true; // Continue iteration
});
return createEnvelope(envelope[0], filteredItems);
}
removeAttachments(envelope: Envelope): Envelope {
const filteredItems: EnvelopeItem[] = [];
forEachEnvelopeItem(envelope, (item, type) => {
if (type !== 'attachment') {
filteredItems.push(item);
}
return true;
});
return createEnvelope(envelope[0], filteredItems);
}
}interface Event {
event_id?: string;
message?: string;
level?: string;
timestamp?: number;
platform?: string;
logger?: string;
server_name?: string;
release?: string;
environment?: string;
fingerprint?: string[];
[key: string]: any;
}
type DataCategory =
| 'default'
| 'error'
| 'transaction'
| 'security'
| 'attachment'
| 'session'
| 'profile'
| 'replay'
| 'internal';Migration Note: All envelope functions have been moved from @sentry/utils to @sentry/core. Update your imports accordingly.
Install with Tessl CLI
npx tessl i tessl/npm-sentry--utils