Skip to content

ref: Refactor transport, request, and client report types #5007

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 2 commits into from
Apr 28, 2022
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
11 changes: 3 additions & 8 deletions packages/core/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
EventItem,
SdkInfo,
SdkMetadata,
SentryRequestType,
Session,
SessionAggregates,
SessionEnvelope,
Expand Down Expand Up @@ -52,14 +51,10 @@ export function createSessionEnvelope(
...(!!tunnel && { dsn: dsnToString(dsn) }),
};

// I know this is hacky but we don't want to add `sessions` to request type since it's never rate limited
const type = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
const envelopeItem: SessionItem =
'aggregates' in session ? [{ type: 'sessions' }, session] : [{ type: 'session' }, session];

// TODO (v7) Have to cast type because envelope items do not accept a `SentryRequestType`
const envelopeItem = [{ type } as { type: 'session' | 'sessions' }, session] as SessionItem;
const envelope = createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);

return envelope;
return createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/transports/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
DataCategory,
Envelope,
InternalBaseTransportOptions,
Transport,
TransportCategory,
TransportRequestExecutor,
} from '@sentry/types';
import {
Expand Down Expand Up @@ -35,7 +35,7 @@ export function createTransport(

function send(envelope: Envelope): PromiseLike<void> {
const envCategory = getEnvelopeType(envelope);
const category = envCategory === 'event' ? 'error' : (envCategory as TransportCategory);
const category = envCategory === 'event' ? 'error' : (envCategory as DataCategory);

// Don't add to buffer if transport is already rate-limited
if (isRateLimited(rateLimits, category)) {
Expand Down
13 changes: 10 additions & 3 deletions packages/types/src/clientreport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { SentryRequestType } from './request';
import { Outcome } from './transport';
import { DataCategory } from './datacategory';

export type EventDropReason =
| 'before_send'
| 'event_processor'
| 'network_error'
| 'queue_overflow'
| 'ratelimit_backoff'
| 'sample_rate';

export type ClientReport = {
timestamp: number;
discarded_events: Array<{ reason: Outcome; category: SentryRequestType; quantity: number }>;
discarded_events: Array<{ reason: EventDropReason; category: DataCategory; quantity: number }>;
};
20 changes: 20 additions & 0 deletions packages/types/src/datacategory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This type is used in various places like Client Reports and Rate Limit Categories
// See:
// - https://develop.sentry.dev/sdk/rate-limiting/#definitions
// - https://github.com/getsentry/relay/blob/10874b587bb676bd6d50ad42d507216513660082/relay-common/src/constants.rs#L97-L113
// - https://develop.sentry.dev/sdk/client-reports/#envelope-item-payload under `discarded_events`
export type DataCategory =
// Reserved and only used in edgecases, unlikely to be ever actually used
| 'default'
// Error events
| 'error'
// Transaction type event
| 'transaction'
// Events with `event_type` csp, hpkp, expectct, expectstaple
| 'security'
// Attachment bytes stored (unused for rate limiting
| 'attachment'
// Session update events
| 'session'
// SDK internal event, like client_reports
| 'internal';
7 changes: 3 additions & 4 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export type { Breadcrumb, BreadcrumbHint } from './breadcrumb';
export type { Client } from './client';
export type { ClientReport } from './clientreport';
export type { ClientReport, EventDropReason } from './clientreport';
export type { Context, Contexts } from './context';
export type { DataCategory } from './datacategory';
export type { DsnComponents, DsnLike, DsnProtocol } from './dsn';
export type { DebugImage, DebugImageType, DebugMeta } from './debugMeta';
export type {
Expand All @@ -28,7 +29,7 @@ export type { Mechanism } from './mechanism';
export type { ExtractedNodeRequestData, Primitive, WorkerLocation } from './misc';
export type { ClientOptions, Options } from './options';
export type { Package } from './package';
export type { QueryParams, Request, SentryRequest, SentryRequestType } from './request';
export type { QueryParams, Request } from './request';
export type { Runtime } from './runtime';
export type { CaptureContext, Scope, ScopeContext } from './scope';
export type { SdkInfo } from './sdkinfo';
Expand Down Expand Up @@ -61,9 +62,7 @@ export type {
} from './transaction';
export type { Thread } from './thread';
export type {
Outcome,
Transport,
TransportCategory,
TransportRequest,
TransportMakeRequestResponse,
InternalBaseTransportOptions,
Expand Down
11 changes: 0 additions & 11 deletions packages/types/src/request.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
/** Possible SentryRequest types that can be used to make a distinction between Sentry features */
// NOTE(kamil): It would be nice if we make it a valid enum instead
export type SentryRequestType = 'event' | 'transaction' | 'session' | 'attachment';

/** A generic client request. */
export interface SentryRequest {
body: string;
type: SentryRequestType;
url: string;
}

/** Request data included in an event as sent to Sentry */
export interface Request {
url?: string;
Expand Down
10 changes: 0 additions & 10 deletions packages/types/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { Envelope } from './envelope';

export type Outcome =
| 'before_send'
| 'event_processor'
| 'network_error'
| 'queue_overflow'
| 'ratelimit_backoff'
| 'sample_rate';

export type TransportCategory = 'error' | 'transaction' | 'attachment' | 'session';

export type TransportRequest = {
body: string;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/test/clientreport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { serializeEnvelope } from '../src/envelope';
const DEFAULT_DISCARDED_EVENTS: ClientReport['discarded_events'] = [
{
reason: 'before_send',
category: 'event',
category: 'error',
quantity: 30,
},
{
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('createClientReportEnvelope', () => {
expect(serializedEnv).toMatchInlineSnapshot(`
"{\\"dsn\\":\\"https://[email protected]/1\\"}
{\\"type\\":\\"client_report\\"}
{\\"timestamp\\":123456,\\"discarded_events\\":[{\\"reason\\":\\"before_send\\",\\"category\\":\\"event\\",\\"quantity\\":30},{\\"reason\\":\\"network_error\\",\\"category\\":\\"transaction\\",\\"quantity\\":23}]}"
{\\"timestamp\\":123456,\\"discarded_events\\":[{\\"reason\\":\\"before_send\\",\\"category\\":\\"error\\",\\"quantity\\":30},{\\"reason\\":\\"network_error\\",\\"category\\":\\"transaction\\",\\"quantity\\":23}]}"
`);
});
});