@nestjs/observe 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/agent/detached-observe-worker.d.ts +2 -1
- package/dist/agent/detached-observe-worker.js +133 -14
- package/dist/agent/observe-agent.shared-buffer.d.ts +1 -1
- package/dist/agent/observe-agent.shared-buffer.js +5 -1
- package/dist/agent/observe-agent.worker.js +9 -3
- package/dist/agent/telemetry-wire-contract.d.ts +127 -0
- package/dist/agent/telemetry-wire-contract.js +414 -0
- package/dist/profiling/profile-uploader.d.ts +13 -0
- package/dist/profiling/profile-uploader.js +41 -1
- package/package.json +1 -1
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape the ingestion API accepts, declared here rather than imported from
|
|
3
|
+
* the collector's DTOs.
|
|
4
|
+
*
|
|
5
|
+
* The encoders and the API agree on nothing but single-letter key names, so
|
|
6
|
+
* something has to fail when they drift. That used to be the API's
|
|
7
|
+
* `InsertTelemetryDataDto`, reached across the repo boundary - which meant this
|
|
8
|
+
* package could not be built, tested or published on its own. The contract is
|
|
9
|
+
* restated here instead: it is the SDK's copy of the agreement, and the thing
|
|
10
|
+
* that has to be updated in lockstep when the API changes.
|
|
11
|
+
*
|
|
12
|
+
* Deliberately *not* generated from the encoders' key maps. A contract derived
|
|
13
|
+
* from the code it checks asserts only that the code equals itself. Every key
|
|
14
|
+
* below is written out by hand so that changing a key map breaks a test and
|
|
15
|
+
* forces the API change to be made consciously.
|
|
16
|
+
*
|
|
17
|
+
* The clearest example is `st`: both snapshot encoders map `startTimestamp` to
|
|
18
|
+
* it, and it is absent here on purpose. The API does not declare that field, it
|
|
19
|
+
* validates with `forbidNonWhitelisted`, and what keeps it off the wire is
|
|
20
|
+
* `OperationTraceRegistry.endTrace` deleting it before the encoder ever sees
|
|
21
|
+
* the snapshot. A payload that still carries `st` is rejected with a 400, so
|
|
22
|
+
* `validateTelemetryPayload(..., { forbidUnknown: true })` rejects it too.
|
|
23
|
+
*
|
|
24
|
+
* Lives under `src/agent` rather than `src/testing` because the contract is no
|
|
25
|
+
* longer only a test assertion: when the collector answers 400, the detached
|
|
26
|
+
* worker applies it to the rejected batch - stripping what the API refuses and
|
|
27
|
+
* dropping individual unsalvageable entries - then re-sends once, so one bad
|
|
28
|
+
* entry cannot cost the batch. Healthy batches are never touched.
|
|
29
|
+
*/
|
|
30
|
+
/** One span. Nests through `ch` to whatever depth the operation produced. */
|
|
31
|
+
const TRACE_NODE = {
|
|
32
|
+
n: { type: "string" },
|
|
33
|
+
o: { type: "string" },
|
|
34
|
+
t: { type: "object" },
|
|
35
|
+
d: { type: "number" },
|
|
36
|
+
// Boolean is the "an error occurred but was handled, details not captured"
|
|
37
|
+
// form - every failed non-root span, and a root span failing on a thrown
|
|
38
|
+
// primitive, reports `e: true` rather than an error object. See
|
|
39
|
+
// `TraceSpan.error`.
|
|
40
|
+
e: { type: "object", alsoAccepts: ["boolean"] },
|
|
41
|
+
c: { type: "string" },
|
|
42
|
+
m: { type: "string" },
|
|
43
|
+
s: { type: "string" },
|
|
44
|
+
so: { type: "number" },
|
|
45
|
+
ch: { type: "object", array: true, recursive: true },
|
|
46
|
+
};
|
|
47
|
+
const REQUEST_ATTRIBUTES = {
|
|
48
|
+
m: { type: "string" },
|
|
49
|
+
sc: { type: "number" },
|
|
50
|
+
ou: { type: "string" },
|
|
51
|
+
};
|
|
52
|
+
/** No `st`: see the note at the top of this file. */
|
|
53
|
+
const REQUEST_SNAPSHOT = {
|
|
54
|
+
ct: { type: "string" },
|
|
55
|
+
ti: { type: "string", required: true },
|
|
56
|
+
d: { type: "number" },
|
|
57
|
+
p: { type: "string" },
|
|
58
|
+
op: { type: "string" },
|
|
59
|
+
u: { type: "string" },
|
|
60
|
+
tg: { type: "object" },
|
|
61
|
+
e: { type: "object" },
|
|
62
|
+
a: { type: "object", shape: REQUEST_ATTRIBUTES },
|
|
63
|
+
t: { type: "object", array: true, shape: TRACE_NODE },
|
|
64
|
+
};
|
|
65
|
+
/** No `st` here either, for the same reason. */
|
|
66
|
+
const JOB_SNAPSHOT = {
|
|
67
|
+
i: { type: "string", required: true },
|
|
68
|
+
ti: { type: "string" },
|
|
69
|
+
n: { type: "string" },
|
|
70
|
+
q: { type: "string" },
|
|
71
|
+
s: { type: "string" },
|
|
72
|
+
c: { type: "string" },
|
|
73
|
+
d: { type: "number" },
|
|
74
|
+
ea: { type: "string" },
|
|
75
|
+
wd: { type: "number" },
|
|
76
|
+
am: { type: "number" },
|
|
77
|
+
ma: { type: "number" },
|
|
78
|
+
tg: { type: "object" },
|
|
79
|
+
e: { type: "object" },
|
|
80
|
+
t: { type: "object", array: true, shape: TRACE_NODE },
|
|
81
|
+
};
|
|
82
|
+
const RUNTIME_METRICS = {
|
|
83
|
+
c: {
|
|
84
|
+
type: "object",
|
|
85
|
+
shape: {
|
|
86
|
+
u: { type: "number" },
|
|
87
|
+
s: { type: "number" },
|
|
88
|
+
p: { type: "number" },
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
m: {
|
|
92
|
+
type: "object",
|
|
93
|
+
shape: {
|
|
94
|
+
r: { type: "number" },
|
|
95
|
+
ht: { type: "number" },
|
|
96
|
+
hu: { type: "number" },
|
|
97
|
+
e: { type: "number" },
|
|
98
|
+
ab: { type: "number" },
|
|
99
|
+
p: { type: "number" },
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
g: {
|
|
103
|
+
type: "object",
|
|
104
|
+
shape: {
|
|
105
|
+
c: { type: "number" },
|
|
106
|
+
td: { type: "number" },
|
|
107
|
+
b: {
|
|
108
|
+
type: "object",
|
|
109
|
+
shape: {
|
|
110
|
+
m: { type: "number" },
|
|
111
|
+
j: { type: "number" },
|
|
112
|
+
i: { type: "number" },
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
e: {
|
|
118
|
+
type: "object",
|
|
119
|
+
shape: {
|
|
120
|
+
l: { type: "number" },
|
|
121
|
+
u: { type: "number" },
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
const CUSTOM_METRIC = {
|
|
126
|
+
n: { type: "string", required: true },
|
|
127
|
+
t: { type: "string" },
|
|
128
|
+
v: { type: "number" },
|
|
129
|
+
tg: { type: "object" },
|
|
130
|
+
d: { type: "string" },
|
|
131
|
+
l: { type: "object" },
|
|
132
|
+
lu: { type: "number" },
|
|
133
|
+
k: { type: "string" },
|
|
134
|
+
iv: { type: "number" },
|
|
135
|
+
q50: { type: "number" },
|
|
136
|
+
q95: { type: "number" },
|
|
137
|
+
q99: { type: "number" },
|
|
138
|
+
ct: { type: "number" },
|
|
139
|
+
sm: { type: "number" },
|
|
140
|
+
mx: { type: "number" },
|
|
141
|
+
};
|
|
142
|
+
const LOG_ENTRY = {
|
|
143
|
+
timestamp: { type: "number", required: true },
|
|
144
|
+
text: { type: "string", required: true },
|
|
145
|
+
traceId: { type: "string" },
|
|
146
|
+
spanId: { type: "string" },
|
|
147
|
+
level: { type: "string" },
|
|
148
|
+
context: { type: "string" },
|
|
149
|
+
attributes: { type: "object" },
|
|
150
|
+
};
|
|
151
|
+
/** The batch body the agent POSTs to the collector. */
|
|
152
|
+
export const TELEMETRY_BATCH = {
|
|
153
|
+
serviceId: { type: "string", required: true },
|
|
154
|
+
serviceVersion: { type: "string" },
|
|
155
|
+
forwardLogs: { type: "boolean" },
|
|
156
|
+
snapshots: { type: "object", array: true, shape: REQUEST_SNAPSHOT },
|
|
157
|
+
jobs: { type: "object", array: true, shape: JOB_SNAPSHOT },
|
|
158
|
+
runtime: { type: "object", shape: RUNTIME_METRICS },
|
|
159
|
+
custom: { type: "object", array: true, shape: CUSTOM_METRIC },
|
|
160
|
+
logs: { type: "object", array: true, shape: LOG_ENTRY },
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* The sections of a batch whose entries are individually expendable. Maps each
|
|
164
|
+
* to the shape one entry must satisfy, so the flush path can judge entries one
|
|
165
|
+
* at a time instead of the collector judging the batch as a whole.
|
|
166
|
+
*/
|
|
167
|
+
export const SECTION_SHAPES = {
|
|
168
|
+
snapshots: REQUEST_SNAPSHOT,
|
|
169
|
+
jobs: JOB_SNAPSHOT,
|
|
170
|
+
custom: CUSTOM_METRIC,
|
|
171
|
+
logs: LOG_ENTRY,
|
|
172
|
+
runtime: RUNTIME_METRICS,
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Checks a batch body against the contract and returns one message per problem,
|
|
176
|
+
* empty when the payload is acceptable. Paths are dotted, with array indices, so
|
|
177
|
+
* a failure names the exact field: `snapshots[0].a.sc`.
|
|
178
|
+
*/
|
|
179
|
+
export function validateTelemetryPayload(payload, options = {}) {
|
|
180
|
+
const errors = [];
|
|
181
|
+
checkShape(payload, TELEMETRY_BATCH, "", options, errors);
|
|
182
|
+
return errors;
|
|
183
|
+
}
|
|
184
|
+
function checkShape(value, shape, path, options, errors) {
|
|
185
|
+
if (!isPlainObject(value)) {
|
|
186
|
+
errors.push(`${path || "payload"}: expected an object`);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
for (const [key, spec] of Object.entries(shape)) {
|
|
190
|
+
const present = value[key] !== undefined && value[key] !== null;
|
|
191
|
+
if (!present) {
|
|
192
|
+
if (spec.required) {
|
|
193
|
+
errors.push(`${join(path, key)}: required`);
|
|
194
|
+
}
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
checkField(value[key], spec, shape, join(path, key), options, errors);
|
|
198
|
+
}
|
|
199
|
+
if (options.forbidUnknown) {
|
|
200
|
+
for (const key of Object.keys(value)) {
|
|
201
|
+
if (!(key in shape)) {
|
|
202
|
+
errors.push(`${join(path, key)}: not declared by the contract`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function checkField(value, spec, parent, path, options, errors) {
|
|
208
|
+
if (spec.array) {
|
|
209
|
+
if (!Array.isArray(value)) {
|
|
210
|
+
errors.push(`${path}: expected an array`);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
value.forEach((item, index) => {
|
|
214
|
+
checkField(item, { ...spec, array: false }, parent, `${path}[${index}]`, options, errors);
|
|
215
|
+
});
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (spec.alsoAccepts?.includes(typeof value)) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
// `recursive` means "the shape this field lives in", which is how a span's
|
|
222
|
+
// children are described without the contract referring to itself before it
|
|
223
|
+
// is defined.
|
|
224
|
+
const nested = spec.recursive ? parent : spec.shape;
|
|
225
|
+
if (nested) {
|
|
226
|
+
checkShape(value, nested, path, options, errors);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (spec.type === "object") {
|
|
230
|
+
if (!isPlainObject(value)) {
|
|
231
|
+
errors.push(`${path}: expected an object`);
|
|
232
|
+
}
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (typeof value !== spec.type) {
|
|
236
|
+
errors.push(`${path}: expected a ${spec.type}, got ${typeof value}`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Builds the sanitizer that brings a rejected batch into contract.
|
|
241
|
+
*
|
|
242
|
+
* Two halves, mirroring the two halves of the API's validation. Keys the
|
|
243
|
+
* contract does not declare, and *optional* fields carrying the wrong type,
|
|
244
|
+
* are removed in place - the data they sat next to is still good, and the API
|
|
245
|
+
* would have rejected the whole batch over them (`forbidNonWhitelisted`).
|
|
246
|
+
* A *required* field that is missing or unusable cannot be repaired by
|
|
247
|
+
* removal, so the entry is dropped alone rather than the collector dropping
|
|
248
|
+
* the batch.
|
|
249
|
+
*
|
|
250
|
+
* Numbers must be finite: `NaN` and `Infinity` survive `typeof` but serialize
|
|
251
|
+
* to `null` in JSON, which arrives at the API as a present-but-null field and
|
|
252
|
+
* fails validation there. They are judged here as what they will be on the
|
|
253
|
+
* wire, not as what they are in memory.
|
|
254
|
+
*
|
|
255
|
+
* A factory rather than module-level functions, and deliberately
|
|
256
|
+
* self-contained - it closes over nothing but its own parameter. The detached
|
|
257
|
+
* worker runs as a stringified function with no module scope, so this
|
|
258
|
+
* function's *source* is inlined into the worker script
|
|
259
|
+
* (`createTelemetrySanitizer.toString()`) and called with the shapes passed
|
|
260
|
+
* through `workerData`. Referencing anything from this module's scope would
|
|
261
|
+
* compile, then throw at runtime inside the worker.
|
|
262
|
+
*
|
|
263
|
+
* This is also why sanitization happens in the worker at all: it runs only
|
|
264
|
+
* when the collector has actually answered 400, on the worker thread, so a
|
|
265
|
+
* healthy application never pays for it - not per flush, and not on the main
|
|
266
|
+
* thread.
|
|
267
|
+
*/
|
|
268
|
+
export function createTelemetrySanitizer(shapes) {
|
|
269
|
+
// Local copy of the module-level helper, for the self-containment above.
|
|
270
|
+
const isPlainRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
271
|
+
/** Returns whether the value is still usable once stripped. */
|
|
272
|
+
const sanitizeShape = (value, shape, path, problems) => {
|
|
273
|
+
if (!isPlainRecord(value)) {
|
|
274
|
+
problems.push(`${path}: expected an object`);
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
let usable = true;
|
|
278
|
+
for (const key of Object.keys(value)) {
|
|
279
|
+
if (!(key in shape)) {
|
|
280
|
+
problems.push(`${path}.${key}: not declared by the contract, removed`);
|
|
281
|
+
delete value[key];
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
for (const [key, spec] of Object.entries(shape)) {
|
|
285
|
+
const fieldPath = `${path}.${key}`;
|
|
286
|
+
const present = value[key] !== undefined && value[key] !== null;
|
|
287
|
+
if (!present) {
|
|
288
|
+
if (spec.required) {
|
|
289
|
+
problems.push(`${fieldPath}: required`);
|
|
290
|
+
usable = false;
|
|
291
|
+
}
|
|
292
|
+
else if (key in value) {
|
|
293
|
+
// Explicit nulls fail the API's type checks even on optional
|
|
294
|
+
// fields; absent keys do not.
|
|
295
|
+
delete value[key];
|
|
296
|
+
}
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
if (!sanitizeField(value[key], spec, shape, fieldPath, problems)) {
|
|
300
|
+
if (spec.required) {
|
|
301
|
+
usable = false;
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
delete value[key];
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return usable;
|
|
309
|
+
};
|
|
310
|
+
/** Returns whether the field value is acceptable (possibly after stripping). */
|
|
311
|
+
const sanitizeField = (value, spec, parent, path, problems) => {
|
|
312
|
+
if (spec.array) {
|
|
313
|
+
if (!Array.isArray(value)) {
|
|
314
|
+
problems.push(`${path}: expected an array, removed`);
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
// Unsalvageable elements are removed one by one - backwards, so the
|
|
318
|
+
// removal of one does not shift the index of the next.
|
|
319
|
+
for (let index = value.length - 1; index >= 0; index--) {
|
|
320
|
+
const elementSpec = { ...spec, array: false };
|
|
321
|
+
if (!sanitizeField(value[index], elementSpec, parent, `${path}[${index}]`, problems)) {
|
|
322
|
+
value.splice(index, 1);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
if (spec.alsoAccepts?.includes(typeof value)) {
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
const nested = spec.recursive ? parent : spec.shape;
|
|
331
|
+
if (nested) {
|
|
332
|
+
return sanitizeShape(value, nested, path, problems);
|
|
333
|
+
}
|
|
334
|
+
if (spec.type === "object") {
|
|
335
|
+
if (!isPlainRecord(value)) {
|
|
336
|
+
problems.push(`${path}: expected an object, removed`);
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
if (typeof value !== spec.type) {
|
|
342
|
+
problems.push(`${path}: expected a ${spec.type}, got ${typeof value}`);
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
if (spec.type === "number" && !Number.isFinite(value)) {
|
|
346
|
+
// Serializes to null, which the API reads as a present, invalid field.
|
|
347
|
+
problems.push(`${path}: non-finite number`);
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
return true;
|
|
351
|
+
};
|
|
352
|
+
const sanitizeEntry = (section, entry) => {
|
|
353
|
+
const problems = [];
|
|
354
|
+
const ok = sanitizeShape(entry, shapes[section], section, problems);
|
|
355
|
+
return { ok, problems };
|
|
356
|
+
};
|
|
357
|
+
/**
|
|
358
|
+
* Repairs a whole batch in place, entry by entry. Every reported problem
|
|
359
|
+
* corresponds to a mutation - a stripped field or a dropped entry - so
|
|
360
|
+
* `changed` doubles as "worth re-sending".
|
|
361
|
+
*/
|
|
362
|
+
const sanitizeBatch = (payload) => {
|
|
363
|
+
const problems = [];
|
|
364
|
+
let dropped = 0;
|
|
365
|
+
const arraySections = [
|
|
366
|
+
"snapshots",
|
|
367
|
+
"jobs",
|
|
368
|
+
"custom",
|
|
369
|
+
"logs",
|
|
370
|
+
];
|
|
371
|
+
for (const section of arraySections) {
|
|
372
|
+
const entries = payload[section];
|
|
373
|
+
if (!Array.isArray(entries)) {
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
payload[section] = entries.filter((entry) => {
|
|
377
|
+
const result = sanitizeEntry(section, entry);
|
|
378
|
+
problems.push(...result.problems);
|
|
379
|
+
if (!result.ok) {
|
|
380
|
+
dropped += 1;
|
|
381
|
+
}
|
|
382
|
+
return result.ok;
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
if (payload.runtime !== undefined && payload.runtime !== null) {
|
|
386
|
+
const result = sanitizeEntry("runtime", payload.runtime);
|
|
387
|
+
problems.push(...result.problems);
|
|
388
|
+
if (!result.ok) {
|
|
389
|
+
dropped += 1;
|
|
390
|
+
delete payload.runtime;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return { changed: problems.length > 0, dropped, problems };
|
|
394
|
+
};
|
|
395
|
+
return { sanitizeEntry, sanitizeBatch };
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* The default sanitizer over this module's own shapes - what tests exercise,
|
|
399
|
+
* and the same instance the worker builds for itself from the factory's
|
|
400
|
+
* source.
|
|
401
|
+
*/
|
|
402
|
+
const defaultSanitizer = createTelemetrySanitizer(SECTION_SHAPES);
|
|
403
|
+
export function sanitizeTelemetryEntry(section, entry) {
|
|
404
|
+
return defaultSanitizer.sanitizeEntry(section, entry);
|
|
405
|
+
}
|
|
406
|
+
export function sanitizeTelemetryBatch(payload) {
|
|
407
|
+
return defaultSanitizer.sanitizeBatch(payload);
|
|
408
|
+
}
|
|
409
|
+
function isPlainObject(value) {
|
|
410
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
411
|
+
}
|
|
412
|
+
function join(path, key) {
|
|
413
|
+
return path ? `${path}.${key}` : key;
|
|
414
|
+
}
|
|
@@ -20,6 +20,12 @@ export declare class ProfileUploader {
|
|
|
20
20
|
private readonly options;
|
|
21
21
|
private readonly pending;
|
|
22
22
|
private inFlight;
|
|
23
|
+
/**
|
|
24
|
+
* When uploads may resume after a 429. Windows enqueued while paused sit in
|
|
25
|
+
* `pending` under its usual two-window cap, so a lapsed pause resumes with
|
|
26
|
+
* the freshest profiles rather than a backlog.
|
|
27
|
+
*/
|
|
28
|
+
private rateLimitedUntil;
|
|
23
29
|
constructor(options: ProfileUploaderOptions);
|
|
24
30
|
enqueue(window: ProfileWindow, sampleRateHz: number): void;
|
|
25
31
|
/**
|
|
@@ -41,5 +47,12 @@ export declare class ProfileUploader {
|
|
|
41
47
|
* that path retries and the entitlement path discards.
|
|
42
48
|
*/
|
|
43
49
|
private isEntitlementError;
|
|
50
|
+
/**
|
|
51
|
+
* How long to pause for, honouring the response's Retry-After when it
|
|
52
|
+
* carries one. Only the delta-seconds form is parsed - the HTTP-date form
|
|
53
|
+
* is rare on rate limits and mis-parsing it risks an enormous pause - and
|
|
54
|
+
* anything absent, malformed, or out of range falls back to the default.
|
|
55
|
+
*/
|
|
56
|
+
private rateLimitPauseMs;
|
|
44
57
|
pendingCount(): number;
|
|
45
58
|
}
|
|
@@ -8,6 +8,15 @@
|
|
|
8
8
|
* that the oldest is dropped.
|
|
9
9
|
*/
|
|
10
10
|
const MAX_PENDING_WINDOWS = 2;
|
|
11
|
+
/**
|
|
12
|
+
* How long to stop uploading after a 429, when the response carries no usable
|
|
13
|
+
* Retry-After. Out-of-credits does not clear between windows, so retrying at
|
|
14
|
+
* the once-a-minute cadence only spends requests on refusals.
|
|
15
|
+
*/
|
|
16
|
+
const RATE_LIMIT_PAUSE_MS = 5 * 60 * 1000;
|
|
17
|
+
/** Ceiling on a server-sent Retry-After, so a bad header cannot park uploads
|
|
18
|
+
* for a day. */
|
|
19
|
+
const RATE_LIMIT_PAUSE_MAX_MS = 60 * 60 * 1000;
|
|
11
20
|
/**
|
|
12
21
|
* Ships one folded-stack window per upload.
|
|
13
22
|
*
|
|
@@ -21,6 +30,12 @@ export class ProfileUploader {
|
|
|
21
30
|
options;
|
|
22
31
|
pending = [];
|
|
23
32
|
inFlight = false;
|
|
33
|
+
/**
|
|
34
|
+
* When uploads may resume after a 429. Windows enqueued while paused sit in
|
|
35
|
+
* `pending` under its usual two-window cap, so a lapsed pause resumes with
|
|
36
|
+
* the freshest profiles rather than a backlog.
|
|
37
|
+
*/
|
|
38
|
+
rateLimitedUntil = 0;
|
|
24
39
|
constructor(options) {
|
|
25
40
|
this.options = options;
|
|
26
41
|
}
|
|
@@ -42,7 +57,7 @@ export class ProfileUploader {
|
|
|
42
57
|
* duplicate rather than an addition.
|
|
43
58
|
*/
|
|
44
59
|
async drain() {
|
|
45
|
-
if (this.inFlight) {
|
|
60
|
+
if (this.inFlight || Date.now() < this.rateLimitedUntil) {
|
|
46
61
|
return;
|
|
47
62
|
}
|
|
48
63
|
this.inFlight = true;
|
|
@@ -94,6 +109,18 @@ export class ProfileUploader {
|
|
|
94
109
|
this.options.onError?.("profiling is not included in this plan; discarding the window");
|
|
95
110
|
return true;
|
|
96
111
|
}
|
|
112
|
+
if (response.status === 429) {
|
|
113
|
+
// Out of credits or over budget. Pause outright instead of retrying
|
|
114
|
+
// every minute: the answer will not change window to window, and each
|
|
115
|
+
// retry is a request spent on a refusal. The window stays queued -
|
|
116
|
+
// `drain` skips while paused - so the first upload after the pause
|
|
117
|
+
// carries the freshest profiles the two-window cap kept.
|
|
118
|
+
const pauseMs = this.rateLimitPauseMs(response);
|
|
119
|
+
this.rateLimitedUntil = Date.now() + pauseMs;
|
|
120
|
+
this.options.onError?.(`profile upload rate-limited (429) - the account may be out of credits or over budget; ` +
|
|
121
|
+
`pausing uploads for ${Math.round(pauseMs / 60000)} minute(s)`);
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
97
124
|
if (response.status === 401 || response.status === 403) {
|
|
98
125
|
// A credential problem, not an entitlement one. Reported specifically
|
|
99
126
|
// and not discarded: it is a misconfiguration the operator can fix,
|
|
@@ -134,6 +161,19 @@ export class ProfileUploader {
|
|
|
134
161
|
return false;
|
|
135
162
|
}
|
|
136
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* How long to pause for, honouring the response's Retry-After when it
|
|
166
|
+
* carries one. Only the delta-seconds form is parsed - the HTTP-date form
|
|
167
|
+
* is rare on rate limits and mis-parsing it risks an enormous pause - and
|
|
168
|
+
* anything absent, malformed, or out of range falls back to the default.
|
|
169
|
+
*/
|
|
170
|
+
rateLimitPauseMs(response) {
|
|
171
|
+
const retryAfter = Number(response.headers.get("retry-after"));
|
|
172
|
+
if (Number.isFinite(retryAfter) && retryAfter > 0) {
|
|
173
|
+
return Math.min(retryAfter * 1000, RATE_LIMIT_PAUSE_MAX_MS);
|
|
174
|
+
}
|
|
175
|
+
return RATE_LIMIT_PAUSE_MS;
|
|
176
|
+
}
|
|
137
177
|
pendingCount() {
|
|
138
178
|
return this.pending.length;
|
|
139
179
|
}
|
package/package.json
CHANGED