Skip to content

Commit b411e38

Browse files
lforstAbhiPrasad
authored andcommitted
ref: Refactor transport, request, and client report types (#5007)
1 parent 716629e commit b411e38

File tree

8 files changed

+40
-40
lines changed

8 files changed

+40
-40
lines changed

packages/core/src/envelope.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
EventItem,
66
SdkInfo,
77
SdkMetadata,
8-
SentryRequestType,
98
Session,
109
SessionAggregates,
1110
SessionEnvelope,
@@ -52,14 +51,10 @@ export function createSessionEnvelope(
5251
...(!!tunnel && { dsn: dsnToString(dsn) }),
5352
};
5453

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

58-
// TODO (v7) Have to cast type because envelope items do not accept a `SentryRequestType`
59-
const envelopeItem = [{ type } as { type: 'session' | 'sessions' }, session] as SessionItem;
60-
const envelope = createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);
61-
62-
return envelope;
57+
return createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);
6358
}
6459

6560
/**

packages/core/src/transports/base.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
2+
DataCategory,
23
Envelope,
34
InternalBaseTransportOptions,
45
Transport,
5-
TransportCategory,
66
TransportRequestExecutor,
77
} from '@sentry/types';
88
import {
@@ -35,7 +35,7 @@ export function createTransport(
3535

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

4040
// Don't add to buffer if transport is already rate-limited
4141
if (isRateLimited(rateLimits, category)) {

packages/types/src/clientreport.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import { SentryRequestType } from './request';
2-
import { Outcome } from './transport';
1+
import { DataCategory } from './datacategory';
2+
3+
export type EventDropReason =
4+
| 'before_send'
5+
| 'event_processor'
6+
| 'network_error'
7+
| 'queue_overflow'
8+
| 'ratelimit_backoff'
9+
| 'sample_rate';
310

411
export type ClientReport = {
512
timestamp: number;
6-
discarded_events: Array<{ reason: Outcome; category: SentryRequestType; quantity: number }>;
13+
discarded_events: Array<{ reason: EventDropReason; category: DataCategory; quantity: number }>;
714
};

packages/types/src/datacategory.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This type is used in various places like Client Reports and Rate Limit Categories
2+
// See:
3+
// - https://develop.sentry.dev/sdk/rate-limiting/#definitions
4+
// - https://github.com/getsentry/relay/blob/10874b587bb676bd6d50ad42d507216513660082/relay-common/src/constants.rs#L97-L113
5+
// - https://develop.sentry.dev/sdk/client-reports/#envelope-item-payload under `discarded_events`
6+
export type DataCategory =
7+
// Reserved and only used in edgecases, unlikely to be ever actually used
8+
| 'default'
9+
// Error events
10+
| 'error'
11+
// Transaction type event
12+
| 'transaction'
13+
// Events with `event_type` csp, hpkp, expectct, expectstaple
14+
| 'security'
15+
// Attachment bytes stored (unused for rate limiting
16+
| 'attachment'
17+
// Session update events
18+
| 'session'
19+
// SDK internal event, like client_reports
20+
| 'internal';

packages/types/src/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export type { Breadcrumb, BreadcrumbHint } from './breadcrumb';
22
export type { Client } from './client';
3-
export type { ClientReport } from './clientreport';
3+
export type { ClientReport, EventDropReason } from './clientreport';
44
export type { Context, Contexts } from './context';
5+
export type { DataCategory } from './datacategory';
56
export type { DsnComponents, DsnLike, DsnProtocol } from './dsn';
67
export type { DebugImage, DebugImageType, DebugMeta } from './debugMeta';
78
export type {
@@ -28,7 +29,7 @@ export type { Mechanism } from './mechanism';
2829
export type { ExtractedNodeRequestData, Primitive, WorkerLocation } from './misc';
2930
export type { ClientOptions, Options } from './options';
3031
export type { Package } from './package';
31-
export type { QueryParams, Request, SentryRequest, SentryRequestType } from './request';
32+
export type { QueryParams, Request } from './request';
3233
export type { Runtime } from './runtime';
3334
export type { CaptureContext, Scope, ScopeContext } from './scope';
3435
export type { SdkInfo } from './sdkinfo';
@@ -61,9 +62,7 @@ export type {
6162
} from './transaction';
6263
export type { Thread } from './thread';
6364
export type {
64-
Outcome,
6565
Transport,
66-
TransportCategory,
6766
TransportRequest,
6867
TransportMakeRequestResponse,
6968
InternalBaseTransportOptions,

packages/types/src/request.ts

-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
/** Possible SentryRequest types that can be used to make a distinction between Sentry features */
2-
// NOTE(kamil): It would be nice if we make it a valid enum instead
3-
export type SentryRequestType = 'event' | 'transaction' | 'session' | 'attachment';
4-
5-
/** A generic client request. */
6-
export interface SentryRequest {
7-
body: string;
8-
type: SentryRequestType;
9-
url: string;
10-
}
11-
121
/** Request data included in an event as sent to Sentry */
132
export interface Request {
143
url?: string;

packages/types/src/transport.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
import { Envelope } from './envelope';
22

3-
export type Outcome =
4-
| 'before_send'
5-
| 'event_processor'
6-
| 'network_error'
7-
| 'queue_overflow'
8-
| 'ratelimit_backoff'
9-
| 'sample_rate';
10-
11-
export type TransportCategory = 'error' | 'transaction' | 'attachment' | 'session';
12-
133
export type TransportRequest = {
144
body: string;
155
};

packages/utils/test/clientreport.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { serializeEnvelope } from '../src/envelope';
66
const DEFAULT_DISCARDED_EVENTS: ClientReport['discarded_events'] = [
77
{
88
reason: 'before_send',
9-
category: 'event',
9+
category: 'error',
1010
quantity: 30,
1111
},
1212
{
@@ -45,7 +45,7 @@ describe('createClientReportEnvelope', () => {
4545
expect(serializedEnv).toMatchInlineSnapshot(`
4646
"{\\"dsn\\":\\"https://[email protected]/1\\"}
4747
{\\"type\\":\\"client_report\\"}
48-
{\\"timestamp\\":123456,\\"discarded_events\\":[{\\"reason\\":\\"before_send\\",\\"category\\":\\"event\\",\\"quantity\\":30},{\\"reason\\":\\"network_error\\",\\"category\\":\\"transaction\\",\\"quantity\\":23}]}"
48+
{\\"timestamp\\":123456,\\"discarded_events\\":[{\\"reason\\":\\"before_send\\",\\"category\\":\\"error\\",\\"quantity\\":30},{\\"reason\\":\\"network_error\\",\\"category\\":\\"transaction\\",\\"quantity\\":23}]}"
4949
`);
5050
});
5151
});

0 commit comments

Comments
 (0)