@nestjs/observe 0.1.5 → 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 +81 -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/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
|
+
}
|
package/package.json
CHANGED