@uselemma/tracing 7.14.0 → 7.15.1
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 +73 -17
- package/dist/client.d.ts +50 -19
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +220 -137
- package/dist/client.js.map +1 -1
- package/dist/coding-agent-backlog.d.ts +81 -0
- package/dist/coding-agent-backlog.d.ts.map +1 -0
- package/dist/coding-agent-backlog.js +239 -0
- package/dist/coding-agent-backlog.js.map +1 -0
- package/dist/coding-agent-delivery.d.ts +31 -0
- package/dist/coding-agent-delivery.d.ts.map +1 -0
- package/dist/coding-agent-delivery.js +97 -0
- package/dist/coding-agent-delivery.js.map +1 -0
- package/dist/coding-agent-queue.d.ts +74 -0
- package/dist/coding-agent-queue.d.ts.map +1 -0
- package/dist/coding-agent-queue.js +230 -0
- package/dist/coding-agent-queue.js.map +1 -0
- package/dist/coding-agent.d.ts +14 -0
- package/dist/coding-agent.d.ts.map +1 -1
- package/dist/coding-agent.js +52 -15
- package/dist/coding-agent.js.map +1 -1
- package/dist/debug-delivery.d.ts +2 -8
- package/dist/debug-delivery.d.ts.map +1 -1
- package/dist/debug-delivery.js +5 -24
- package/dist/debug-delivery.js.map +1 -1
- package/dist/debug-mode.d.ts +9 -0
- package/dist/debug-mode.d.ts.map +1 -1
- package/dist/debug-mode.js +24 -4
- package/dist/debug-mode.js.map +1 -1
- package/dist/delivery.d.ts +50 -7
- package/dist/delivery.d.ts.map +1 -1
- package/dist/delivery.js +117 -21
- package/dist/delivery.js.map +1 -1
- package/dist/env.d.ts +8 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +18 -0
- package/dist/env.js.map +1 -0
- package/dist/error-message.d.ts +10 -0
- package/dist/error-message.d.ts.map +1 -1
- package/dist/error-message.js +14 -0
- package/dist/error-message.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/integration-client.d.ts +20 -0
- package/dist/integration-client.d.ts.map +1 -0
- package/dist/integration-client.js +47 -0
- package/dist/integration-client.js.map +1 -0
- package/dist/langchain-context.d.ts +7 -0
- package/dist/langchain-context.d.ts.map +1 -0
- package/dist/langchain-context.js +28 -0
- package/dist/langchain-context.js.map +1 -0
- package/dist/langchain-eviction.d.ts +42 -0
- package/dist/langchain-eviction.d.ts.map +1 -0
- package/dist/langchain-eviction.js +56 -0
- package/dist/langchain-eviction.js.map +1 -0
- package/dist/langchain.d.ts +31 -13
- package/dist/langchain.d.ts.map +1 -1
- package/dist/langchain.js +74 -109
- package/dist/langchain.js.map +1 -1
- package/dist/mastra.d.ts +9 -11
- package/dist/mastra.d.ts.map +1 -1
- package/dist/mastra.js +40 -25
- package/dist/mastra.js.map +1 -1
- package/dist/openai-agents.d.ts +8 -0
- package/dist/openai-agents.d.ts.map +1 -1
- package/dist/openai-agents.js +17 -14
- package/dist/openai-agents.js.map +1 -1
- package/dist/serialize.d.ts +17 -0
- package/dist/serialize.d.ts.map +1 -0
- package/dist/serialize.js +143 -0
- package/dist/serialize.js.map +1 -0
- package/dist/stream-result.d.ts +33 -0
- package/dist/stream-result.d.ts.map +1 -0
- package/dist/stream-result.js +135 -0
- package/dist/stream-result.js.map +1 -0
- package/dist/vercel-ai-events.d.ts +308 -0
- package/dist/vercel-ai-events.d.ts.map +1 -0
- package/dist/vercel-ai-events.js +340 -0
- package/dist/vercel-ai-events.js.map +1 -0
- package/dist/vercel-ai-run.d.ts +171 -0
- package/dist/vercel-ai-run.d.ts.map +1 -0
- package/dist/vercel-ai-run.js +650 -0
- package/dist/vercel-ai-run.js.map +1 -0
- package/dist/vercel-ai.d.ts +36 -108
- package/dist/vercel-ai.d.ts.map +1 -1
- package/dist/vercel-ai.js +185 -882
- package/dist/vercel-ai.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/examples/vercel-ai-v6.ts +2 -2
- package/examples/vercel-ai-v7.ts +14 -16
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Lemma = exports.TraceHandle = exports.TraceContext = exports.NoopSpanHandle = exports.SpanHandle = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = void 0;
|
|
4
|
+
exports.startBackgroundTrace = startBackgroundTrace;
|
|
4
5
|
const debug_delivery_1 = require("./debug-delivery");
|
|
5
6
|
const debug_mode_1 = require("./debug-mode");
|
|
6
7
|
const delivery_1 = require("./delivery");
|
|
7
8
|
const dropped_traces_1 = require("./dropped-traces");
|
|
9
|
+
const env_1 = require("./env");
|
|
8
10
|
const error_message_1 = require("./error-message");
|
|
9
11
|
const release_1 = require("./release");
|
|
12
|
+
const serialize_1 = require("./serialize");
|
|
13
|
+
const stream_result_1 = require("./stream-result");
|
|
10
14
|
const usage_1 = require("./usage");
|
|
11
15
|
const version_1 = require("./version");
|
|
12
16
|
var usage_2 = require("./usage");
|
|
@@ -61,12 +65,7 @@ function serializeAttribute(value) {
|
|
|
61
65
|
typeof value === "number" ||
|
|
62
66
|
typeof value === "boolean")
|
|
63
67
|
return value;
|
|
64
|
-
|
|
65
|
-
return JSON.stringify(value);
|
|
66
|
-
}
|
|
67
|
-
catch {
|
|
68
|
-
return String(value);
|
|
69
|
-
}
|
|
68
|
+
return (0, serialize_1.stringifyTraceJson)(value);
|
|
70
69
|
}
|
|
71
70
|
function addDefined(attributes, key, value) {
|
|
72
71
|
if (value !== undefined)
|
|
@@ -172,6 +171,13 @@ function normalizeSpan(options, fallbackType) {
|
|
|
172
171
|
...(usage ? { usage } : {}),
|
|
173
172
|
};
|
|
174
173
|
}
|
|
174
|
+
/** JSON for a built payload, which is a plain object and always serializes. */
|
|
175
|
+
function payloadBody(payload) {
|
|
176
|
+
const body = (0, serialize_1.stringifyTraceJson)(payload);
|
|
177
|
+
if (body === undefined)
|
|
178
|
+
throw new Error("trace payload is empty");
|
|
179
|
+
return body;
|
|
180
|
+
}
|
|
175
181
|
function warnNoop(message) {
|
|
176
182
|
console.warn(`@uselemma/tracing: ${message}`);
|
|
177
183
|
}
|
|
@@ -230,6 +236,21 @@ class SpanHandle {
|
|
|
230
236
|
this.trace.spanEnded(this.payload);
|
|
231
237
|
this.trace.changed();
|
|
232
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Close this span as failed if it is still open. A no-op once ended, so a
|
|
241
|
+
* completed span is never rewritten. The end is never before the start.
|
|
242
|
+
*
|
|
243
|
+
* @internal
|
|
244
|
+
*/
|
|
245
|
+
failUnfinished(error, endedAt) {
|
|
246
|
+
if (this.ended)
|
|
247
|
+
return;
|
|
248
|
+
const startedMs = timestampMs(this.payload.started_at);
|
|
249
|
+
const closedAt = startedMs != null && startedMs > endedAt.getTime()
|
|
250
|
+
? new Date(startedMs)
|
|
251
|
+
: endedAt;
|
|
252
|
+
this.end({ status: "ERROR", error, endedAt: closedAt });
|
|
253
|
+
}
|
|
233
254
|
/**
|
|
234
255
|
* Fill input or output after end() when a child model call finishes later.
|
|
235
256
|
* Input is written only while this span still has none. Output is replaced
|
|
@@ -325,6 +346,8 @@ exports.SpanHandle = SpanHandle;
|
|
|
325
346
|
class NoopSpanHandle {
|
|
326
347
|
id = "";
|
|
327
348
|
end() { }
|
|
349
|
+
/** @internal */
|
|
350
|
+
failUnfinished(_error, _endedAt) { }
|
|
328
351
|
ensureEndedAt(_endedAt) { }
|
|
329
352
|
startSpan() {
|
|
330
353
|
return this;
|
|
@@ -352,8 +375,11 @@ class TraceContext {
|
|
|
352
375
|
onChange;
|
|
353
376
|
spans = [];
|
|
354
377
|
spanHandles = new Map();
|
|
355
|
-
|
|
378
|
+
/** Boxed, so an output set to undefined still counts as set. */
|
|
379
|
+
rootOutput;
|
|
356
380
|
traceError = null;
|
|
381
|
+
/** Set by {@link markPayloadFinal}; cleared by the first change after it. */
|
|
382
|
+
sent = false;
|
|
357
383
|
id;
|
|
358
384
|
constructor(options, onChange) {
|
|
359
385
|
this.options = options;
|
|
@@ -361,16 +387,25 @@ class TraceContext {
|
|
|
361
387
|
this.options.name ??= "trace";
|
|
362
388
|
this.options.id ??= crypto.randomUUID();
|
|
363
389
|
this.id = this.options.id;
|
|
364
|
-
this.
|
|
390
|
+
this.rootOutput =
|
|
391
|
+
options.output === undefined ? undefined : { value: options.output };
|
|
365
392
|
}
|
|
366
393
|
input(value) {
|
|
367
394
|
this.options.input = value;
|
|
368
395
|
this.changed();
|
|
369
396
|
}
|
|
370
397
|
output(value) {
|
|
371
|
-
this.
|
|
398
|
+
this.rootOutput = { value };
|
|
372
399
|
this.changed();
|
|
373
400
|
}
|
|
401
|
+
/**
|
|
402
|
+
* Whether the root output was set through options or `output()`.
|
|
403
|
+
*
|
|
404
|
+
* @internal
|
|
405
|
+
*/
|
|
406
|
+
get hasOutput() {
|
|
407
|
+
return this.rootOutput !== undefined;
|
|
408
|
+
}
|
|
374
409
|
threadId(value) {
|
|
375
410
|
this.options.threadId = value;
|
|
376
411
|
this.changed();
|
|
@@ -405,8 +440,25 @@ class TraceContext {
|
|
|
405
440
|
return this.spans.some((span) => span.id === id);
|
|
406
441
|
}
|
|
407
442
|
changed() {
|
|
443
|
+
if (this.sent) {
|
|
444
|
+
this.sent = false;
|
|
445
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace changed after its final payload was built; not delivered", {
|
|
446
|
+
traceId: this.id,
|
|
447
|
+
});
|
|
448
|
+
}
|
|
408
449
|
this.onChange?.();
|
|
409
450
|
}
|
|
451
|
+
/**
|
|
452
|
+
* Marks that this trace's payload won't be built again: an automatic send
|
|
453
|
+
* builds it once and never rebuilds it, and a strict send can be retried
|
|
454
|
+
* with a rebuilt payload until it succeeds. Debug mode then logs the first
|
|
455
|
+
* later change, which isn't delivered.
|
|
456
|
+
*
|
|
457
|
+
* @internal
|
|
458
|
+
*/
|
|
459
|
+
markPayloadFinal() {
|
|
460
|
+
this.sent = true;
|
|
461
|
+
}
|
|
410
462
|
setChangeHandler(onChange) {
|
|
411
463
|
this.onChange = onChange;
|
|
412
464
|
}
|
|
@@ -503,7 +555,7 @@ class TraceContext {
|
|
|
503
555
|
id: this.options.id,
|
|
504
556
|
name: this.options.name ?? "trace",
|
|
505
557
|
input: this.options.input,
|
|
506
|
-
output: this.
|
|
558
|
+
output: this.rootOutput?.value,
|
|
507
559
|
metadata: this.options.metadata,
|
|
508
560
|
thread_id: this.options.threadId,
|
|
509
561
|
user_id: this.options.userId,
|
|
@@ -520,8 +572,10 @@ class TraceContext {
|
|
|
520
572
|
exports.TraceContext = TraceContext;
|
|
521
573
|
class TraceHandle extends TraceContext {
|
|
522
574
|
sendFn;
|
|
523
|
-
|
|
524
|
-
|
|
575
|
+
/** The send `end()` started; cleared when a strict send fails. */
|
|
576
|
+
sending;
|
|
577
|
+
/** Set by the first `end()`; a retry reuses its output and root timing. */
|
|
578
|
+
rootTiming;
|
|
525
579
|
startedAt;
|
|
526
580
|
constructor(options, sendFn, startedAt) {
|
|
527
581
|
super(options);
|
|
@@ -534,38 +588,66 @@ class TraceHandle extends TraceContext {
|
|
|
534
588
|
// duplicates. Recording mutates the in-memory trace only; the single
|
|
535
589
|
// terminal send from `end()` carries the complete trace.
|
|
536
590
|
}
|
|
537
|
-
|
|
538
|
-
//
|
|
539
|
-
//
|
|
540
|
-
if (this.
|
|
541
|
-
return this.
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
591
|
+
end(outputOrOptions) {
|
|
592
|
+
// A second end() returns the first send, so a delivered trace's spans
|
|
593
|
+
// are never sent twice. Only a failed strict send lets end() send again.
|
|
594
|
+
if (this.sending)
|
|
595
|
+
return this.sending;
|
|
596
|
+
this.rootTiming ??= this.applyEnd(outputOrOptions, arguments.length > 0);
|
|
597
|
+
const { startedAt, endedAt } = this.rootTiming;
|
|
598
|
+
// Started now, not on a later tick, so a flush() called right after
|
|
599
|
+
// end() already waits for this send.
|
|
600
|
+
const sending = this.startSend(startedAt, endedAt);
|
|
601
|
+
this.sending = sending;
|
|
602
|
+
// Registered before the caller's own handler, so a caller that catches
|
|
603
|
+
// the failure can call end() again at once. It also observes the
|
|
604
|
+
// rejection, so an end() nobody awaits is not an unhandled rejection.
|
|
605
|
+
sending.catch(() => {
|
|
606
|
+
this.sending = undefined;
|
|
607
|
+
});
|
|
608
|
+
return sending;
|
|
609
|
+
}
|
|
610
|
+
/** Apply the first `end()`'s output and duration, and fix the root timing. */
|
|
611
|
+
applyEnd(outputOrOptions, hasArgument) {
|
|
612
|
+
if (!isTraceEndOptions(outputOrOptions)) {
|
|
613
|
+
if (hasArgument)
|
|
614
|
+
this.output(outputOrOptions);
|
|
615
|
+
return { startedAt: this.startedAt, endedAt: new Date() };
|
|
553
616
|
}
|
|
554
|
-
|
|
555
|
-
this.output(outputOrOptions);
|
|
617
|
+
if ("output" in outputOrOptions)
|
|
618
|
+
this.output(outputOrOptions.output);
|
|
619
|
+
if (outputOrOptions.durationMs != null) {
|
|
620
|
+
this.duration(outputOrOptions.durationMs);
|
|
556
621
|
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
await this.sendPromise;
|
|
622
|
+
return {
|
|
623
|
+
startedAt: coerceDate(outputOrOptions.startedAt) ?? this.startedAt,
|
|
624
|
+
endedAt: coerceDate(outputOrOptions.endedAt) ?? new Date(),
|
|
625
|
+
};
|
|
562
626
|
}
|
|
563
|
-
/**
|
|
627
|
+
/** A sendFn that throws synchronously rejects instead of throwing. */
|
|
628
|
+
startSend(startedAt, endedAt) {
|
|
629
|
+
try {
|
|
630
|
+
return this.sendFn(this, startedAt, endedAt);
|
|
631
|
+
}
|
|
632
|
+
catch (error) {
|
|
633
|
+
return Promise.reject(error);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/** Whether `end()` has sent this trace, or is sending it. */
|
|
564
637
|
get isEnded() {
|
|
565
|
-
return this.
|
|
638
|
+
return this.sending !== undefined;
|
|
566
639
|
}
|
|
567
640
|
}
|
|
568
641
|
exports.TraceHandle = TraceHandle;
|
|
642
|
+
/**
|
|
643
|
+
* A trace handle whose `end()` delivers with the background budget, for an
|
|
644
|
+
* integration that starts and ends its own trace after the run.
|
|
645
|
+
*
|
|
646
|
+
* @internal
|
|
647
|
+
*/
|
|
648
|
+
function startBackgroundTrace(lemma, options) {
|
|
649
|
+
return lemma.startTrace(options, delivery_1.BACKGROUND_DELIVERY);
|
|
650
|
+
}
|
|
569
651
|
class Lemma {
|
|
570
652
|
apiKey;
|
|
571
653
|
projectId;
|
|
@@ -575,23 +657,32 @@ class Lemma {
|
|
|
575
657
|
baseHeaders;
|
|
576
658
|
release;
|
|
577
659
|
traces = new Map();
|
|
660
|
+
/** Sends in flight, and callback traces held open by a returned stream. */
|
|
661
|
+
pending = new Set();
|
|
578
662
|
configLogged = false;
|
|
579
663
|
constructor(options = {}) {
|
|
580
|
-
this.apiKey = required(options.apiKey ?? process.env.LEMMA_API_KEY, "LEMMA_API_KEY");
|
|
581
|
-
this.projectId = required(options.projectId ?? process.env.LEMMA_PROJECT_ID, "LEMMA_PROJECT_ID");
|
|
664
|
+
this.apiKey = required(options.apiKey ?? (0, env_1.readEnv)(() => process.env.LEMMA_API_KEY), "LEMMA_API_KEY");
|
|
665
|
+
this.projectId = required(options.projectId ?? (0, env_1.readEnv)(() => process.env.LEMMA_PROJECT_ID), "LEMMA_PROJECT_ID");
|
|
582
666
|
this.baseUrl = (options.baseUrl ??
|
|
583
|
-
(process.env.LEMMA_BASE_URL?.trim() || debug_delivery_1.PRODUCTION_BASE_URL)).replace(/\/+$/, "");
|
|
584
|
-
|
|
667
|
+
((0, env_1.readEnv)(() => process.env.LEMMA_BASE_URL)?.trim() || debug_delivery_1.PRODUCTION_BASE_URL)).replace(/\/+$/, "");
|
|
668
|
+
// Workers' fetch throws "Illegal invocation" when it is called with a
|
|
669
|
+
// receiver other than the global object, so neither the global nor a
|
|
670
|
+
// passed-in fetch is called as a method of this client. The global is
|
|
671
|
+
// read per call, so a fetch that instrumentation installs after the
|
|
672
|
+
// client is built is the one used.
|
|
673
|
+
const customFetch = options.fetch;
|
|
674
|
+
this.fetchImpl = (input, init) => (customFetch ?? fetch)(input, init);
|
|
585
675
|
this.baseHeaders = {
|
|
586
676
|
"User-Agent": options.userAgent ?? version_1.SDK_USER_AGENT,
|
|
587
677
|
Authorization: `Bearer ${this.apiKey}`,
|
|
588
678
|
};
|
|
589
|
-
this.release = (0, release_1.normalizeRelease)(options.release ?? process.env.LEMMA_RELEASE);
|
|
679
|
+
this.release = (0, release_1.normalizeRelease)(options.release ?? (0, env_1.readEnv)(() => process.env.LEMMA_RELEASE));
|
|
590
680
|
this.logInitConfigOnce();
|
|
591
681
|
}
|
|
592
682
|
/**
|
|
593
|
-
* Run a one-call delivery diagnostic: config check
|
|
594
|
-
*
|
|
683
|
+
* Run a one-call delivery diagnostic: config check and a minimal ingest.
|
|
684
|
+
* `ok` means ingest returned 201 for the smoke trace; confirm it arrived
|
|
685
|
+
* on the project's Traces page in the Lemma dashboard.
|
|
595
686
|
*/
|
|
596
687
|
async debugSmokeTest() {
|
|
597
688
|
const configWarnings = (0, debug_delivery_1.buildConfigWarnings)(this.baseUrl, this.projectId);
|
|
@@ -613,7 +704,7 @@ class Lemma {
|
|
|
613
704
|
context.output("ok");
|
|
614
705
|
const startedAt = new Date();
|
|
615
706
|
const payload = this.stampRelease(context.toPayload(this.projectId, startedAt, new Date()));
|
|
616
|
-
const body =
|
|
707
|
+
const body = payloadBody(payload);
|
|
617
708
|
const url = `${this.baseUrl}${debug_delivery_1.INGEST_PATH}`;
|
|
618
709
|
let response;
|
|
619
710
|
try {
|
|
@@ -631,7 +722,10 @@ class Lemma {
|
|
|
631
722
|
const ingestWarnings = [];
|
|
632
723
|
if (response.status !== debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS) {
|
|
633
724
|
ingestWarnings.push(`expected status ${debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS}, got ${response.status}`);
|
|
634
|
-
|
|
725
|
+
// Best effort under its own timeout: a stalled body must neither hang
|
|
726
|
+
// the smoke test nor hide the status Lemma returned.
|
|
727
|
+
const failureBody = await (0, delivery_1.withIngestTimeout)(() => response.text()).catch(() => "");
|
|
728
|
+
const hint = (0, delivery_1.ingestFailureHint)(response.status, failureBody);
|
|
635
729
|
if (hint)
|
|
636
730
|
hints.push(hint);
|
|
637
731
|
}
|
|
@@ -655,42 +749,20 @@ class Lemma {
|
|
|
655
749
|
hints,
|
|
656
750
|
};
|
|
657
751
|
}
|
|
658
|
-
const
|
|
659
|
-
if (
|
|
660
|
-
hints.push(
|
|
661
|
-
}
|
|
662
|
-
else if (!(0, debug_delivery_1.isSuccessfulIngestStatus)(ingestStatus)) {
|
|
663
|
-
hints.push(`ingest returned 201 but ingest-status=not_found after ${debug_delivery_1.INGEST_STATUS_POLL_TIMEOUT_MS / 1000}s — may be processing lag or downstream issue`);
|
|
664
|
-
}
|
|
752
|
+
const ok = response.status === debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS;
|
|
753
|
+
if (ok)
|
|
754
|
+
hints.push((0, debug_delivery_1.smokeTestConfirmHint)(traceId));
|
|
665
755
|
return {
|
|
666
|
-
ok
|
|
667
|
-
(0, debug_delivery_1.isSuccessfulIngestStatus)(ingestStatus),
|
|
756
|
+
ok,
|
|
668
757
|
config,
|
|
669
758
|
ingest,
|
|
670
|
-
ingestStatus: ingestStatus ?? undefined,
|
|
671
759
|
hints,
|
|
672
760
|
};
|
|
673
761
|
}
|
|
674
762
|
trace(options = {}, fn) {
|
|
675
763
|
const traceOptions = typeof options === "string" ? { name: options } : options;
|
|
676
|
-
if (!fn)
|
|
677
|
-
|
|
678
|
-
try {
|
|
679
|
-
await this.flushTraceAutomatic(trace, startedAt, endedAt);
|
|
680
|
-
}
|
|
681
|
-
finally {
|
|
682
|
-
// Drop the handle once ended so long-lived clients don't retain
|
|
683
|
-
// every historical TraceHandle (and its spans/payloads) forever.
|
|
684
|
-
this.traces.delete(trace.id);
|
|
685
|
-
}
|
|
686
|
-
});
|
|
687
|
-
this.traces.set(handle.id, handle);
|
|
688
|
-
(0, debug_mode_1.lemmaDebug)("client", "trace handle created", {
|
|
689
|
-
traceId: handle.id,
|
|
690
|
-
name: traceOptions.name ?? "trace",
|
|
691
|
-
});
|
|
692
|
-
return handle;
|
|
693
|
-
}
|
|
764
|
+
if (!fn)
|
|
765
|
+
return this.startTrace(traceOptions, delivery_1.AWAITED_DELIVERY);
|
|
694
766
|
const context = new TraceContext(traceOptions);
|
|
695
767
|
const startedAt = new Date();
|
|
696
768
|
(0, debug_mode_1.lemmaDebug)("client", "trace started", {
|
|
@@ -705,19 +777,71 @@ class Lemma {
|
|
|
705
777
|
catch (error) {
|
|
706
778
|
// Only the agent's own failure belongs in this branch.
|
|
707
779
|
context.fail(error);
|
|
708
|
-
await this.flushTraceAutomatic(context, startedAt, new Date());
|
|
780
|
+
await this.flushTraceAutomatic(context, startedAt, new Date(), delivery_1.AWAITED_DELIVERY);
|
|
709
781
|
throw error;
|
|
710
782
|
}
|
|
711
|
-
|
|
783
|
+
const held = (0, stream_result_1.holdTraceForStream)(result, context, () => this.flushTraceAutomatic(context, startedAt, new Date(), delivery_1.BACKGROUND_DELIVERY));
|
|
784
|
+
if (held) {
|
|
785
|
+
// Tracked from now, so a flush() called while the stream is still
|
|
786
|
+
// being read waits for its trace.
|
|
787
|
+
this.track(held.sent);
|
|
788
|
+
return held.stream;
|
|
789
|
+
}
|
|
790
|
+
if (!context.hasOutput) {
|
|
712
791
|
context.output(result);
|
|
713
792
|
}
|
|
714
793
|
// Automatic delivery is outside the agent's try: a transport failure
|
|
715
794
|
// must not be recorded as an agent failure. Fail-open so Lemma being
|
|
716
795
|
// down cannot fail the caller's app; ingest() stays strict for retries.
|
|
717
|
-
await this.flushTraceAutomatic(context, startedAt, new Date());
|
|
796
|
+
await this.flushTraceAutomatic(context, startedAt, new Date(), delivery_1.AWAITED_DELIVERY);
|
|
718
797
|
return result;
|
|
719
798
|
})();
|
|
720
799
|
}
|
|
800
|
+
/**
|
|
801
|
+
* Wait until every trace this client has started sending is delivered or
|
|
802
|
+
* dropped, including each callback trace whose returned stream is still
|
|
803
|
+
* being read. Never rejects. A trace handle that hasn't been ended yet is
|
|
804
|
+
* not waited for.
|
|
805
|
+
*
|
|
806
|
+
* Pass it to the runtime so delivery outlives the response:
|
|
807
|
+
* `ctx.waitUntil(lemma.flush())` on Cloudflare Workers,
|
|
808
|
+
* `after(() => lemma.flush())` in Next.js. On AWS Lambda, await it before
|
|
809
|
+
* the handler returns.
|
|
810
|
+
*/
|
|
811
|
+
async flush() {
|
|
812
|
+
await Promise.allSettled(Array.from(this.pending));
|
|
813
|
+
}
|
|
814
|
+
track(promise) {
|
|
815
|
+
this.pending.add(promise);
|
|
816
|
+
const forget = () => this.pending.delete(promise);
|
|
817
|
+
promise.then(forget, forget);
|
|
818
|
+
return promise;
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* A trace handle whose `end()` delivers within `budget`. A caller's
|
|
822
|
+
* `lemma.trace()` handle uses `AWAITED_DELIVERY`; `startBackgroundTrace()`
|
|
823
|
+
* uses `BACKGROUND_DELIVERY`.
|
|
824
|
+
*
|
|
825
|
+
* @internal
|
|
826
|
+
*/
|
|
827
|
+
startTrace(options, budget) {
|
|
828
|
+
const handle = new TraceHandle(options, async (trace, startedAt, endedAt) => {
|
|
829
|
+
try {
|
|
830
|
+
await this.flushTraceAutomatic(trace, startedAt, endedAt, budget);
|
|
831
|
+
}
|
|
832
|
+
finally {
|
|
833
|
+
// Drop the handle once ended so long-lived clients don't retain
|
|
834
|
+
// every historical TraceHandle (and its spans/payloads) forever.
|
|
835
|
+
this.traces.delete(trace.id);
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
this.traces.set(handle.id, handle);
|
|
839
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace handle created", {
|
|
840
|
+
traceId: handle.id,
|
|
841
|
+
name: options.name ?? "trace",
|
|
842
|
+
});
|
|
843
|
+
return handle;
|
|
844
|
+
}
|
|
721
845
|
/**
|
|
722
846
|
* Deliver a trace you assembled yourself, in a single request.
|
|
723
847
|
*
|
|
@@ -733,13 +857,15 @@ class Lemma {
|
|
|
733
857
|
* fields do not preserve prior values, and after Lemma processes the trace
|
|
734
858
|
* once, a later re-delivery does not re-run issue extraction (late new span
|
|
735
859
|
* IDs may still append for display). Retries of the same complete payload are
|
|
736
|
-
* safe: already-stored span IDs are skipped. Throws
|
|
737
|
-
* never mutates the trace's status, so a failed send
|
|
860
|
+
* safe: already-stored span IDs are skipped. Throws an `IngestError` on
|
|
861
|
+
* a non-2xx response, and never mutates the trace's status, so a failed send
|
|
862
|
+
* can be retried as-is.
|
|
738
863
|
*/
|
|
739
864
|
async ingest(context, options) {
|
|
740
|
-
const failure = await this.sendTrace(context, options.startedAt, options.endedAt ?? new Date(), delivery_1.
|
|
865
|
+
const failure = await this.sendTrace(context, options.startedAt, options.endedAt ?? new Date(), delivery_1.STRICT_DELIVERY);
|
|
741
866
|
if (failure)
|
|
742
867
|
throw failure.error;
|
|
868
|
+
context.markPayloadFinal();
|
|
743
869
|
}
|
|
744
870
|
recordSpan(options) {
|
|
745
871
|
const context = this.detachedTraceFor(options.traceId, "span");
|
|
@@ -876,56 +1002,6 @@ class Lemma {
|
|
|
876
1002
|
...(warnings.length ? { warnings } : {}),
|
|
877
1003
|
});
|
|
878
1004
|
}
|
|
879
|
-
async fetchIngestStatus(otelTraceId) {
|
|
880
|
-
const url = `${this.baseUrl}${debug_delivery_1.INGEST_STATUS_PATH}` +
|
|
881
|
-
`?project_id=${encodeURIComponent(this.projectId)}` +
|
|
882
|
-
`&otel_trace_id=${encodeURIComponent(otelTraceId)}`;
|
|
883
|
-
try {
|
|
884
|
-
return await (0, delivery_1.withIngestTimeout)(async (signal) => {
|
|
885
|
-
const response = await this.fetchImpl(url, {
|
|
886
|
-
headers: { ...this.baseHeaders },
|
|
887
|
-
signal,
|
|
888
|
-
});
|
|
889
|
-
if (!response.ok)
|
|
890
|
-
return null;
|
|
891
|
-
const data = (await response.json());
|
|
892
|
-
return (0, debug_delivery_1.parseIngestStatus)(data.status);
|
|
893
|
-
});
|
|
894
|
-
}
|
|
895
|
-
catch {
|
|
896
|
-
return null;
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
/**
|
|
900
|
-
* Poll ingest-status immediately, then every 1s until success, hard failure,
|
|
901
|
-
* or the 15s timeout.
|
|
902
|
-
*/
|
|
903
|
-
async pollIngestStatus(otelTraceId) {
|
|
904
|
-
const deadline = Date.now() + debug_delivery_1.INGEST_STATUS_POLL_TIMEOUT_MS;
|
|
905
|
-
for (;;) {
|
|
906
|
-
const status = await this.fetchIngestStatus(otelTraceId);
|
|
907
|
-
if (status === null)
|
|
908
|
-
return null;
|
|
909
|
-
if ((0, debug_delivery_1.isSuccessfulIngestStatus)(status))
|
|
910
|
-
return status;
|
|
911
|
-
const remaining = deadline - Date.now();
|
|
912
|
-
if (remaining <= 0)
|
|
913
|
-
return status;
|
|
914
|
-
await (0, delivery_1.sleep)(Math.min(debug_delivery_1.INGEST_STATUS_POLL_INTERVAL_MS, remaining));
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
async verifyIngestDelivery(otelTraceId) {
|
|
918
|
-
const result = await this.pollIngestStatus(otelTraceId);
|
|
919
|
-
if (result === null) {
|
|
920
|
-
(0, debug_mode_1.lemmaDebug)("verify", "ingest accepted (201), ingest-status check failed (status/network)");
|
|
921
|
-
return;
|
|
922
|
-
}
|
|
923
|
-
if ((0, debug_delivery_1.isSuccessfulIngestStatus)(result)) {
|
|
924
|
-
(0, debug_mode_1.lemmaDebug)("verify", `ingest accepted (201), trace ${result === "enqueued" ? "enqueued" : `visible (${result})`} (status=${result})`);
|
|
925
|
-
return;
|
|
926
|
-
}
|
|
927
|
-
(0, debug_mode_1.lemmaDebug)("verify", `ingest accepted (201), ingest-status=not_found after ${debug_delivery_1.INGEST_STATUS_POLL_TIMEOUT_MS / 1000}s — may be processing lag or downstream issue`);
|
|
928
|
-
}
|
|
929
1005
|
stampRelease(payload) {
|
|
930
1006
|
if (this.release)
|
|
931
1007
|
payload.trace.release = this.release;
|
|
@@ -937,21 +1013,28 @@ class Lemma {
|
|
|
937
1013
|
* {@link Lemma.ingest} sends once and throws, because queue/plugin producers
|
|
938
1014
|
* keep their own retry backlog.
|
|
939
1015
|
*/
|
|
940
|
-
async flushTraceAutomatic(context, startedAt, endedAt) {
|
|
941
|
-
const
|
|
1016
|
+
async flushTraceAutomatic(context, startedAt, endedAt, budget) {
|
|
1017
|
+
const sending = this.sendTrace(context, startedAt, endedAt, budget);
|
|
1018
|
+
// Built by now, and never resent, so a later change isn't delivered.
|
|
1019
|
+
context.markPayloadFinal();
|
|
1020
|
+
const failure = await sending;
|
|
942
1021
|
if (failure)
|
|
943
1022
|
(0, dropped_traces_1.reportDroppedTrace)(context.id, failure.cause);
|
|
944
1023
|
}
|
|
1024
|
+
/** {@link Lemma.buildAndSend}, tracked so `flush()` waits for it. */
|
|
1025
|
+
sendTrace(context, startedAt, endedAt, budget) {
|
|
1026
|
+
return this.track(this.buildAndSend(context, startedAt, endedAt, budget));
|
|
1027
|
+
}
|
|
945
1028
|
/**
|
|
946
1029
|
* Build, send, and check one trace; undefined when it was delivered. A
|
|
947
1030
|
* payload that cannot be built comes back as a failure, so strict and
|
|
948
1031
|
* automatic delivery handle it once.
|
|
949
1032
|
*/
|
|
950
|
-
async
|
|
1033
|
+
async buildAndSend(context, startedAt, endedAt, budget) {
|
|
951
1034
|
const built = this.buildIngestBody(context, startedAt, endedAt);
|
|
952
1035
|
if (!built.ok)
|
|
953
1036
|
return built;
|
|
954
|
-
const result = await (0, delivery_1.deliver)((signal) => this.postIngest(built.body, signal), { traceId: context.id, projectId: this.projectId },
|
|
1037
|
+
const result = await (0, delivery_1.deliver)((signal) => this.postIngest(built.body, signal), { traceId: context.id, projectId: this.projectId }, budget);
|
|
955
1038
|
if (!result.ok)
|
|
956
1039
|
return result;
|
|
957
1040
|
// Debug-only checks; the trace is already stored, so a failure here
|
|
@@ -971,7 +1054,7 @@ class Lemma {
|
|
|
971
1054
|
buildIngestBody(context, startedAt, endedAt) {
|
|
972
1055
|
try {
|
|
973
1056
|
const payload = this.stampRelease(context.toPayload(this.projectId, startedAt, endedAt));
|
|
974
|
-
const body =
|
|
1057
|
+
const body = payloadBody(payload);
|
|
975
1058
|
(0, debug_mode_1.lemmaDebug)("client", "sending trace", {
|
|
976
1059
|
traceId: payload.trace.id,
|
|
977
1060
|
name: payload.trace.name,
|
|
@@ -1013,8 +1096,8 @@ class Lemma {
|
|
|
1013
1096
|
...responseHeaders,
|
|
1014
1097
|
...(sentWarnings.length ? { warnings: sentWarnings } : {}),
|
|
1015
1098
|
});
|
|
1016
|
-
if ((0, debug_mode_1.isDebugModeEnabled)() && (0, debug_mode_1.isDebugVerifyEnabled)()
|
|
1017
|
-
|
|
1099
|
+
if ((0, debug_mode_1.isDebugModeEnabled)() && (0, debug_mode_1.isDebugVerifyEnabled)()) {
|
|
1100
|
+
(0, debug_mode_1.noteDebugVerifyRemovedOnce)();
|
|
1018
1101
|
}
|
|
1019
1102
|
}
|
|
1020
1103
|
}
|