-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
base.ts
223 lines (197 loc) · 6.62 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { API } from '@sentry/core';
import {
Event,
Outcome,
Response as SentryResponse,
SentryRequestType,
Status,
Transport,
TransportOptions,
} from '@sentry/types';
import {
dateTimestampInSeconds,
getGlobalObject,
logger,
parseRetryAfterHeader,
PromiseBuffer,
SentryError,
} from '@sentry/utils';
import { sendReport } from './utils';
const CATEGORY_MAPPING: {
[key in SentryRequestType]: string;
} = {
event: 'error',
transaction: 'transaction',
session: 'session',
attachment: 'attachment',
};
const global = getGlobalObject<Window>();
/** Base Transport class implementation */
export abstract class BaseTransport implements Transport {
/**
* @deprecated
*/
public url: string;
/** Helper to get Sentry API endpoints. */
protected readonly _api: API;
/** A simple buffer holding all requests. */
protected readonly _buffer: PromiseBuffer<SentryResponse> = new PromiseBuffer(30);
/** Locks transport after receiving rate limits in a response */
protected readonly _rateLimits: Record<string, Date> = {};
protected _outcomes: { [key: string]: number } = {};
public constructor(public options: TransportOptions) {
this._api = new API(options.dsn, options._metadata, options.tunnel);
// eslint-disable-next-line deprecation/deprecation
this.url = this._api.getStoreEndpointWithUrlEncodedAuth();
if (this.options.sendClientReports && global.document) {
global.document.addEventListener('visibilitychange', () => {
if (global.document.visibilityState === 'hidden') {
this._flushOutcomes();
}
});
}
}
/**
* @inheritDoc
*/
public sendEvent(_: Event): PromiseLike<SentryResponse> {
throw new SentryError('Transport Class has to implement `sendEvent` method');
}
/**
* @inheritDoc
*/
public close(timeout?: number): PromiseLike<boolean> {
return this._buffer.drain(timeout);
}
/**
* @inheritDoc
*/
public recordLostEvent(reason: Outcome, category: SentryRequestType): void {
if (!this.options.sendClientReports) {
return;
}
// We want to track each category (event, transaction, session) separately
// but still keep the distinction between different type of outcomes.
// We could use nested maps, but it's much easier to read and type this way.
// A correct type for map-based implementation if we want to go that route
// would be `Partial<Record<SentryRequestType, Partial<Record<Outcome, number>>>>`
const key = `${CATEGORY_MAPPING[category]}:${reason}`;
logger.log(`Adding outcome: ${key}`);
this._outcomes[key] = (this._outcomes[key] ?? 0) + 1;
}
/**
* Send outcomes as an envelope
*/
protected _flushOutcomes(): void {
if (!this.options.sendClientReports) {
return;
}
const outcomes = this._outcomes;
this._outcomes = {};
// Nothing to send
if (!Object.keys(outcomes).length) {
logger.log('No outcomes to flush');
return;
}
logger.log(`Flushing outcomes:\n${JSON.stringify(outcomes, null, 2)}`);
const url = this._api.getEnvelopeEndpointWithUrlEncodedAuth();
// Envelope header is required to be at least an empty object
const envelopeHeader = JSON.stringify({ ...(this.options.tunnel && { dsn: this._api.getDsn().toString() }) });
const itemHeaders = JSON.stringify({
type: 'client_report',
});
const item = JSON.stringify({
timestamp: dateTimestampInSeconds(),
discarded_events: Object.keys(outcomes).map(key => {
const [category, reason] = key.split(':');
return {
reason,
category,
quantity: outcomes[key],
};
}),
});
const envelope = `${envelopeHeader}\n${itemHeaders}\n${item}`;
try {
sendReport(url, envelope);
} catch (e) {
logger.error(e);
}
}
/**
* Handle Sentry repsonse for promise-based transports.
*/
protected _handleResponse({
requestType,
response,
headers,
resolve,
reject,
}: {
requestType: SentryRequestType;
response: Response | XMLHttpRequest;
headers: Record<string, string | null>;
resolve: (value?: SentryResponse | PromiseLike<SentryResponse> | null | undefined) => void;
reject: (reason?: unknown) => void;
}): void {
const status = Status.fromHttpCode(response.status);
/**
* "The name is case-insensitive."
* https://developer.mozilla.org/en-US/docs/Web/API/Headers/get
*/
const limited = this._handleRateLimit(headers);
if (limited)
logger.warn(`Too many ${requestType} requests, backing off until: ${this._disabledUntil(requestType)}`);
if (status === Status.Success) {
resolve({ status });
return;
}
reject(response);
}
/**
* Gets the time that given category is disabled until for rate limiting
*/
protected _disabledUntil(requestType: SentryRequestType): Date {
const category = CATEGORY_MAPPING[requestType];
return this._rateLimits[category] || this._rateLimits.all;
}
/**
* Checks if a category is rate limited
*/
protected _isRateLimited(requestType: SentryRequestType): boolean {
return this._disabledUntil(requestType) > new Date(Date.now());
}
/**
* Sets internal _rateLimits from incoming headers. Returns true if headers contains a non-empty rate limiting header.
*/
protected _handleRateLimit(headers: Record<string, string | null>): boolean {
const now = Date.now();
const rlHeader = headers['x-sentry-rate-limits'];
const raHeader = headers['retry-after'];
if (rlHeader) {
// rate limit headers are of the form
// <header>,<header>,..
// where each <header> is of the form
// <retry_after>: <categories>: <scope>: <reason_code>
// where
// <retry_after> is a delay in ms
// <categories> is the event type(s) (error, transaction, etc) being rate limited and is of the form
// <category>;<category>;...
// <scope> is what's being limited (org, project, or key) - ignored by SDK
// <reason_code> is an arbitrary string like "org_quota" - ignored by SDK
for (const limit of rlHeader.trim().split(',')) {
const parameters = limit.split(':', 2);
const headerDelay = parseInt(parameters[0], 10);
const delay = (!isNaN(headerDelay) ? headerDelay : 60) * 1000; // 60sec default
for (const category of parameters[1].split(';')) {
this._rateLimits[category || 'all'] = new Date(now + delay);
}
}
return true;
} else if (raHeader) {
this._rateLimits.all = new Date(now + parseRetryAfterHeader(now, raHeader));
return true;
}
return false;
}
}