@nestjs/observe 0.3.25 → 0.3.27
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/README.md +55 -2
- package/dist/agent/detached-observe-worker.js +50 -6
- package/dist/agent/observe-agent.shared-buffer.d.ts +44 -0
- package/dist/agent/observe-agent.shared-buffer.js +201 -19
- package/dist/agent/observe-agent.worker.d.ts +22 -2
- package/dist/agent/observe-agent.worker.js +43 -7
- package/dist/agent/telemetry-wire-contract.js +10 -4
- package/dist/cli/agent-notes.js +16 -4
- package/dist/cli/link.js +51 -4
- package/dist/cli/observe-cli.js +1 -19
- package/dist/cli/open-url.d.ts +27 -0
- package/dist/cli/open-url.js +63 -0
- package/dist/cli/package-manager.d.ts +6 -0
- package/dist/cli/package-manager.js +60 -3
- package/dist/cli/project-files.d.ts +62 -1
- package/dist/cli/project-files.js +229 -7
- package/dist/cli/project-setup.d.ts +6 -0
- package/dist/cli/project-setup.js +14 -1
- package/dist/cli/wire-application.js +6 -0
- package/dist/custom-metrics/counter.d.ts +13 -2
- package/dist/custom-metrics/counter.js +33 -16
- package/dist/custom-metrics/finite-value.util.d.ts +14 -0
- package/dist/custom-metrics/finite-value.util.js +33 -1
- package/dist/custom-metrics/gauge.d.ts +2 -0
- package/dist/custom-metrics/gauge.js +41 -17
- package/dist/custom-metrics/label-values.util.d.ts +37 -0
- package/dist/custom-metrics/label-values.util.js +51 -0
- package/dist/custom-metrics/summary.d.ts +23 -0
- package/dist/custom-metrics/summary.js +90 -12
- package/dist/instrument/create-instance-decorator.instrument.js +504 -116
- package/dist/instrument/raw-receiver.d.ts +6 -0
- package/dist/instrument/raw-receiver.js +32 -0
- package/dist/interfaces/observe-options.interface.d.ts +17 -6
- package/dist/objectives/objectives.registry.js +12 -0
- package/dist/observe.module.js +119 -3
- package/dist/otlp/otlp-encoders.js +22 -5
- package/dist/otlp/otlp-span.mapper.d.ts +2 -0
- package/dist/otlp/otlp-span.mapper.js +26 -4
- package/dist/otlp/otlp-trace-exporter.service.d.ts +63 -9
- package/dist/otlp/otlp-trace-exporter.service.js +295 -43
- package/dist/outgoing/http.integration.d.ts +8 -4
- package/dist/outgoing/http.integration.js +49 -17
- package/dist/outgoing/llm/llm-fetch.integration.js +62 -1
- package/dist/outgoing/llm/llm-providers.d.ts +16 -4
- package/dist/outgoing/llm/llm-providers.js +72 -22
- package/dist/outgoing/llm/llm-usage.d.ts +11 -6
- package/dist/outgoing/llm/llm-usage.js +72 -25
- package/dist/outgoing/mongodb.integration.d.ts +3 -1
- package/dist/outgoing/mongodb.integration.js +93 -18
- package/dist/outgoing/mssql.integration.js +25 -6
- package/dist/outgoing/mysql2.integration.js +17 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +20 -4
- package/dist/outgoing/outgoing-span.recorder.js +17 -3
- package/dist/outgoing/pg.integration.d.ts +3 -2
- package/dist/outgoing/pg.integration.js +39 -11
- package/dist/outgoing/sql-statement.util.js +139 -21
- package/dist/outgoing/undici.integration.d.ts +13 -0
- package/dist/outgoing/undici.integration.js +68 -0
- package/dist/protocols/bull-observe-agent.service.js +23 -12
- package/dist/protocols/deferred-trace-completion.d.ts +17 -3
- package/dist/protocols/deferred-trace-completion.js +76 -9
- package/dist/protocols/graphql-observe-agent.service.d.ts +97 -6
- package/dist/protocols/graphql-observe-agent.service.js +354 -19
- package/dist/protocols/graphql-operation-parser.js +111 -26
- package/dist/protocols/http-observe-agent.service.d.ts +50 -9
- package/dist/protocols/http-observe-agent.service.js +220 -59
- package/dist/protocols/job-trace-runner.d.ts +42 -3
- package/dist/protocols/job-trace-runner.js +197 -36
- package/dist/protocols/queue-observe-agent.service.js +44 -20
- package/dist/protocols/rpc-observe-agent.service.d.ts +111 -8
- package/dist/protocols/rpc-observe-agent.service.js +332 -56
- package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
- package/dist/protocols/ws-observe-agent.service.js +227 -21
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/logger-patcher.service.js +16 -7
- package/dist/services/operation-trace.registry.d.ts +58 -1
- package/dist/services/operation-trace.registry.js +576 -63
- package/dist/services/stdout-forwarder.service.d.ts +69 -0
- package/dist/services/stdout-forwarder.service.js +308 -51
- package/dist/services/tracer.service.d.ts +39 -11
- package/dist/services/tracer.service.js +145 -36
- package/dist/utils/capture-request.util.d.ts +3 -1
- package/dist/utils/capture-request.util.js +128 -12
- package/dist/utils/context-accessor.util.d.ts +16 -0
- package/dist/utils/context-accessor.util.js +28 -0
- package/dist/utils/default-trace-id-generator.util.js +5 -7
- package/dist/utils/error-stack.util.d.ts +13 -0
- package/dist/utils/error-stack.util.js +166 -0
- package/dist/utils/fit-to-length.util.js +33 -10
- package/dist/utils/log-line.parser.d.ts +22 -0
- package/dist/utils/log-line.parser.js +20 -7
- package/dist/utils/log-redactor.d.ts +14 -0
- package/dist/utils/log-redactor.js +690 -65
- package/dist/utils/operation-state.util.d.ts +39 -0
- package/dist/utils/operation-state.util.js +50 -0
- package/dist/utils/redact-url-query.d.ts +37 -4
- package/dist/utils/redact-url-query.js +197 -32
- package/dist/utils/sensitive-keys.d.ts +4 -1
- package/dist/utils/sensitive-keys.js +48 -10
- package/dist/utils/shared-hook-slot.util.d.ts +48 -0
- package/dist/utils/shared-hook-slot.util.js +103 -0
- package/dist/utils/source-context.util.d.ts +14 -0
- package/dist/utils/source-context.util.js +142 -43
- package/dist/utils/source-map-resolver.util.js +33 -6
- package/dist/utils/store-copy.util.d.ts +10 -0
- package/dist/utils/store-copy.util.js +43 -0
- package/dist/utils/trace-context.util.js +21 -6
- package/package.json +2 -2
|
@@ -35,12 +35,32 @@ const gzipAsync = promisify(gzip);
|
|
|
35
35
|
* must cost telemetry, not memory.
|
|
36
36
|
*/
|
|
37
37
|
const MAX_QUEUED_SPANS = 10_000;
|
|
38
|
+
/**
|
|
39
|
+
* The most characters waiting to be exported, counted over every string the
|
|
40
|
+
* waiting spans carry (see `charsOf`). Past it, as past `MAX_QUEUED_SPANS`,
|
|
41
|
+
* a finished operation's spans are dropped whole and counted.
|
|
42
|
+
*
|
|
43
|
+
* A count of spans alone does not bound the memory: a span is as large as
|
|
44
|
+
* its tags, its status and its exception event, and some of them are as
|
|
45
|
+
* long as a client made them. Ten thousand spans that each carried a few
|
|
46
|
+
* outgoing calls' URLs with a client's text in them ran the process out of
|
|
47
|
+
* heap while the receiver was down.
|
|
48
|
+
*/
|
|
49
|
+
const MAX_QUEUED_CHARS = 32 * 1024 * 1024;
|
|
38
50
|
/**
|
|
39
51
|
* The most spans one export carries. A full batch is sent at once rather
|
|
40
52
|
* than at the next interval, so a busy service exports in step with what it
|
|
41
53
|
* records instead of building up a backlog between ticks.
|
|
42
54
|
*/
|
|
43
55
|
const MAX_BATCH_SPANS = 1_000;
|
|
56
|
+
/**
|
|
57
|
+
* The most characters one export carries, counted as for the queue: a batch
|
|
58
|
+
* is full at whichever of the two it reaches first, and holds at least one
|
|
59
|
+
* span. Receivers limit the size of what they take - a collector's request
|
|
60
|
+
* body, the message size of whatever it passes spans on to - and a thousand
|
|
61
|
+
* large spans in one export would be refused whole, every time it is tried.
|
|
62
|
+
*/
|
|
63
|
+
const MAX_BATCH_CHARS = 2 * 1024 * 1024;
|
|
44
64
|
const DEFAULT_FLUSH_INTERVAL_MS = 5_000;
|
|
45
65
|
const MIN_FLUSH_INTERVAL_MS = 1_000;
|
|
46
66
|
const DEFAULT_SHUTDOWN_FLUSH_TIMEOUT_MS = 2_000;
|
|
@@ -50,6 +70,13 @@ const DEFAULT_SHUTDOWN_FLUSH_TIMEOUT_MS = 2_000;
|
|
|
50
70
|
* the credentials, which sending the batch again would not change.
|
|
51
71
|
*/
|
|
52
72
|
const RETRYABLE_STATUSES = new Set([429, 502, 503, 504]);
|
|
73
|
+
/**
|
|
74
|
+
* How often a refused batch may be refused again once a later export was
|
|
75
|
+
* accepted, before it is dropped: what tells a batch the receiver will
|
|
76
|
+
* never take from one a rate limiter turned away just after letting the
|
|
77
|
+
* export before it through.
|
|
78
|
+
*/
|
|
79
|
+
const MAX_REFUSALS_ONCE_OUTLIVED = 3;
|
|
53
80
|
/** The first wait before an export is retried; it doubles to the ceiling. */
|
|
54
81
|
const RETRY_BASE_MS = 1_000;
|
|
55
82
|
const RETRY_MAX_MS = 60_000;
|
|
@@ -57,6 +84,22 @@ const RETRY_MAX_MS = 60_000;
|
|
|
57
84
|
const RETRY_AFTER_MAX_MS = 5 * 60_000;
|
|
58
85
|
/** How long an idle connection to the receiver is kept for the next batch. */
|
|
59
86
|
const KEEP_ALIVE_IDLE_MS = 30_000;
|
|
87
|
+
/**
|
|
88
|
+
* The most of a receiver's reply that is read. Only its first 300 characters
|
|
89
|
+
* are ever shown, and it is held and decoded on the application's thread:
|
|
90
|
+
* past this, the reading stops and the connection goes with it, and the
|
|
91
|
+
* status is acted on as it stands.
|
|
92
|
+
*/
|
|
93
|
+
const MAX_REPLY_BYTES = 64 * 1024;
|
|
94
|
+
/**
|
|
95
|
+
* The most interim (1xx) replies one export sits through. Nothing it sends
|
|
96
|
+
* asks for one, and each is parsed on the application's thread: how many of
|
|
97
|
+
* them that thread works through is not the receiver's to choose. A proxy's
|
|
98
|
+
* few ahead of the answer still pass.
|
|
99
|
+
*/
|
|
100
|
+
const MAX_INTERIM_REPLIES = 8;
|
|
101
|
+
/** The longest a timer runs; a longer delay would fire at once instead. */
|
|
102
|
+
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
60
103
|
/**
|
|
61
104
|
* Exports what the agent records as OpenTelemetry traces, over OTLP/HTTP, to
|
|
62
105
|
* whatever receiver `otlp` names - a collector, Jaeger, Tempo, Honeycomb, a
|
|
@@ -89,6 +132,15 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
89
132
|
scope = { name: "@nestjs/observe", version: "" };
|
|
90
133
|
agent;
|
|
91
134
|
queue = [];
|
|
135
|
+
/** What `charsOf` counts in `queue`, kept as spans come and go. */
|
|
136
|
+
queuedChars = 0;
|
|
137
|
+
/**
|
|
138
|
+
* The batches the receiver turned away and that wait for another try,
|
|
139
|
+
* oldest first. Their spans are the first `refusedSpans` of `queue`, in
|
|
140
|
+
* the same order; everything behind them has not been sent yet.
|
|
141
|
+
*/
|
|
142
|
+
refused = [];
|
|
143
|
+
refusedSpans = 0;
|
|
92
144
|
sending = null;
|
|
93
145
|
flushTimer = null;
|
|
94
146
|
shutdownPromise = null;
|
|
@@ -101,8 +153,6 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
101
153
|
/** No export starts before this (epoch ms): a retry is waiting. */
|
|
102
154
|
retryNotBefore = 0;
|
|
103
155
|
retryDelayMs = 0;
|
|
104
|
-
/** Whether the last export failed - the final flush does not retry it. */
|
|
105
|
-
lastSendFailed = false;
|
|
106
156
|
/**
|
|
107
157
|
* What the last failure was, while exports are failing: said once when it
|
|
108
158
|
* starts or changes, and again when exports recover. Null while healthy.
|
|
@@ -240,11 +290,30 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
240
290
|
Date.now() < this.retryNotBefore) {
|
|
241
291
|
return;
|
|
242
292
|
}
|
|
243
|
-
|
|
244
|
-
|
|
293
|
+
// A batch the receiver turned away goes again once a later one got
|
|
294
|
+
// through, or when nothing later is waiting to go ahead of it; until
|
|
295
|
+
// then, later batches go first.
|
|
296
|
+
const oldest = this.refused[0];
|
|
297
|
+
let batch;
|
|
298
|
+
let retried;
|
|
299
|
+
if (oldest &&
|
|
300
|
+
(oldest.outlived || this.queue.length === this.refusedSpans)) {
|
|
301
|
+
retried = this.refused.shift();
|
|
302
|
+
this.refusedSpans -= retried.spans;
|
|
303
|
+
batch = this.queue.splice(0, retried.spans);
|
|
304
|
+
this.queuedChars -= totalChars(batch);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
batch = this.takeBatch(this.refusedSpans);
|
|
308
|
+
}
|
|
309
|
+
this.sending = this.send(batch, retried).finally(() => {
|
|
245
310
|
this.sending = null;
|
|
246
|
-
|
|
247
|
-
|
|
311
|
+
if (this.shutdownPromise) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
// A backlog left behind a full batch goes straight out after it, as
|
|
315
|
+
// does a refused batch a later one has just outlived.
|
|
316
|
+
if (this.refused[0]?.outlived || this.holdsFullBatch()) {
|
|
248
317
|
this.flush();
|
|
249
318
|
}
|
|
250
319
|
});
|
|
@@ -254,26 +323,60 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
254
323
|
return this.shutdownPromise;
|
|
255
324
|
}
|
|
256
325
|
enqueue(spans) {
|
|
257
|
-
if (this.
|
|
326
|
+
if (!this.fits(spans)) {
|
|
258
327
|
this.droppedSpans += spans.length;
|
|
259
328
|
if (!this.reportedDrop) {
|
|
260
329
|
this.reportedDrop = true;
|
|
261
|
-
this.logger.warn(`OTLP export is falling behind:
|
|
330
|
+
this.logger.warn(`OTLP export is falling behind: the spans waiting for ${this.settings?.url} have reached the limit of what is held (${MAX_QUEUED_SPANS} spans, or ${MAX_QUEUED_CHARS} characters of text in them), so newer traces are dropped until it catches up. Dropped spans are counted and reported when it does.`);
|
|
262
331
|
}
|
|
263
332
|
return;
|
|
264
333
|
}
|
|
334
|
+
this.queueAtEnd(spans);
|
|
335
|
+
if (this.holdsFullBatch()) {
|
|
336
|
+
this.flush();
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/** Whether `spans` can wait in the queue as it stands, within both limits. */
|
|
340
|
+
fits(spans) {
|
|
341
|
+
return (this.queue.length + spans.length <= MAX_QUEUED_SPANS &&
|
|
342
|
+
this.queuedChars + totalChars(spans) <= MAX_QUEUED_CHARS);
|
|
343
|
+
}
|
|
344
|
+
queueAtEnd(spans) {
|
|
265
345
|
for (const span of spans) {
|
|
266
346
|
this.queue.push(span);
|
|
267
347
|
}
|
|
268
|
-
|
|
269
|
-
|
|
348
|
+
this.queuedChars += totalChars(spans);
|
|
349
|
+
}
|
|
350
|
+
/** Whether what waits fills a batch by either of its limits. */
|
|
351
|
+
holdsFullBatch() {
|
|
352
|
+
return (this.queue.length >= MAX_BATCH_SPANS ||
|
|
353
|
+
this.queuedChars >= MAX_BATCH_CHARS);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Takes the next batch off the queue from `start`: as many spans as fit
|
|
357
|
+
* in `MAX_BATCH_SPANS` and `MAX_BATCH_CHARS`, and always at least one, so
|
|
358
|
+
* a span larger than a batch on its own still goes out.
|
|
359
|
+
*/
|
|
360
|
+
takeBatch(start) {
|
|
361
|
+
let count = 0;
|
|
362
|
+
let chars = 0;
|
|
363
|
+
while (start + count < this.queue.length && count < MAX_BATCH_SPANS) {
|
|
364
|
+
const next = charsOf(this.queue[start + count]);
|
|
365
|
+
if (count > 0 && chars + next > MAX_BATCH_CHARS) {
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
chars += next;
|
|
369
|
+
count += 1;
|
|
270
370
|
}
|
|
371
|
+
this.queuedChars -= chars;
|
|
372
|
+
return this.queue.splice(start, count);
|
|
271
373
|
}
|
|
272
374
|
/**
|
|
273
|
-
* One export
|
|
274
|
-
*
|
|
375
|
+
* One export - of `retried`, when it is a refused batch going again.
|
|
376
|
+
* Resolves whatever happens: a failure is reported and either put back
|
|
377
|
+
* for a retry or dropped, never thrown.
|
|
275
378
|
*/
|
|
276
|
-
async send(batch) {
|
|
379
|
+
async send(batch, retried) {
|
|
277
380
|
const settings = this.settings;
|
|
278
381
|
let body;
|
|
279
382
|
try {
|
|
@@ -285,7 +388,6 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
285
388
|
}
|
|
286
389
|
catch (error) {
|
|
287
390
|
// Nothing a retry would change: the batch is lost, and said so.
|
|
288
|
-
this.lastSendFailed = true;
|
|
289
391
|
this.reportFailure(`the batch could not be encoded (${String(error)})`, false);
|
|
290
392
|
return;
|
|
291
393
|
}
|
|
@@ -301,34 +403,80 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
301
403
|
return;
|
|
302
404
|
}
|
|
303
405
|
const code = error.code;
|
|
304
|
-
this.retryLater(batch, undefined);
|
|
305
|
-
this.reportFailure(`the receiver could not be reached (${code ?? String(error)})`,
|
|
406
|
+
const retrying = this.retryLater(batch, retried, undefined);
|
|
407
|
+
this.reportFailure(`the receiver could not be reached (${code ?? String(error)})`, retrying);
|
|
306
408
|
return;
|
|
307
409
|
}
|
|
308
410
|
if (reply.status >= 200 && reply.status < 300) {
|
|
309
411
|
this.recovered(batch.length);
|
|
310
412
|
return;
|
|
311
413
|
}
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
this.retryLater(batch, reply.retryAfter);
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
this.lastSendFailed = true;
|
|
318
|
-
}
|
|
414
|
+
const retrying = RETRYABLE_STATUSES.has(reply.status) &&
|
|
415
|
+
this.retryLater(batch, retried, reply.retryAfter);
|
|
319
416
|
const detail = reply.body && /json|text/.test(reply.contentType ?? "")
|
|
320
417
|
? ` - ${reply.body.slice(0, 300)}`
|
|
321
418
|
: "";
|
|
322
|
-
this.reportFailure(`it answered ${reply.status}${detail}`,
|
|
419
|
+
this.reportFailure(`it answered ${reply.status}${detail}`, retrying);
|
|
323
420
|
}
|
|
324
421
|
/**
|
|
325
|
-
*
|
|
326
|
-
*
|
|
422
|
+
* Sets a batch the receiver turned away aside for a retry, behind the
|
|
423
|
+
* batches refused before it and ahead of everything else, if it still
|
|
424
|
+
* fits; and holds exports back for the receiver's `Retry-After` or the
|
|
425
|
+
* next backoff step. Returns whether it will be retried.
|
|
426
|
+
*
|
|
427
|
+
* A receiver that is away refuses whatever it is sent, but one may also
|
|
428
|
+
* refuse a batch for what is in it - larger than what it passes spans on
|
|
429
|
+
* to takes, say - with the same statuses. Put back at the front and sent
|
|
430
|
+
* again first, as it was, such a batch was refused for as long as the
|
|
431
|
+
* process ran, took in every span queued behind it at each try, and not
|
|
432
|
+
* one trace was exported after it. So later batches now go ahead of it,
|
|
433
|
+
* and once one of them got through it goes again first, a second apart
|
|
434
|
+
* or as long as `Retry-After` asks, and is dropped - counted, and the
|
|
435
|
+
* backoff left as the accepted export reset it - when it has been
|
|
436
|
+
* refused `MAX_REFUSALS_ONCE_OUTLIVED` times more: a receiver that only
|
|
437
|
+
* limits how often it is sent to takes it within those. During an outage
|
|
438
|
+
* nothing gets through, so nothing is dropped before the queue is full;
|
|
439
|
+
* each refused batch goes again once the receiver is back. At shutdown
|
|
440
|
+
* nothing is put back: the drain gives each batch one try.
|
|
327
441
|
*/
|
|
328
|
-
retryLater(batch, retryAfter) {
|
|
329
|
-
this.
|
|
330
|
-
|
|
442
|
+
retryLater(batch, retried, retryAfter) {
|
|
443
|
+
if (this.shutdownPromise) {
|
|
444
|
+
this.droppedSpans += batch.length;
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
if (retried?.outlived) {
|
|
448
|
+
// The receiver took an export since this one was first refused: it
|
|
449
|
+
// is not away, so this does not add to the backoff.
|
|
450
|
+
retried.refusalsOnceOutlived += 1;
|
|
451
|
+
if (retried.refusalsOnceOutlived >= MAX_REFUSALS_ONCE_OUTLIVED ||
|
|
452
|
+
!this.fits(batch)) {
|
|
453
|
+
this.droppedSpans += batch.length;
|
|
454
|
+
this.holdBack(retryAfter, 0);
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
331
457
|
this.queue.unshift(...batch);
|
|
458
|
+
this.refused.unshift(retried);
|
|
459
|
+
this.refusedSpans += batch.length;
|
|
460
|
+
this.queuedChars += totalChars(batch);
|
|
461
|
+
this.holdBack(retryAfter, RETRY_BASE_MS);
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
if (this.fits(batch)) {
|
|
465
|
+
const refused = {
|
|
466
|
+
spans: batch.length,
|
|
467
|
+
outlived: false,
|
|
468
|
+
refusalsOnceOutlived: 0,
|
|
469
|
+
};
|
|
470
|
+
if (retried) {
|
|
471
|
+
this.queue.unshift(...batch);
|
|
472
|
+
this.refused.unshift(refused);
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
this.queue.splice(this.refusedSpans, 0, ...batch);
|
|
476
|
+
this.refused.push(refused);
|
|
477
|
+
}
|
|
478
|
+
this.refusedSpans += batch.length;
|
|
479
|
+
this.queuedChars += totalChars(batch);
|
|
332
480
|
}
|
|
333
481
|
else {
|
|
334
482
|
this.droppedSpans += batch.length;
|
|
@@ -337,15 +485,35 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
337
485
|
this.retryDelayMs === 0
|
|
338
486
|
? RETRY_BASE_MS
|
|
339
487
|
: Math.min(this.retryDelayMs * 2, RETRY_MAX_MS);
|
|
488
|
+
this.holdBack(retryAfter, this.retryDelayMs);
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Holds exports back for the receiver's `Retry-After`, or else for
|
|
493
|
+
* `otherwiseMs`.
|
|
494
|
+
*
|
|
495
|
+
* Never sooner than the first backoff step when the receiver asks.
|
|
496
|
+
* `Retry-After` is whole seconds, so no receiver that follows HTTP asks
|
|
497
|
+
* for less; one that answered `0.0000001` had every flush - and the
|
|
498
|
+
* follow-up after each full batch - send the same export again at once,
|
|
499
|
+
* as fast as the application's thread could encode it.
|
|
500
|
+
*/
|
|
501
|
+
holdBack(retryAfter, otherwiseMs) {
|
|
340
502
|
const asked = Number(retryAfter);
|
|
341
503
|
const delay = Number.isFinite(asked) && asked > 0
|
|
342
|
-
? Math.min(asked * 1000, RETRY_AFTER_MAX_MS)
|
|
343
|
-
:
|
|
504
|
+
? Math.min(Math.max(asked * 1000, RETRY_BASE_MS), RETRY_AFTER_MAX_MS)
|
|
505
|
+
: otherwiseMs;
|
|
344
506
|
this.retryNotBefore = Date.now() + delay;
|
|
345
507
|
}
|
|
346
|
-
/**
|
|
508
|
+
/**
|
|
509
|
+
* Clears the failure state after an accepted export, and says so. Every
|
|
510
|
+
* batch refused before it has now outlived a refusal that may have been
|
|
511
|
+
* the receiver's, and goes again next.
|
|
512
|
+
*/
|
|
347
513
|
recovered(exported) {
|
|
348
|
-
this.
|
|
514
|
+
for (const refused of this.refused) {
|
|
515
|
+
refused.outlived = true;
|
|
516
|
+
}
|
|
349
517
|
this.retryDelayMs = 0;
|
|
350
518
|
this.retryNotBefore = 0;
|
|
351
519
|
if (this.failure !== null || this.droppedSpans > 0) {
|
|
@@ -407,9 +575,8 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
407
575
|
},
|
|
408
576
|
}, (response) => {
|
|
409
577
|
const chunks = [];
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
response.on("end", () => {
|
|
578
|
+
let received = 0;
|
|
579
|
+
const settle = () => {
|
|
413
580
|
const retryAfter = response.headers["retry-after"];
|
|
414
581
|
resolve({
|
|
415
582
|
status: response.statusCode ?? 0,
|
|
@@ -417,12 +584,55 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
417
584
|
? retryAfter[0]
|
|
418
585
|
: retryAfter,
|
|
419
586
|
contentType: response.headers["content-type"],
|
|
420
|
-
body: Buffer.concat(chunks).toString("utf8"),
|
|
587
|
+
body: Buffer.concat(chunks, Math.min(received, MAX_REPLY_BYTES)).toString("utf8"),
|
|
421
588
|
});
|
|
589
|
+
// A reply that ends before the batch has been written leaves a
|
|
590
|
+
// socket that is never pooled, timed out or closed again.
|
|
591
|
+
if (!request.writableFinished) {
|
|
592
|
+
request.destroy();
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
response.on("data", (chunk) => {
|
|
596
|
+
if (received >= MAX_REPLY_BYTES) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
chunks.push(chunk);
|
|
600
|
+
received += chunk.length;
|
|
601
|
+
if (received >= MAX_REPLY_BYTES) {
|
|
602
|
+
response.destroy();
|
|
603
|
+
settle();
|
|
604
|
+
}
|
|
422
605
|
});
|
|
606
|
+
response.on("error", reject);
|
|
607
|
+
response.on("end", settle);
|
|
423
608
|
});
|
|
424
609
|
request.on("error", reject);
|
|
425
|
-
|
|
610
|
+
// A 101 with `Connection: Upgrade` is no response to Node: it emits
|
|
611
|
+
// only this, and destroys the socket without an error. Unheard, the
|
|
612
|
+
// export would never settle and every later one would wait behind it.
|
|
613
|
+
// No upgrade was asked for: it is a status like any other refusal.
|
|
614
|
+
request.on("upgrade", (response, socket) => {
|
|
615
|
+
socket.destroy();
|
|
616
|
+
resolve({
|
|
617
|
+
status: response.statusCode ?? 101,
|
|
618
|
+
retryAfter: undefined,
|
|
619
|
+
contentType: undefined,
|
|
620
|
+
body: "",
|
|
621
|
+
});
|
|
622
|
+
});
|
|
623
|
+
// A limit on the export as a whole, which is what `timeoutMillis` is.
|
|
624
|
+
// `request.setTimeout` measures silence on the socket instead and
|
|
625
|
+
// starts over with every byte: a receiver that went on writing -
|
|
626
|
+
// interim replies, a body a byte at a time - would hold this export,
|
|
627
|
+
// and every one queued behind it, open for as long as it liked.
|
|
628
|
+
const deadline = setTimeout(() => request.destroy(Object.assign(new Error(`no answer within ${settings.timeoutMs} ms`), { code: "ETIMEDOUT" })), Math.min(settings.timeoutMs, MAX_TIMER_MS));
|
|
629
|
+
request.on("close", () => clearTimeout(deadline));
|
|
630
|
+
let interimReplies = 0;
|
|
631
|
+
request.on("information", () => {
|
|
632
|
+
if (++interimReplies > MAX_INTERIM_REPLIES) {
|
|
633
|
+
request.destroy(new Error(`it sent more than ${MAX_INTERIM_REPLIES} interim (1xx) replies`));
|
|
634
|
+
}
|
|
635
|
+
});
|
|
426
636
|
request.end(body);
|
|
427
637
|
});
|
|
428
638
|
}
|
|
@@ -433,8 +643,11 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
433
643
|
* Operations finish their traces a tick after their response, and one
|
|
434
644
|
* whose spans were still open waits a further second in the registry, so
|
|
435
645
|
* the drain waits for both before deciding it is done - as the Observe
|
|
436
|
-
* worker's final flush does.
|
|
437
|
-
*
|
|
646
|
+
* worker's final flush does. Each batch still waiting is sent once, with
|
|
647
|
+
* no backoff in between: one the receiver fails is not retried - shutdown
|
|
648
|
+
* is not the time to wait one out - and the drain goes on with the next,
|
|
649
|
+
* so the batch a receiver refuses for what is in it no longer takes every
|
|
650
|
+
* later one with it.
|
|
438
651
|
*/
|
|
439
652
|
async shutDown() {
|
|
440
653
|
if (this.flushTimer) {
|
|
@@ -478,9 +691,6 @@ let OtlpTraceExporter = OtlpTraceExporter_1 = class OtlpTraceExporter {
|
|
|
478
691
|
sleep(remaining, undefined, { signal: timeout.signal }).catch(() => undefined),
|
|
479
692
|
]);
|
|
480
693
|
timeout.abort();
|
|
481
|
-
if (this.lastSendFailed) {
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
694
|
continue;
|
|
485
695
|
}
|
|
486
696
|
if (this.operationTraceRegistry.hasSnapshotsAwaitingCompletion()) {
|
|
@@ -502,3 +712,45 @@ OtlpTraceExporter = OtlpTraceExporter_1 = __decorate([
|
|
|
502
712
|
AsyncLocalStorage])
|
|
503
713
|
], OtlpTraceExporter);
|
|
504
714
|
export { OtlpTraceExporter };
|
|
715
|
+
/** What `charsOf` has counted, so a span is walked once however often it moves. */
|
|
716
|
+
const countedChars = new WeakMap();
|
|
717
|
+
/**
|
|
718
|
+
* How many characters of text a span carries: its ids and name, its trace
|
|
719
|
+
* state and status message, and the keys and string values of its
|
|
720
|
+
* attributes and its events'. What the queue and a batch are measured in -
|
|
721
|
+
* not the bytes an export takes, which the encoding decides, but what grows
|
|
722
|
+
* with them.
|
|
723
|
+
*/
|
|
724
|
+
function charsOf(span) {
|
|
725
|
+
let chars = countedChars.get(span);
|
|
726
|
+
if (chars !== undefined) {
|
|
727
|
+
return chars;
|
|
728
|
+
}
|
|
729
|
+
chars =
|
|
730
|
+
span.traceId.length +
|
|
731
|
+
span.spanId.length +
|
|
732
|
+
(span.parentSpanId?.length ?? 0) +
|
|
733
|
+
(span.traceState?.length ?? 0) +
|
|
734
|
+
span.name.length +
|
|
735
|
+
(span.status?.message?.length ?? 0) +
|
|
736
|
+
attributeChars(span.attributes);
|
|
737
|
+
for (const event of span.events) {
|
|
738
|
+
chars += event.name.length + attributeChars(event.attributes);
|
|
739
|
+
}
|
|
740
|
+
countedChars.set(span, chars);
|
|
741
|
+
return chars;
|
|
742
|
+
}
|
|
743
|
+
function attributeChars(attributes) {
|
|
744
|
+
let chars = 0;
|
|
745
|
+
for (const { key, value } of attributes) {
|
|
746
|
+
chars += key.length + (typeof value === "string" ? value.length : 0);
|
|
747
|
+
}
|
|
748
|
+
return chars;
|
|
749
|
+
}
|
|
750
|
+
function totalChars(spans) {
|
|
751
|
+
let chars = 0;
|
|
752
|
+
for (const span of spans) {
|
|
753
|
+
chars += charsOf(span);
|
|
754
|
+
}
|
|
755
|
+
return chars;
|
|
756
|
+
}
|
|
@@ -45,12 +45,14 @@ export interface OutgoingTrace {
|
|
|
45
45
|
/**
|
|
46
46
|
* Spans for outbound HTTP, from the diagnostics channels Node publishes on its
|
|
47
47
|
* own: `undici` - which is also what the global `fetch` runs on - and the
|
|
48
|
-
* `node:http` client that axios, got and most SDKs sit on.
|
|
49
|
-
* so there is
|
|
48
|
+
* `node:http` client that axios, got and most SDKs sit on. No HTTP client is
|
|
49
|
+
* patched, so there is none to keep up with.
|
|
50
50
|
*
|
|
51
51
|
* The handlers run synchronously inside the call that makes the request, which
|
|
52
52
|
* is what puts the span under the right parent; the span is then carried to
|
|
53
|
-
* the completion events on the request object itself.
|
|
53
|
+
* the completion events on the request object itself. An undici request a
|
|
54
|
+
* pool queued, or a redirect it follows, is created later, from a socket's
|
|
55
|
+
* events: `patchUndiciDispatch` puts it back in its caller's context first.
|
|
54
56
|
*
|
|
55
57
|
* Returns the function that removes every subscription.
|
|
56
58
|
*/
|
|
@@ -65,4 +67,6 @@ getRedactor?: () => LogRedactor | null,
|
|
|
65
67
|
* `traceContext` not off, while the agent reports to NestJS Observe (see
|
|
66
68
|
* `ObserveAgentSharedBuffer.isReporting`). Off unless given.
|
|
67
69
|
*/
|
|
68
|
-
traceContextOn?: () => boolean
|
|
70
|
+
traceContextOn?: () => boolean,
|
|
71
|
+
/** `http.queryParamsObfuscateRegex`, global: held to every recorded URL. */
|
|
72
|
+
obfuscateUrl?: RegExp): () => void;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
1
2
|
import { subscribe, unsubscribe } from "diagnostics_channel";
|
|
2
3
|
import { openTelemetryWritesTraceparent } from "../utils/opentelemetry.util.js";
|
|
3
4
|
import { redactUrlQuery } from "../utils/redact-url-query.js";
|
|
@@ -10,12 +11,14 @@ const TRACESTATE_HEADER = "tracestate";
|
|
|
10
11
|
/**
|
|
11
12
|
* Spans for outbound HTTP, from the diagnostics channels Node publishes on its
|
|
12
13
|
* own: `undici` - which is also what the global `fetch` runs on - and the
|
|
13
|
-
* `node:http` client that axios, got and most SDKs sit on.
|
|
14
|
-
* so there is
|
|
14
|
+
* `node:http` client that axios, got and most SDKs sit on. No HTTP client is
|
|
15
|
+
* patched, so there is none to keep up with.
|
|
15
16
|
*
|
|
16
17
|
* The handlers run synchronously inside the call that makes the request, which
|
|
17
18
|
* is what puts the span under the right parent; the span is then carried to
|
|
18
|
-
* the completion events on the request object itself.
|
|
19
|
+
* the completion events on the request object itself. An undici request a
|
|
20
|
+
* pool queued, or a redirect it follows, is created later, from a socket's
|
|
21
|
+
* events: `patchUndiciDispatch` puts it back in its caller's context first.
|
|
19
22
|
*
|
|
20
23
|
* Returns the function that removes every subscription.
|
|
21
24
|
*/
|
|
@@ -30,13 +33,20 @@ getRedactor = () => null,
|
|
|
30
33
|
* `traceContext` not off, while the agent reports to NestJS Observe (see
|
|
31
34
|
* `ObserveAgentSharedBuffer.isReporting`). Off unless given.
|
|
32
35
|
*/
|
|
33
|
-
traceContextOn = () => false
|
|
36
|
+
traceContextOn = () => false,
|
|
37
|
+
/** `http.queryParamsObfuscateRegex`, global: held to every recorded URL. */
|
|
38
|
+
obfuscateUrl) {
|
|
34
39
|
const spans = new WeakMap();
|
|
35
40
|
/**
|
|
36
41
|
* Span ids minted for a `node:http` request at `created`, where its
|
|
37
42
|
* `traceparent` has to be written, for the span `start` opens later.
|
|
38
43
|
*/
|
|
39
44
|
const presetSpanIds = new WeakMap();
|
|
45
|
+
/**
|
|
46
|
+
* The async context a `node:http` request was created in, for `start` to
|
|
47
|
+
* open its span in - see `http.client.request.start`.
|
|
48
|
+
*/
|
|
49
|
+
const createdIn = new WeakMap();
|
|
40
50
|
const subscriptions = [];
|
|
41
51
|
const on = (channel, handler) => {
|
|
42
52
|
const guarded = (message) => {
|
|
@@ -67,7 +77,7 @@ traceContextOn = () => false) {
|
|
|
67
77
|
}
|
|
68
78
|
const span = recorder.open("http", `${method} ${host}`, {
|
|
69
79
|
"http.method": method,
|
|
70
|
-
"http.url": redactUrlQuery(url, getRedactor()),
|
|
80
|
+
"http.url": redactUrlQuery(url, getRedactor(), obfuscateUrl),
|
|
71
81
|
}, presetSpanId);
|
|
72
82
|
if (span) {
|
|
73
83
|
spans.set(request, span);
|
|
@@ -101,8 +111,11 @@ traceContextOn = () => false) {
|
|
|
101
111
|
}
|
|
102
112
|
if (sendsTraceContext() &&
|
|
103
113
|
!hasUndiciHeader(request.headers, TRACEPARENT_HEADER)) {
|
|
114
|
+
// Sampled when the operation is recorded, as for node:http, not when
|
|
115
|
+
// this call got a span: one that opened as many spans as it may is
|
|
116
|
+
// still recorded, and its callee must not drop the trace.
|
|
104
117
|
const span = spans.get(request);
|
|
105
|
-
const headers = traceContextHeaders(trace, span?.spanId,
|
|
118
|
+
const headers = traceContextHeaders(trace, span?.spanId, recorder.isRecording());
|
|
106
119
|
request.addHeader(TRACEPARENT_HEADER, headers.traceparent);
|
|
107
120
|
if (headers.tracestate &&
|
|
108
121
|
!hasUndiciHeader(request.headers, TRACESTATE_HEADER)) {
|
|
@@ -122,6 +135,9 @@ traceContextOn = () => false) {
|
|
|
122
135
|
// runtimes there is simply no propagation for this client; `start` below
|
|
123
136
|
// fires after the header block is already on its way.
|
|
124
137
|
on("http.client.request.created", ({ request }) => {
|
|
138
|
+
// Taken for every request, in a trace or not: whichever context
|
|
139
|
+
// `start` fires in is no more this request's when it has none.
|
|
140
|
+
createdIn.set(request, AsyncLocalStorage.snapshot());
|
|
125
141
|
const { url } = describeNodeRequest(request);
|
|
126
142
|
const trace = currentTrace();
|
|
127
143
|
if (!trace || options.ignore?.(url) || !shouldPropagate(url)) {
|
|
@@ -145,19 +161,35 @@ traceContextOn = () => false) {
|
|
|
145
161
|
}
|
|
146
162
|
}
|
|
147
163
|
});
|
|
164
|
+
// `start` fires once the request is ended, which is not always in the call
|
|
165
|
+
// that made it: a body piped in ends it from the source's `end` event, in
|
|
166
|
+
// the context of whoever opened the source's socket - another request's,
|
|
167
|
+
// for a pooled connection an earlier one opened. So the span opens in the
|
|
168
|
+
// context the request was created in, where `created` fired; on runtimes
|
|
169
|
+
// without that event, where `start` fired.
|
|
148
170
|
on("http.client.request.start", ({ request }) => {
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
171
|
+
const runInCreated = createdIn.get(request);
|
|
172
|
+
createdIn.delete(request);
|
|
173
|
+
const begin = () => {
|
|
174
|
+
const { host, url, method } = describeNodeRequest(request);
|
|
175
|
+
const presetSpanId = presetSpanIds.get(request);
|
|
176
|
+
presetSpanIds.delete(request);
|
|
177
|
+
if (!open(request, method, url, host, presetSpanId)) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
// `close` always fires, with or without a response. An `error`
|
|
181
|
+
// listener would tell more, but adding one changes what an otherwise
|
|
182
|
+
// unhandled request error does to the process.
|
|
183
|
+
request.once?.("close", () => close(request, request.res
|
|
184
|
+
? undefined
|
|
185
|
+
: new Error("Request closed with no response")));
|
|
186
|
+
};
|
|
187
|
+
if (runInCreated) {
|
|
188
|
+
runInCreated(begin);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
begin();
|
|
154
192
|
}
|
|
155
|
-
// `close` always fires, with or without a response. An `error` listener
|
|
156
|
-
// would tell more, but adding one changes what an otherwise unhandled
|
|
157
|
-
// request error does to the process.
|
|
158
|
-
request.once?.("close", () => close(request, request.res
|
|
159
|
-
? undefined
|
|
160
|
-
: new Error("Request closed with no response")));
|
|
161
193
|
});
|
|
162
194
|
on("http.client.request.error", ({ request, error }) => close(request, error));
|
|
163
195
|
on("http.client.response.finish", ({ request }) => close(request));
|