risicare 0.10.0 → 0.11.0
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/CHANGELOG.md +76 -5
- package/README.md +157 -39
- package/dist/frameworks/instructor.cjs +338 -117
- package/dist/frameworks/instructor.cjs.map +1 -1
- package/dist/frameworks/instructor.js +338 -117
- package/dist/frameworks/instructor.js.map +1 -1
- package/dist/frameworks/langchain.cjs +351 -130
- package/dist/frameworks/langchain.cjs.map +1 -1
- package/dist/frameworks/langchain.js +351 -130
- package/dist/frameworks/langchain.js.map +1 -1
- package/dist/frameworks/langgraph.cjs +351 -130
- package/dist/frameworks/langgraph.cjs.map +1 -1
- package/dist/frameworks/langgraph.js +351 -130
- package/dist/frameworks/langgraph.js.map +1 -1
- package/dist/frameworks/llamaindex.cjs +351 -130
- package/dist/frameworks/llamaindex.cjs.map +1 -1
- package/dist/frameworks/llamaindex.js +351 -130
- package/dist/frameworks/llamaindex.js.map +1 -1
- package/dist/index.cjs +808 -226
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +137 -21
- package/dist/index.d.ts +137 -21
- package/dist/index.js +804 -226
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic/index.cjs +351 -130
- package/dist/providers/anthropic/index.cjs.map +1 -1
- package/dist/providers/anthropic/index.js +351 -130
- package/dist/providers/anthropic/index.js.map +1 -1
- package/dist/providers/bedrock/index.cjs +346 -125
- package/dist/providers/bedrock/index.cjs.map +1 -1
- package/dist/providers/bedrock/index.js +346 -125
- package/dist/providers/bedrock/index.js.map +1 -1
- package/dist/providers/cerebras/index.cjs +351 -130
- package/dist/providers/cerebras/index.cjs.map +1 -1
- package/dist/providers/cerebras/index.js +351 -130
- package/dist/providers/cerebras/index.js.map +1 -1
- package/dist/providers/cohere/index.cjs +351 -130
- package/dist/providers/cohere/index.cjs.map +1 -1
- package/dist/providers/cohere/index.js +351 -130
- package/dist/providers/cohere/index.js.map +1 -1
- package/dist/providers/google/index.cjs +351 -130
- package/dist/providers/google/index.cjs.map +1 -1
- package/dist/providers/google/index.js +351 -130
- package/dist/providers/google/index.js.map +1 -1
- package/dist/providers/groq/index.cjs +351 -130
- package/dist/providers/groq/index.cjs.map +1 -1
- package/dist/providers/groq/index.js +351 -130
- package/dist/providers/groq/index.js.map +1 -1
- package/dist/providers/huggingface/index.cjs +351 -130
- package/dist/providers/huggingface/index.cjs.map +1 -1
- package/dist/providers/huggingface/index.js +351 -130
- package/dist/providers/huggingface/index.js.map +1 -1
- package/dist/providers/mistral/index.cjs +351 -130
- package/dist/providers/mistral/index.cjs.map +1 -1
- package/dist/providers/mistral/index.js +351 -130
- package/dist/providers/mistral/index.js.map +1 -1
- package/dist/providers/ollama/index.cjs +351 -130
- package/dist/providers/ollama/index.cjs.map +1 -1
- package/dist/providers/ollama/index.js +351 -130
- package/dist/providers/ollama/index.js.map +1 -1
- package/dist/providers/openai/index.cjs +351 -130
- package/dist/providers/openai/index.cjs.map +1 -1
- package/dist/providers/openai/index.js +351 -130
- package/dist/providers/openai/index.js.map +1 -1
- package/dist/providers/together/index.cjs +351 -130
- package/dist/providers/together/index.cjs.map +1 -1
- package/dist/providers/together/index.js +351 -130
- package/dist/providers/together/index.js.map +1 -1
- package/dist/providers/vercel-ai/index.cjs +351 -130
- package/dist/providers/vercel-ai/index.cjs.map +1 -1
- package/dist/providers/vercel-ai/index.js +351 -130
- package/dist/providers/vercel-ai/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -38,6 +38,35 @@ module.exports = __toCommonJS(bedrock_exports);
|
|
|
38
38
|
// src/client.ts
|
|
39
39
|
var import_node_crypto3 = require("crypto");
|
|
40
40
|
|
|
41
|
+
// src/exporters/retry.ts
|
|
42
|
+
var RETRY_STATUSES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
43
|
+
var TOO_LARGE_STATUS = 413;
|
|
44
|
+
var RETRY_BACKOFF_BASE_S = 1;
|
|
45
|
+
var RETRY_BACKOFF_CAP_S = 30;
|
|
46
|
+
var DEFAULT_RETRY_HORIZON_S = 300;
|
|
47
|
+
var DEFAULT_SHUTDOWN_TIMEOUT_S = 10;
|
|
48
|
+
var RETRY_WARN_AFTER_ATTEMPTS = 3;
|
|
49
|
+
var jitter = { fraction: () => Math.random() };
|
|
50
|
+
function backoffDelayS(retryNumber, fraction) {
|
|
51
|
+
const exponent = Math.min(Math.max(retryNumber, 0), 62);
|
|
52
|
+
const ceiling = Math.min(RETRY_BACKOFF_CAP_S, RETRY_BACKOFF_BASE_S * 2 ** exponent);
|
|
53
|
+
return Math.min(Math.max(fraction, 0), 1) * ceiling;
|
|
54
|
+
}
|
|
55
|
+
var SECONDS = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
|
|
56
|
+
function parseRetryAfterS(value, nowMs = Date.now()) {
|
|
57
|
+
if (value === null || value === void 0) return null;
|
|
58
|
+
const text = String(value).trim();
|
|
59
|
+
if (text === "") return null;
|
|
60
|
+
if (SECONDS.test(text)) {
|
|
61
|
+
const seconds = Number(text);
|
|
62
|
+
return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
|
|
63
|
+
}
|
|
64
|
+
if (!/[A-Za-z]/.test(text)) return null;
|
|
65
|
+
const when = Date.parse(text);
|
|
66
|
+
if (!Number.isFinite(when)) return null;
|
|
67
|
+
return Math.max(0, (when - nowMs) / 1e3);
|
|
68
|
+
}
|
|
69
|
+
|
|
41
70
|
// src/globals.ts
|
|
42
71
|
var import_node_async_hooks = require("async_hooks");
|
|
43
72
|
var G = globalThis;
|
|
@@ -184,10 +213,19 @@ var VALUE_RULE_PREFIXES = new Set(
|
|
|
184
213
|
);
|
|
185
214
|
|
|
186
215
|
// src/exporters/batch.ts
|
|
216
|
+
var SHUTDOWN_BUDGET_MS = DEFAULT_SHUTDOWN_TIMEOUT_S * 1e3;
|
|
187
217
|
var MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
188
218
|
var MAX_BATCH_BYTES = 8 * 1024 * 1024;
|
|
189
219
|
var utf8ByteLength = typeof Buffer !== "undefined" && typeof Buffer.byteLength === "function" ? (s) => Buffer.byteLength(s, "utf8") : (s) => s.length * 3;
|
|
190
220
|
|
|
221
|
+
// src/context/storage.ts
|
|
222
|
+
function storage() {
|
|
223
|
+
return getContextStorage();
|
|
224
|
+
}
|
|
225
|
+
function getContext() {
|
|
226
|
+
return storage().getStore() ?? {};
|
|
227
|
+
}
|
|
228
|
+
|
|
191
229
|
// src/ids.ts
|
|
192
230
|
var import_node_crypto = require("crypto");
|
|
193
231
|
|
|
@@ -318,16 +356,8 @@ var SUPPLEMENTAL_RULES = [
|
|
|
318
356
|
{ re: new RegExp("json|syntaxerror|unexpected token|decodeerror", "u"), code: "PERCEPTION.PARSING.JSON_INVALID" }
|
|
319
357
|
];
|
|
320
358
|
|
|
321
|
-
// src/context/storage.ts
|
|
322
|
-
function storage() {
|
|
323
|
-
return getContextStorage();
|
|
324
|
-
}
|
|
325
|
-
function getContext() {
|
|
326
|
-
return storage().getStore() ?? {};
|
|
327
|
-
}
|
|
328
|
-
|
|
329
359
|
// src/version.ts
|
|
330
|
-
var SDK_VERSION = "0.
|
|
360
|
+
var SDK_VERSION = "0.11.0";
|
|
331
361
|
|
|
332
362
|
// src/exporters/quota.ts
|
|
333
363
|
var QUOTA_REASON = "monthly_quota_exceeded";
|
|
@@ -435,12 +465,18 @@ var QuotaPause = class {
|
|
|
435
465
|
|
|
436
466
|
// src/exporters/http.ts
|
|
437
467
|
var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
|
|
438
|
-
var RETRY_AFTER_CEILING_MS = 1e4;
|
|
439
|
-
var REFUSAL_BACKOFF_BASE_MS = 1e3;
|
|
440
|
-
var REFUSAL_NO_HINT = -1;
|
|
441
468
|
var MISROUTE_WARN_WINDOW_MS = 3e5;
|
|
442
469
|
var AUTH_REFUSAL_STATUSES = /* @__PURE__ */ new Set([401, 403]);
|
|
443
470
|
var FAILURE_WARN_WINDOW_MS = 3e5;
|
|
471
|
+
var MAX_TIMER_MS = 2 ** 31 - 1;
|
|
472
|
+
function validHorizonS(value) {
|
|
473
|
+
if (value === void 0) return DEFAULT_RETRY_HORIZON_S;
|
|
474
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0) return value;
|
|
475
|
+
warn(
|
|
476
|
+
`Risicare: retryHorizonS=${String(value)} is not a finite number of 0 or more; using the default, ${DEFAULT_RETRY_HORIZON_S} s.`
|
|
477
|
+
);
|
|
478
|
+
return DEFAULT_RETRY_HORIZON_S;
|
|
479
|
+
}
|
|
444
480
|
var HttpExporter = class {
|
|
445
481
|
name = "http";
|
|
446
482
|
_endpoint;
|
|
@@ -450,20 +486,28 @@ var HttpExporter = class {
|
|
|
450
486
|
_timeoutMs;
|
|
451
487
|
_maxRetries;
|
|
452
488
|
_compress;
|
|
489
|
+
_retryHorizonS;
|
|
490
|
+
/** Total HTTP requests that this exporter sent (each retry and each half of a split counts). */
|
|
491
|
+
attempts = 0;
|
|
453
492
|
// Circuit breaker (CON-201 / B-16). Threshold and cooldown sized for
|
|
454
|
-
// a "30-second prod restart" scenario
|
|
455
|
-
//
|
|
456
|
-
// cooldown gives the gateway time to come back before the half-open
|
|
457
|
-
// probe fires. Lower numbers (was 3@30s) tripped on transient blips.
|
|
493
|
+
// a "30-second prod restart" scenario. Since W2-1c only answers that a retry
|
|
494
|
+
// cannot change move it (see the block above).
|
|
458
495
|
_consecutiveFailures = 0;
|
|
459
496
|
_lastMisrouteWarnAt = -Infinity;
|
|
460
497
|
_lastFailureWarnAt = -Infinity;
|
|
498
|
+
_lastExhaustedWarnAt = -Infinity;
|
|
499
|
+
/** "log once" (W2-1c): the WARNING for a `too_large` drop is written one time. */
|
|
500
|
+
_tooLargeWarned = false;
|
|
461
501
|
/**
|
|
462
502
|
* The kind of failure that the last outage WARNING named ('auth', 'http', 'none',
|
|
463
503
|
* 'unknown'). `null` after a success. A new kind in the same outage writes one more
|
|
464
504
|
* WARNING, also inside the window.
|
|
465
505
|
*/
|
|
466
506
|
_failureKind = null;
|
|
507
|
+
/** `endRetries()` was called, and `shutdown()` has not run since. */
|
|
508
|
+
_noWait = false;
|
|
509
|
+
/** The wake-ups of the backoff waits in progress. `endRetries()` calls each one. */
|
|
510
|
+
_waits = /* @__PURE__ */ new Set();
|
|
467
511
|
/** The endpoint as a log line may show it: no userinfo, no query (see `safeUrl`). */
|
|
468
512
|
_safeEndpoint;
|
|
469
513
|
/** The endpoint holds userinfo. `fetch` refuses such a URL. */
|
|
@@ -474,6 +518,8 @@ var HttpExporter = class {
|
|
|
474
518
|
_acks = /* @__PURE__ */ new WeakMap();
|
|
475
519
|
/** The requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
|
|
476
520
|
_requests = /* @__PURE__ */ new WeakMap();
|
|
521
|
+
/** The drop reason of each failed call, by its batch. Read once by `takeDropReason(spans)`. */
|
|
522
|
+
_dropReasons = /* @__PURE__ */ new WeakMap();
|
|
477
523
|
/** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
|
|
478
524
|
noteQuotaDrop(count = 1) {
|
|
479
525
|
this._quota.addDrops(count);
|
|
@@ -498,8 +544,16 @@ var HttpExporter = class {
|
|
|
498
544
|
this._projectId = options.projectId;
|
|
499
545
|
this._environment = options.environment;
|
|
500
546
|
this._timeoutMs = options.timeoutMs ?? 5e3;
|
|
501
|
-
|
|
547
|
+
let maxRetries = options.maxRetries;
|
|
548
|
+
if (maxRetries !== void 0 && !(Number.isInteger(maxRetries) && maxRetries >= 1)) {
|
|
549
|
+
warn(
|
|
550
|
+
`Risicare: maxRetries=${String(maxRetries)} is not a whole number of 1 or more; the exporter uses no count limit (the retry horizon is the limit).`
|
|
551
|
+
);
|
|
552
|
+
maxRetries = void 0;
|
|
553
|
+
}
|
|
554
|
+
this._maxRetries = maxRetries;
|
|
502
555
|
this._compress = options.compress ?? false;
|
|
556
|
+
this._retryHorizonS = validHorizonS(options.retryHorizonS);
|
|
503
557
|
this._safeEndpoint = safeUrl(this._endpoint);
|
|
504
558
|
this._endpointHasCredentials = hasCredentials(this._endpoint);
|
|
505
559
|
}
|
|
@@ -521,16 +575,18 @@ var HttpExporter = class {
|
|
|
521
575
|
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
|
|
522
576
|
return `HttpExporter { endpoint: '${this._safeEndpoint}' }`;
|
|
523
577
|
}
|
|
524
|
-
/**
|
|
578
|
+
/**
|
|
579
|
+
* The most attempts for one request body (`maxRetries`). `undefined` when no count is set:
|
|
580
|
+
* the retry horizon is the limit.
|
|
581
|
+
*/
|
|
525
582
|
get attemptsPerExport() {
|
|
526
583
|
return this._maxRetries;
|
|
527
584
|
}
|
|
528
585
|
/**
|
|
529
586
|
* Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
|
|
530
|
-
* it.
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
* kept per batch, as `takeAck` keeps the ack.
|
|
587
|
+
* it. Each retry and each half of a 413 split counts one; a call that the open circuit
|
|
588
|
+
* breaker or the quota pause stops sends none. Returns `null` when no call is on record for
|
|
589
|
+
* `spans`. The answer is kept per batch, as `takeAck` keeps the ack.
|
|
534
590
|
*/
|
|
535
591
|
takeAttempts(spans) {
|
|
536
592
|
const n = this._requests.get(spans) ?? null;
|
|
@@ -547,6 +603,12 @@ var HttpExporter = class {
|
|
|
547
603
|
this._acks.delete(spans);
|
|
548
604
|
return ack;
|
|
549
605
|
}
|
|
606
|
+
/** Returns why the `export()` call for `spans` failed (W2-1c), and clears it. See `SpanExporter`. */
|
|
607
|
+
takeDropReason(spans) {
|
|
608
|
+
const reason = this._dropReasons.get(spans) ?? null;
|
|
609
|
+
this._dropReasons.delete(spans);
|
|
610
|
+
return reason;
|
|
611
|
+
}
|
|
550
612
|
/** Returns the spans of `spans` that could not be serialized and were not sent, and clears them. */
|
|
551
613
|
takeUnserializable(spans) {
|
|
552
614
|
const bad = this._unserializable.get(spans) ?? [];
|
|
@@ -555,7 +617,7 @@ var HttpExporter = class {
|
|
|
555
617
|
}
|
|
556
618
|
/**
|
|
557
619
|
* Cancel the `export()` call for `spans` (all calls when `spans` is not given): its
|
|
558
|
-
* request, its retries and its backoff
|
|
620
|
+
* request, its retries and its backoff wait. The batch processor calls it when
|
|
559
621
|
* `shutdown()` abandons the batch, so that the call does not keep the process alive. The
|
|
560
622
|
* cancelled call returns FAILURE, changes no counter (the circuit breaker included) and
|
|
561
623
|
* logs no WARNING.
|
|
@@ -565,24 +627,39 @@ var HttpExporter = class {
|
|
|
565
627
|
else for (const call of this._calls.values()) call.cancel.abort();
|
|
566
628
|
}
|
|
567
629
|
/**
|
|
568
|
-
*
|
|
630
|
+
* End the retry waits of this exporter until its next `shutdown()`. The batch processor
|
|
631
|
+
* calls it when `shutdown()` starts. A call that waits for a retry makes one more attempt
|
|
632
|
+
* at once. An attempt that fails after this is not retried: the call fails with the drop
|
|
633
|
+
* reason `shutdown_residue`. So a drain against a dead endpoint costs one attempt for each
|
|
634
|
+
* batch, not the shutdown budget.
|
|
635
|
+
*/
|
|
636
|
+
endRetries() {
|
|
637
|
+
this._noWait = true;
|
|
638
|
+
for (const wake of [...this._waits]) wake();
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Send one batch, with the W2-1c retry policy (see the block above the class).
|
|
642
|
+
*
|
|
643
|
+
* Resolves SUCCESS when the backend accounted for the batch: `takeAck` has the counts (and
|
|
644
|
+
* the spans that a 413 split dropped, in `dropped`). Resolves FAILURE when no span was
|
|
645
|
+
* accounted for: `takeDropReason` then says why, and the batch processor drops the batch
|
|
646
|
+
* with that reason.
|
|
569
647
|
*
|
|
570
648
|
* ⚠ NO CALLER BUDGET. `flush(timeoutMs)` only stops WAITING at its deadline; it never
|
|
571
|
-
* cuts a request that is already sent.
|
|
572
|
-
* healthy, slow server look like a failure: the span used a retry, the breaker moved, and
|
|
573
|
-
* the server stored the same spans again. Only `abort()` stops the call, and only
|
|
649
|
+
* cuts a request that is already sent. Only `abort()` stops the call, and only
|
|
574
650
|
* `shutdown()` calls it.
|
|
575
651
|
*/
|
|
576
652
|
async export(spans) {
|
|
577
653
|
if (spans.length === 0) return "success" /* SUCCESS */;
|
|
578
654
|
const call = {
|
|
579
655
|
cancel: new AbortController(),
|
|
580
|
-
|
|
656
|
+
retryAfterS: null,
|
|
581
657
|
misroute: null,
|
|
582
658
|
noRetry: false,
|
|
583
659
|
status: null,
|
|
584
660
|
error: null,
|
|
585
|
-
attempts: 0
|
|
661
|
+
attempts: 0,
|
|
662
|
+
ack: null
|
|
586
663
|
};
|
|
587
664
|
this._calls.set(spans, call);
|
|
588
665
|
try {
|
|
@@ -619,83 +696,236 @@ var HttpExporter = class {
|
|
|
619
696
|
);
|
|
620
697
|
}
|
|
621
698
|
if (parts.length === 0) return "success" /* SUCCESS */;
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
699
|
+
if (this._consecutiveFailures >= this._circuitBreakerThreshold && Date.now() < this._circuitOpenUntil) {
|
|
700
|
+
this._dropReasons.set(spans, "transport_refused");
|
|
701
|
+
return "failure" /* FAILURE */;
|
|
702
|
+
}
|
|
703
|
+
const out = { delivered: [], dropped: {}, failed: [], cancelled: false };
|
|
704
|
+
const start = performance.now();
|
|
705
|
+
await this._deliver(parts, start, call, out, true);
|
|
706
|
+
if (cancelled.aborted || out.cancelled) {
|
|
707
|
+
this._acks.delete(spans);
|
|
708
|
+
return "failure" /* FAILURE */;
|
|
709
|
+
}
|
|
710
|
+
return this._finish(spans, out, call.attempts);
|
|
711
|
+
}
|
|
712
|
+
/** The request body for `parts` (the serialized spans of one part of a batch). */
|
|
713
|
+
_payload(parts) {
|
|
714
|
+
return `{"spans":[${parts.join(",")}]` + (this._projectId ? `,"projectId":${JSON.stringify(this._projectId)}` : "") + (this._environment ? `,"environment":${JSON.stringify(this._environment)}` : "") + "}";
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Send one part of a batch with the retry policy, and record its outcome in `out`.
|
|
718
|
+
* `maySplit` is true only for the whole batch: a 413 splits it in two one time. A half
|
|
719
|
+
* that gets 413 again is dropped as `too_large`.
|
|
720
|
+
*/
|
|
721
|
+
async _deliver(parts, start, call, out, maySplit) {
|
|
722
|
+
const n = parts.length;
|
|
723
|
+
if (!maySplit && this._quota.paused()) {
|
|
724
|
+
out.delivered.push({ n, ack: { accepted: 0, rejected: 0, quotaDropped: n } });
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
const result = await this._sendWithRetries(this._payload(parts), n, start, call);
|
|
728
|
+
if (result.kind === "delivered") {
|
|
729
|
+
out.delivered.push({ n, ack: result.ack });
|
|
730
|
+
} else if (result.kind === "too_large") {
|
|
731
|
+
if (maySplit && n > 1) {
|
|
732
|
+
const mid = Math.ceil(n / 2);
|
|
733
|
+
debug(`Ingest answered HTTP 413 for ${n} span(s): sending two halves of ${mid} and ${n - mid}`);
|
|
734
|
+
await this._deliver(parts.slice(0, mid), start, call, out, false);
|
|
735
|
+
if (!out.cancelled) await this._deliver(parts.slice(mid), start, call, out, false);
|
|
736
|
+
} else {
|
|
737
|
+
out.dropped.too_large = (out.dropped.too_large ?? 0) + n;
|
|
628
738
|
}
|
|
629
|
-
|
|
739
|
+
} else if (result.kind === "cancelled") {
|
|
740
|
+
out.cancelled = true;
|
|
741
|
+
} else {
|
|
742
|
+
out.failed.push({ n, reason: result.kind, answer: result.answer });
|
|
630
743
|
}
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
744
|
+
}
|
|
745
|
+
/** Send one request body until it is delivered or the policy stops. */
|
|
746
|
+
async _sendWithRetries(body, spansSent, start, call) {
|
|
747
|
+
const cancelled = call.cancel.signal;
|
|
748
|
+
let retryNumber = 0;
|
|
749
|
+
let attemptsHere = 0;
|
|
750
|
+
for (; ; ) {
|
|
751
|
+
if (cancelled.aborted) return { kind: "cancelled" };
|
|
752
|
+
call.retryAfterS = null;
|
|
634
753
|
call.misroute = null;
|
|
635
754
|
call.noRetry = false;
|
|
636
755
|
call.status = null;
|
|
637
756
|
call.error = null;
|
|
757
|
+
call.ack = null;
|
|
638
758
|
call.attempts++;
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
}
|
|
644
|
-
if (
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
759
|
+
attemptsHere++;
|
|
760
|
+
this.attempts++;
|
|
761
|
+
const result = await this._sendRequest(body, spansSent, call);
|
|
762
|
+
if (cancelled.aborted) return { kind: "cancelled" };
|
|
763
|
+
if (result === "success" /* SUCCESS */) return { kind: "delivered", ack: call.ack };
|
|
764
|
+
if (call.status === TOO_LARGE_STATUS) return { kind: "too_large" };
|
|
765
|
+
const answer = {
|
|
766
|
+
status: call.status,
|
|
767
|
+
error: call.error,
|
|
768
|
+
misroute: call.misroute,
|
|
769
|
+
zeroAck: call.noRetry,
|
|
770
|
+
sent: spansSent
|
|
771
|
+
};
|
|
772
|
+
if (!this._retryable(call)) return { kind: "transport_refused", answer };
|
|
773
|
+
if (attemptsHere === RETRY_WARN_AFTER_ATTEMPTS) this._warnOutage(answer, true);
|
|
774
|
+
if (this._noWait) return { kind: "shutdown_residue", answer };
|
|
775
|
+
if (this._maxRetries !== void 0 && attemptsHere >= this._maxRetries) {
|
|
776
|
+
return { kind: "retry_exhausted", answer };
|
|
648
777
|
}
|
|
649
|
-
|
|
650
|
-
if (
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
778
|
+
const remainingS = this._retryHorizonS - (performance.now() - start) / 1e3;
|
|
779
|
+
if (remainingS <= 0) return { kind: "retry_exhausted", answer };
|
|
780
|
+
const hint = call.retryAfterS;
|
|
781
|
+
const waitS = hint !== null && hint > 0 ? hint : backoffDelayS(retryNumber, jitter.fraction());
|
|
782
|
+
const delayS = Math.min(waitS, remainingS);
|
|
783
|
+
retryNumber++;
|
|
784
|
+
debug(
|
|
785
|
+
`Ingest attempt ${attemptsHere} failed (${answer.status ?? answer.error ?? "unknown"}); next attempt in ${delayS.toFixed(2)} s (${Math.round(remainingS)} s of the horizon left)`
|
|
786
|
+
);
|
|
787
|
+
await this._retryWait(delayS * 1e3, cancelled);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
/** True when the answer of the last attempt of `call` can change on a retry. */
|
|
791
|
+
_retryable(call) {
|
|
792
|
+
if (call.misroute !== null || call.noRetry) return false;
|
|
793
|
+
if (call.status !== null) return RETRY_STATUSES.has(call.status);
|
|
794
|
+
if (this._endpointHasCredentials) return false;
|
|
795
|
+
return call.error !== null;
|
|
796
|
+
}
|
|
797
|
+
/**
|
|
798
|
+
* Wait `ms` before the next attempt. `abort()` and `endRetries()` end the wait at once. The
|
|
799
|
+
* timer does not keep the process alive: when nothing else runs, `beforeExit` starts the
|
|
800
|
+
* SDK's `shutdown()`, which ends the wait.
|
|
801
|
+
*/
|
|
802
|
+
_retryWait(ms, signal) {
|
|
803
|
+
return new Promise((resolve) => {
|
|
804
|
+
if (signal.aborted || this._noWait || ms <= 0) return resolve();
|
|
805
|
+
const done = () => {
|
|
806
|
+
clearTimeout(timer);
|
|
807
|
+
signal.removeEventListener("abort", done);
|
|
808
|
+
this._waits.delete(done);
|
|
809
|
+
resolve();
|
|
810
|
+
};
|
|
811
|
+
const timer = setTimeout(done, Math.min(ms, MAX_TIMER_MS));
|
|
812
|
+
timer.unref?.();
|
|
813
|
+
signal.addEventListener("abort", done, { once: true });
|
|
814
|
+
this._waits.add(done);
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
/** Turn the outcome of the parts of one batch into the result of `export()`. */
|
|
818
|
+
_finish(spans, out, attempts) {
|
|
819
|
+
const reasons = new Set(out.failed.map((f) => f.reason));
|
|
820
|
+
if (out.delivered.length === 0 && Object.keys(out.dropped).length === 0 && reasons.size === 1) {
|
|
821
|
+
const reason = out.failed[0].reason;
|
|
822
|
+
const nFailed = out.failed.reduce((sum, f) => sum + f.n, 0);
|
|
823
|
+
this._dropReasons.set(spans, reason);
|
|
824
|
+
return this._fail(reason, out.failed[out.failed.length - 1].answer, nFailed, attempts);
|
|
825
|
+
}
|
|
826
|
+
let ack;
|
|
827
|
+
if (out.delivered.length === 1 && Object.keys(out.dropped).length === 0 && out.failed.length === 0) {
|
|
828
|
+
ack = out.delivered[0].ack;
|
|
829
|
+
} else {
|
|
830
|
+
let accepted = 0;
|
|
831
|
+
let rejected = 0;
|
|
832
|
+
let quotaDropped = 0;
|
|
833
|
+
for (const part of out.delivered) {
|
|
834
|
+
accepted += part.ack !== null ? part.ack.accepted : part.n;
|
|
835
|
+
rejected += part.ack !== null ? part.ack.rejected : 0;
|
|
836
|
+
quotaDropped += part.ack?.quotaDropped ?? 0;
|
|
656
837
|
}
|
|
838
|
+
const dropped = { ...out.dropped };
|
|
839
|
+
for (const f of out.failed) dropped[f.reason] = (dropped[f.reason] ?? 0) + f.n;
|
|
840
|
+
ack = { accepted, rejected };
|
|
841
|
+
if (quotaDropped > 0) ack.quotaDropped = quotaDropped;
|
|
842
|
+
if (Object.keys(dropped).length > 0) ack.dropped = dropped;
|
|
657
843
|
}
|
|
658
|
-
if (
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
844
|
+
if (ack !== null) this._acks.set(spans, ack);
|
|
845
|
+
this._consecutiveFailures = 0;
|
|
846
|
+
this._failureKind = null;
|
|
847
|
+
const tooLarge = out.dropped.too_large ?? 0;
|
|
848
|
+
if (tooLarge > 0) this._warnTooLarge(tooLarge);
|
|
849
|
+
for (const f of out.failed) {
|
|
850
|
+
if (f.reason === "retry_exhausted") this._warnExhausted(f.n, attempts, f.answer);
|
|
851
|
+
}
|
|
852
|
+
return "success" /* SUCCESS */;
|
|
853
|
+
}
|
|
854
|
+
/** End a call in which no span was accounted for. Always returns FAILURE. */
|
|
855
|
+
_fail(reason, answer, nSpans, attempts) {
|
|
856
|
+
if (reason === "shutdown_residue") {
|
|
857
|
+
debug(`Export ended by shutdown after ${attempts} attempt(s)`);
|
|
662
858
|
return "failure" /* FAILURE */;
|
|
663
859
|
}
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
this.
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
);
|
|
860
|
+
if (reason === "retry_exhausted") {
|
|
861
|
+
if (answer.status === null || !CAPACITY_REFUSAL_STATUSES.has(answer.status)) this._stepBreaker();
|
|
862
|
+
this._warnOutage(answer, true);
|
|
863
|
+
this._warnExhausted(nSpans, attempts, answer);
|
|
864
|
+
return "failure" /* FAILURE */;
|
|
670
865
|
}
|
|
866
|
+
this._stepBreaker();
|
|
867
|
+
this._warnOutage(answer, false);
|
|
868
|
+
return "failure" /* FAILURE */;
|
|
869
|
+
}
|
|
870
|
+
/** Count one failed call toward the circuit breaker. */
|
|
871
|
+
_stepBreaker() {
|
|
671
872
|
this._consecutiveFailures++;
|
|
672
|
-
if (misroute === null && !call.noRetry) {
|
|
673
|
-
const status = call.status;
|
|
674
|
-
const kind = status !== null && AUTH_REFUSAL_STATUSES.has(status) ? "auth" : status !== null ? "http" : call.error !== null ? "none" : "unknown";
|
|
675
|
-
let write = false;
|
|
676
|
-
if (this._consecutiveFailures === 1 || this._failureKind === null) {
|
|
677
|
-
if (Date.now() - this._lastFailureWarnAt > FAILURE_WARN_WINDOW_MS) {
|
|
678
|
-
this._lastFailureWarnAt = Date.now();
|
|
679
|
-
write = true;
|
|
680
|
-
}
|
|
681
|
-
this._failureKind = kind;
|
|
682
|
-
} else if (kind !== this._failureKind) {
|
|
683
|
-
this._failureKind = kind;
|
|
684
|
-
write = true;
|
|
685
|
-
}
|
|
686
|
-
if (write) this._warnOutage(kind, status, call.error);
|
|
687
|
-
}
|
|
688
873
|
if (this._consecutiveFailures >= this._circuitBreakerThreshold) {
|
|
689
874
|
this._circuitOpenUntil = Date.now() + this._circuitBreakerCooldownMs;
|
|
690
875
|
warn(
|
|
691
876
|
`HTTP exporter circuit breaker opened after ${this._consecutiveFailures} failures. Cooldown: ${this._circuitBreakerCooldownMs / 1e3}s`
|
|
692
877
|
);
|
|
693
878
|
}
|
|
694
|
-
return "failure" /* FAILURE */;
|
|
695
879
|
}
|
|
696
|
-
/**
|
|
697
|
-
|
|
880
|
+
/**
|
|
881
|
+
* The outage WARNING: at the first failure of an outage, and when its kind changes. At most
|
|
882
|
+
* one in the window for the first failure; a misroute has its own WARNING, at most one in
|
|
883
|
+
* the window. `retrying` is true for a retryable answer (written after
|
|
884
|
+
* `RETRY_WARN_AFTER_ATTEMPTS` failed attempts of one batch, or when its retries end before
|
|
885
|
+
* that), false for an answer that a retry cannot change (one attempt). The WARNING names the last HTTP status, or the error
|
|
886
|
+
* code when there was no answer. It never names the key or the response body, and names
|
|
887
|
+
* the API key as a cause only for a 401 or 403. An ack of 0 and 0 has its WARNING from
|
|
888
|
+
* `checkAck`.
|
|
889
|
+
*/
|
|
890
|
+
_warnOutage(answer, retrying) {
|
|
891
|
+
if (answer.misroute !== null) {
|
|
892
|
+
if (Date.now() - this._lastMisrouteWarnAt > MISROUTE_WARN_WINDOW_MS) {
|
|
893
|
+
this._lastMisrouteWarnAt = Date.now();
|
|
894
|
+
warn(
|
|
895
|
+
`Risicare: spans are NOT reaching ${this._safeEndpoint}. It answered POST /v1/spans with ${answer.misroute}. That is not the Risicare ingest gateway, so the endpoint is probably wrong (for example the dashboard URL instead of the ingest gateway URL). The SDK does not follow redirects, and this batch is counted as NOT delivered. Set endpoint or RISICARE_ENDPOINT to the ingest gateway URL.`
|
|
896
|
+
);
|
|
897
|
+
}
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
if (answer.zeroAck) return;
|
|
901
|
+
const status = answer.status;
|
|
902
|
+
const kind = status !== null && AUTH_REFUSAL_STATUSES.has(status) ? "auth" : status !== null ? "http" : answer.error !== null ? "none" : "unknown";
|
|
903
|
+
let write = false;
|
|
904
|
+
if (this._failureKind === null || !retrying && this._consecutiveFailures === 1) {
|
|
905
|
+
if (Date.now() - this._lastFailureWarnAt > FAILURE_WARN_WINDOW_MS) {
|
|
906
|
+
this._lastFailureWarnAt = Date.now();
|
|
907
|
+
write = true;
|
|
908
|
+
}
|
|
909
|
+
this._failureKind = kind;
|
|
910
|
+
} else if (kind !== this._failureKind) {
|
|
911
|
+
this._failureKind = kind;
|
|
912
|
+
write = true;
|
|
913
|
+
}
|
|
914
|
+
if (!write) return;
|
|
698
915
|
const where = `Risicare: spans are NOT reaching ${this._safeEndpoint}`;
|
|
916
|
+
if (retrying) {
|
|
917
|
+
let limit = `${this._retryHorizonS} s from its first attempt (retryHorizonS)`;
|
|
918
|
+
if (this._maxRetries !== void 0) limit = `${this._maxRetries} attempt(s) (maxRetries) or ${limit}`;
|
|
919
|
+
const retryText = `The SDK sends each batch again with a growing wait, for up to ${limit}, and then drops it (reason=retry_exhausted).`;
|
|
920
|
+
if (kind === "http") {
|
|
921
|
+
warn(`${where}: it answered HTTP ${status}. ${retryText} Set debug: true to see each attempt.`);
|
|
922
|
+
} else {
|
|
923
|
+
warn(
|
|
924
|
+
`${where}: no answer (${answer.error}). The endpoint cannot be reached, or it did not answer in time. ${retryText} Set debug: true to see each attempt.`
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
699
929
|
const tail = "Tracing continues, but the SDK drops the spans that it cannot send.";
|
|
700
930
|
if (kind === "auth") {
|
|
701
931
|
warn(
|
|
@@ -703,15 +933,11 @@ var HttpExporter = class {
|
|
|
703
933
|
);
|
|
704
934
|
} else if (kind === "http") {
|
|
705
935
|
warn(
|
|
706
|
-
`${where}: it answered HTTP ${status}. The server did not take the batch. Check the endpoint and the server. ${tail} Set debug: true to see each attempt.`
|
|
936
|
+
`${where}: it answered HTTP ${status}. The server did not take the batch, and a retry cannot change that answer. Check the endpoint and the server. ${tail} Set debug: true to see each attempt.`
|
|
707
937
|
);
|
|
708
938
|
} else if (kind === "none" && this._endpointHasCredentials) {
|
|
709
939
|
warn(
|
|
710
|
-
`${where}: no answer (${error}). The endpoint URL holds credentials (a user name or a password before the host), and fetch does not send a request to such a URL. Remove the credentials from the endpoint URL. ${tail}`
|
|
711
|
-
);
|
|
712
|
-
} else if (kind === "none") {
|
|
713
|
-
warn(
|
|
714
|
-
`${where}: no answer (${error}). The endpoint cannot be reached, or it did not answer in time. Check the endpoint and the network. ${tail} Set debug: true to see each attempt.`
|
|
940
|
+
`${where}: no answer (${answer.error}). The endpoint URL holds credentials (a user name or a password before the host), and fetch does not send a request to such a URL. Remove the credentials from the endpoint URL. ${tail}`
|
|
715
941
|
);
|
|
716
942
|
} else {
|
|
717
943
|
warn(
|
|
@@ -719,33 +945,38 @@ var HttpExporter = class {
|
|
|
719
945
|
);
|
|
720
946
|
}
|
|
721
947
|
}
|
|
948
|
+
/** The WARNING for spans dropped as `retry_exhausted`. At most one in the window. */
|
|
949
|
+
_warnExhausted(nSpans, attempts, answer) {
|
|
950
|
+
if (Date.now() - this._lastExhaustedWarnAt <= FAILURE_WARN_WINDOW_MS) return;
|
|
951
|
+
this._lastExhaustedWarnAt = Date.now();
|
|
952
|
+
const last = answer.status !== null ? `HTTP ${answer.status}` : `no answer (${answer.error})`;
|
|
953
|
+
const capacity = answer.status !== null && CAPACITY_REFUSAL_STATUSES.has(answer.status) ? " (over capacity)" : "";
|
|
954
|
+
const limit = this._maxRetries !== void 0 && attempts >= this._maxRetries ? `the maxRetries limit of ${this._maxRetries} attempt(s)` : `the retry horizon of ${this._retryHorizonS} s (retryHorizonS)`;
|
|
955
|
+
warn(
|
|
956
|
+
`Risicare: ${nSpans} span(s) DISCARDED (reason=retry_exhausted): ${this._safeEndpoint} did not take them in ${attempts} attempt(s), and ${limit} is used up. The last answer: ${last}${capacity}. A gateway outage that is longer than the retry horizon loses spans. To wait longer, raise retryHorizonS.`
|
|
957
|
+
);
|
|
958
|
+
}
|
|
959
|
+
/** The WARNING for spans dropped as `too_large`. One time for each exporter. */
|
|
960
|
+
_warnTooLarge(nSpans) {
|
|
961
|
+
if (this._tooLargeWarned) return;
|
|
962
|
+
this._tooLargeWarned = true;
|
|
963
|
+
warn(
|
|
964
|
+
`Risicare: ${nSpans} span(s) DISCARDED (reason=too_large): ${this._safeEndpoint} answered HTTP 413 (the request is too large) for a batch, and again for a half of it after one split. The SDK does not send these spans again. Make the span attributes smaller. This WARNING is written one time; droppedSpansByReason.too_large has the count.`
|
|
965
|
+
);
|
|
966
|
+
}
|
|
722
967
|
/** Record a MISROUTE for the retry loop and the breaker. Always returns FAILURE. */
|
|
723
968
|
_noteMisroute(call, answer) {
|
|
724
969
|
call.misroute = this._scrub(answer, 300);
|
|
725
970
|
debug(`Ingest misroute: ${call.misroute}`);
|
|
726
971
|
return "failure" /* FAILURE */;
|
|
727
972
|
}
|
|
728
|
-
/**
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
* parse.
|
|
734
|
-
*/
|
|
735
|
-
_noteRefusal(call, status, retryAfterRaw) {
|
|
736
|
-
if (!CAPACITY_REFUSAL_STATUSES.has(status)) return false;
|
|
737
|
-
let delay = REFUSAL_NO_HINT;
|
|
738
|
-
if (retryAfterRaw) {
|
|
739
|
-
const secs = Number(retryAfterRaw);
|
|
740
|
-
if (Number.isFinite(secs) && secs >= 0) {
|
|
741
|
-
delay = Math.min(secs * 1e3, RETRY_AFTER_CEILING_MS);
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
call.refusalAfterMs = delay;
|
|
745
|
-
debug(`Ingest refused for capacity: HTTP ${status} (Retry-After=${this._scrub(retryAfterRaw)})`);
|
|
746
|
-
return true;
|
|
973
|
+
/** Record the `Retry-After` wait of a retryable answer for the retry loop. */
|
|
974
|
+
_noteRetryAfter(call, status, retryAfterRaw) {
|
|
975
|
+
if (!RETRY_STATUSES.has(status)) return;
|
|
976
|
+
call.retryAfterS = parseRetryAfterS(retryAfterRaw);
|
|
977
|
+
debug(`Ingest answered HTTP ${status} (Retry-After=${this._scrub(retryAfterRaw)})`);
|
|
747
978
|
}
|
|
748
|
-
async _sendRequest(body, spansSent, call
|
|
979
|
+
async _sendRequest(body, spansSent, call) {
|
|
749
980
|
const cancelled = call.cancel.signal;
|
|
750
981
|
const timeoutMs = this._timeoutMs;
|
|
751
982
|
const url = `${this._endpoint}/v1/spans`;
|
|
@@ -813,14 +1044,15 @@ var HttpExporter = class {
|
|
|
813
1044
|
} else if (ack.result === "success" /* SUCCESS */) {
|
|
814
1045
|
this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
|
|
815
1046
|
}
|
|
816
|
-
if (ack.result === "success" /* SUCCESS */)
|
|
1047
|
+
if (ack.result === "success" /* SUCCESS */) call.ack = ack.counts;
|
|
817
1048
|
if (ack.final) call.noRetry = true;
|
|
818
1049
|
return ack.result;
|
|
819
|
-
}
|
|
1050
|
+
}
|
|
1051
|
+
this._noteRetryAfter(call, response.status, response.headers?.get?.("Retry-After") ?? null);
|
|
1052
|
+
if (response.status === 408 || response.status === 504) {
|
|
820
1053
|
debug(`HTTP export timeout: ${response.status}`);
|
|
821
1054
|
return "timeout" /* TIMEOUT */;
|
|
822
1055
|
}
|
|
823
|
-
this._noteRefusal(call, response.status, response.headers.get("Retry-After"));
|
|
824
1056
|
debug(`HTTP export failed: ${response.status}`);
|
|
825
1057
|
return "failure" /* FAILURE */;
|
|
826
1058
|
} catch (e) {
|
|
@@ -834,20 +1066,9 @@ var HttpExporter = class {
|
|
|
834
1066
|
}
|
|
835
1067
|
}
|
|
836
1068
|
shutdown() {
|
|
1069
|
+
this._noWait = false;
|
|
837
1070
|
}
|
|
838
1071
|
};
|
|
839
|
-
function sleep(ms, signal) {
|
|
840
|
-
return new Promise((resolve) => {
|
|
841
|
-
if (signal?.aborted) return resolve();
|
|
842
|
-
const done = () => {
|
|
843
|
-
clearTimeout(timer);
|
|
844
|
-
signal?.removeEventListener("abort", done);
|
|
845
|
-
resolve();
|
|
846
|
-
};
|
|
847
|
-
const timer = setTimeout(done, ms);
|
|
848
|
-
signal?.addEventListener("abort", done, { once: true });
|
|
849
|
-
});
|
|
850
|
-
}
|
|
851
1072
|
function asCount(v) {
|
|
852
1073
|
return typeof v === "number" && Number.isInteger(v) && v >= 0 ? v : void 0;
|
|
853
1074
|
}
|