Skip to content

feat(performance): Adds span envelope and datacategory #10706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export {
export { applyScopeDataToEvent, mergeScopeData } from './utils/applyScopeDataToEvent';
export { prepareEvent } from './utils/prepareEvent';
export { createCheckInEnvelope } from './checkin';
export { createSpanEnvelope } from './span';
export { hasTracingEnabled } from './utils/hasTracingEnabled';
export { isSentryRequestUrl } from './utils/isSentryRequestUrl';
export { handleCallbackErrors } from './utils/handleCallbackErrors';
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/span.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { SpanEnvelope, SpanItem } from '@sentry/types';
import type { Span } from '@sentry/types';
import { createEnvelope } from '@sentry/utils';

/**
* Create envelope from Span item.
*/
export function createSpanEnvelope(spans: Span[]): SpanEnvelope {
const headers: SpanEnvelope[0] = {
sent_at: new Date().toISOString(),
};

const items = spans.map(createSpanItem);
return createEnvelope<SpanEnvelope>(headers, items);
}

function createSpanItem(span: Span): SpanItem {
const spanHeaders: SpanItem[0] = {
type: 'span',
};
return [spanHeaders, span];
}
4 changes: 3 additions & 1 deletion packages/types/src/datacategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ export type DataCategory =
// Feedback type event (v2)
| 'feedback'
// Unknown data category
| 'unknown';
| 'unknown'
// Span
| 'span';
11 changes: 9 additions & 2 deletions packages/types/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Profile } from './profiling';
import type { ReplayEvent, ReplayRecordingData } from './replay';
import type { SdkInfo } from './sdkinfo';
import type { SerializedSession, Session, SessionAggregates } from './session';
import type { Span } from './span';
import type { Transaction } from './transaction';
import type { UserFeedback } from './user';

Expand Down Expand Up @@ -41,7 +42,8 @@ export type EnvelopeItemType =
| 'replay_event'
| 'replay_recording'
| 'check_in'
| 'statsd';
| 'statsd'
| 'span';

export type BaseEnvelopeHeaders = {
[key: string]: unknown;
Expand Down Expand Up @@ -82,6 +84,7 @@ type ReplayRecordingItemHeaders = { type: 'replay_recording'; length: number };
type CheckInItemHeaders = { type: 'check_in' };
type StatsdItemHeaders = { type: 'statsd'; length: number };
type ProfileItemHeaders = { type: 'profile' };
type SpanItemHeaders = { type: 'span' };

// TODO (v8): Replace `Event` with `SerializedEvent`
export type EventItem = BaseEnvelopeItem<EventItemHeaders, Event>;
Expand All @@ -98,13 +101,15 @@ type ReplayRecordingItem = BaseEnvelopeItem<ReplayRecordingItemHeaders, ReplayRe
export type StatsdItem = BaseEnvelopeItem<StatsdItemHeaders, string>;
export type FeedbackItem = BaseEnvelopeItem<FeedbackItemHeaders, FeedbackEvent>;
export type ProfileItem = BaseEnvelopeItem<ProfileItemHeaders, Profile>;
export type SpanItem = BaseEnvelopeItem<SpanItemHeaders, Span>;

export type EventEnvelopeHeaders = { event_id: string; sent_at: string; trace?: DynamicSamplingContext };
type SessionEnvelopeHeaders = { sent_at: string };
type CheckInEnvelopeHeaders = { trace?: DynamicSamplingContext };
type ClientReportEnvelopeHeaders = BaseEnvelopeHeaders;
type ReplayEnvelopeHeaders = BaseEnvelopeHeaders;
type StatsdEnvelopeHeaders = BaseEnvelopeHeaders;
type SpanEnvelopeHeaders = BaseEnvelopeHeaders;

export type EventEnvelope = BaseEnvelope<
EventEnvelopeHeaders,
Expand All @@ -115,12 +120,14 @@ export type ClientReportEnvelope = BaseEnvelope<ClientReportEnvelopeHeaders, Cli
export type ReplayEnvelope = [ReplayEnvelopeHeaders, [ReplayEventItem, ReplayRecordingItem]];
export type CheckInEnvelope = BaseEnvelope<CheckInEnvelopeHeaders, CheckInItem>;
export type StatsdEnvelope = BaseEnvelope<StatsdEnvelopeHeaders, StatsdItem>;
export type SpanEnvelope = BaseEnvelope<SpanEnvelopeHeaders, SpanItem>;

export type Envelope =
| EventEnvelope
| SessionEnvelope
| ClientReportEnvelope
| ReplayEnvelope
| CheckInEnvelope
| StatsdEnvelope;
| StatsdEnvelope
| SpanEnvelope;
export type EnvelopeItem = Envelope[1][number];
2 changes: 2 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export type {
StatsdItem,
StatsdEnvelope,
ProfileItem,
SpanEnvelope,
SpanItem,
} from './envelope';
export type { ExtendedError } from './error';
export type { Event, EventHint, EventType, ErrorEvent, TransactionEvent, SerializedEvent } from './event';
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ const ITEM_TYPE_TO_DATA_CATEGORY_MAP: Record<EnvelopeItemType, DataCategory> = {
replay_recording: 'replay',
check_in: 'monitor',
feedback: 'feedback',
span: 'span',
// TODO: This is a temporary workaround until we have a proper data category for metrics
statsd: 'unknown',
};
Expand Down