@mastra/observability 1.21.1-alpha.2 → 1.22.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/dist/exporters/event-buffer.d.ts +5 -0
- package/dist/exporters/event-buffer.d.ts.map +1 -1
- package/dist/exporters/mastra-platform.d.ts.map +1 -1
- package/dist/exporters/mastra-storage.d.ts +14 -1
- package/dist/exporters/mastra-storage.d.ts.map +1 -1
- package/dist/exporters/span-previews.d.ts +4 -0
- package/dist/exporters/span-previews.d.ts.map +1 -0
- package/dist/index.cjs +136 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +136 -16
- package/dist/index.js.map +1 -1
- package/dist/instances/base.d.ts.map +1 -1
- package/dist/metrics/pricing-data.json +979 -503
- package/dist/model-tracing.d.ts +5 -0
- package/dist/model-tracing.d.ts.map +1 -1
- package/dist/spans/base.d.ts +5 -0
- package/dist/spans/base.d.ts.map +1 -1
- package/package.json +6 -6
|
@@ -49,12 +49,17 @@ export declare class EventBuffer {
|
|
|
49
49
|
* Builds a unique span key for tracking
|
|
50
50
|
*/
|
|
51
51
|
private buildSpanKey;
|
|
52
|
+
private exportedSpanKey;
|
|
52
53
|
/** Track successfully created spans so updates can verify span existence before flushing. */
|
|
53
54
|
addCreatedSpans(args: {
|
|
54
55
|
records: CreateSpanRecord[];
|
|
55
56
|
}): void;
|
|
57
|
+
/** Stop waiting on creates that were dropped, so their updates aren't held back for them. */
|
|
58
|
+
forgetDroppedCreates(events: BufferedEvent[]): void;
|
|
56
59
|
/** Check whether a span's create record has already been flushed to storage. */
|
|
57
60
|
spanExists(span: AnyExportedSpan): boolean;
|
|
61
|
+
/** Check whether this buffer saw the span's SPAN_STARTED but its create hasn't been confirmed yet. */
|
|
62
|
+
isAwaitingCreate(span: AnyExportedSpan): boolean;
|
|
58
63
|
/** Remove completed spans from tracking after their SPAN_ENDED updates are flushed. */
|
|
59
64
|
endFinishedSpans(args: {
|
|
60
65
|
records: UpdateSpanPartial[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-buffer.d.ts","sourceRoot":"","sources":["../../src/exporters/event-buffer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AACtF,OAAO,KAAK,EAAE,gBAAgB,EAAE,4BAA4B,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE7G,oFAAoF;AACpF,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,+DAA+D;AAC/D,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;CACpC;AAED,2EAA2E;AAC3E,MAAM,MAAM,aAAa,GAAG,kBAAkB,GAAG,UAAU,CAAC;AAE5D;;;;GAIG;AACH,qBAAa,WAAW;;
|
|
1
|
+
{"version":3,"file":"event-buffer.d.ts","sourceRoot":"","sources":["../../src/exporters/event-buffer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AACtF,OAAO,KAAK,EAAE,gBAAgB,EAAE,4BAA4B,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE7G,oFAAoF;AACpF,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,+DAA+D;AAC/D,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;CACpC;AAED,2EAA2E;AAC3E,MAAM,MAAM,aAAa,GAAG,kBAAkB,GAAG,UAAU,CAAC;AAE5D;;;;GAIG;AACH,qBAAa,WAAW;;IAWtB,YAAY,IAAI,EAAE;QAA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1
|
-
{"version":3,"file":"mastra-platform.d.ts","sourceRoot":"","sources":["../../src/exporters/mastra-platform.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,YAAY,EAEZ,QAAQ,EACR,WAAW,EACX,UAAU,EACV,aAAa,EACd,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"mastra-platform.d.ts","sourceRoot":"","sources":["../../src/exporters/mastra-platform.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,YAAY,EAEZ,QAAQ,EACR,WAAW,EACX,UAAU,EACV,aAAa,EACd,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AAGjD,MAAM,WAAW,4BAA6B,SAAQ,kBAAkB;IACtE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;;;OAQG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAyBD,eAAO,MAAM,iCAAiC,wCAAwC,CAAC;AACvF,eAAO,MAAM,sBAAsB,mBAAmB,CAAC;AA6MvD,qBAAa,sBAAuB,SAAQ,YAAY;IACtD,IA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B,OAAO,CAAC,kBAAkB;IAkB1B;;;;;OAKG;YACW,gBAAgB;YAoDhB,gBAAgB;IAmC9B,OAAO,CAAC,WAAW;IAUnB;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAyB3B;IAEK,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAqC9B;CACF"}
|
|
@@ -52,10 +52,23 @@ export declare class MastraStorageExporter extends BaseExporter {
|
|
|
52
52
|
*/
|
|
53
53
|
private flushCreates;
|
|
54
54
|
/**
|
|
55
|
-
* Flush span update/end events
|
|
55
|
+
* Flush span update/end events.
|
|
56
|
+
*
|
|
57
|
+
* When the store supports conditional updates, only spans this exporter started
|
|
58
|
+
* and hasn't confirmed the create for are deferred. Everything else, including
|
|
59
|
+
* spans another exporter instance started (such as a durable agent ending spans
|
|
60
|
+
* it rebuilt after a crash), goes straight to storage, and updates that didn't
|
|
61
|
+
* apply are counted as drops. Without conditional updates, updates for spans this
|
|
62
|
+
* exporter hasn't created are deferred until their create is flushed.
|
|
63
|
+
*
|
|
56
64
|
* When `isEnd` is true, successfully flushed spans are removed from tracking.
|
|
57
65
|
*/
|
|
58
66
|
private flushSpanUpdates;
|
|
67
|
+
/**
|
|
68
|
+
* Reports conditional span updates that storage didn't apply: the span wasn't
|
|
69
|
+
* stored, or it had already ended. Ended spans are immutable, so these are dropped.
|
|
70
|
+
*/
|
|
71
|
+
private emitNotApplied;
|
|
59
72
|
/**
|
|
60
73
|
* Runs flushBuffer() and tracks its promise so flush() can wait for writes
|
|
61
74
|
* that a timer or size trigger started on their own.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mastra-storage.d.ts","sourceRoot":"","sources":["../../src/exporters/mastra-storage.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACX,QAAQ,EACR,UAAU,EACV,aAAa,EAId,MAAM,4BAA4B,CAAC;AAEpC,OAAO,KAAK,EAAw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BzB;;;;OAIG;YACW,kBAAkB;IAQhC,OAAO,CAAC,iBAAiB;IAgBzB,OAAO,CAAC,QAAQ;IAsBhB;;;;OAIG;YACW,YAAY;IAmC1B;;;;;;;;;;;OAWG;YACW,gBAAgB;IA4E9B;;;OAGG;IACH,OAAO,CAAC,cAAc;IAuBtB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAMxB;;;;;;;OAOG;YACW,WAAW;IA+HnB,mBAAmB,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAS5D;IAED;;OAEG;IACG,aAAa,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAMrD;IAED;;OAEG;IACG,UAAU,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAM/C;IAED;;OAEG;IACG,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAMnD;IAED;;OAEG;IACG,eAAe,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAMzD;IAED;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CA2B3B;IAEK,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAgB9B;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"span-previews.d.ts","sourceRoot":"","sources":["../../src/exporters/span-previews.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAKlE,0EAA0E;AAC1E,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,eAAe,GAAG,eAAe,CAUvE"}
|
package/dist/index.cjs
CHANGED
|
@@ -24,6 +24,7 @@ let _mastra_core_base = require("@mastra/core/base");
|
|
|
24
24
|
let _mastra_core_error = require("@mastra/core/error");
|
|
25
25
|
let _mastra_core_logger = require("@mastra/core/logger");
|
|
26
26
|
let _mastra_core_observability = require("@mastra/core/observability");
|
|
27
|
+
_mastra_core_observability = __toESM(_mastra_core_observability, 1);
|
|
27
28
|
let _mastra_core_utils = require("@mastra/core/utils");
|
|
28
29
|
let zod_v4 = require("zod/v4");
|
|
29
30
|
let fs = require("fs");
|
|
@@ -2737,8 +2738,20 @@ var ModelSpanTracker = class {
|
|
|
2737
2738
|
* feature flag, or when called without an active step span. Auto-invoked from
|
|
2738
2739
|
* chunk handlers as a safety net; explicit callers get the most accurate
|
|
2739
2740
|
* start time.
|
|
2741
|
+
*
|
|
2742
|
+
* `payload.modelId` / `payload.provider` name the model the caller is about
|
|
2743
|
+
* to invoke and are recorded on MODEL_STEP, also when a fallback attempt
|
|
2744
|
+
* reuses the step's open inference span. The safety net never records them:
|
|
2745
|
+
* a replayed step-start chunk names the model of the original call.
|
|
2740
2746
|
*/
|
|
2741
2747
|
startInference(payload) {
|
|
2748
|
+
if (payload?.modelId && payload.provider) this.#currentStepSpan?.update({ attributes: {
|
|
2749
|
+
model: payload.modelId,
|
|
2750
|
+
provider: payload.provider
|
|
2751
|
+
} });
|
|
2752
|
+
this.#openInference(payload);
|
|
2753
|
+
}
|
|
2754
|
+
#openInference(payload) {
|
|
2742
2755
|
if (!supportsModelInference()) return;
|
|
2743
2756
|
if (!this.#currentStepSpan || this.#currentInferenceSpan) return;
|
|
2744
2757
|
this.#inferenceCompletionStartTime = void 0;
|
|
@@ -2863,7 +2876,7 @@ var ModelSpanTracker = class {
|
|
|
2863
2876
|
*/
|
|
2864
2877
|
#ensureStepAndInference() {
|
|
2865
2878
|
if (!this.#currentStepSpan) this.startStep();
|
|
2866
|
-
if (!this.#currentInferenceSpan) this
|
|
2879
|
+
if (!this.#currentInferenceSpan) this.#openInference();
|
|
2867
2880
|
}
|
|
2868
2881
|
/**
|
|
2869
2882
|
* Create a new chunk span (for multi-part chunks like text-start/delta/end)
|
|
@@ -3063,7 +3076,7 @@ var ModelSpanTracker = class {
|
|
|
3063
3076
|
this.#inModelResponse = true;
|
|
3064
3077
|
if (this.#currentStepSpan) this.updateStep(chunk.payload);
|
|
3065
3078
|
else this.startStep(chunk.payload);
|
|
3066
|
-
if (!this.#currentInferenceSpan) this
|
|
3079
|
+
if (!this.#currentInferenceSpan) this.#openInference(chunk.payload);
|
|
3067
3080
|
break;
|
|
3068
3081
|
case "step-finish":
|
|
3069
3082
|
this.#inModelResponse = false;
|
|
@@ -3269,6 +3282,19 @@ function getExternalParentId(options) {
|
|
|
3269
3282
|
if (parent.isInternal || parent.isExcluded) return parent.getParentSpanId(false);
|
|
3270
3283
|
return parent.id;
|
|
3271
3284
|
}
|
|
3285
|
+
/**
|
|
3286
|
+
* Vercel AI Gateway reports which provider served a step, and any provider
|
|
3287
|
+
* attempts that failed first, only in `providerMetadata.gateway.routing`.
|
|
3288
|
+
* Read it defensively so a throwing getter never skips the strip below.
|
|
3289
|
+
*/
|
|
3290
|
+
function readGatewayRouting(providerMetadata) {
|
|
3291
|
+
try {
|
|
3292
|
+
const gateway = isPlainRecord(providerMetadata) ? providerMetadata.gateway : void 0;
|
|
3293
|
+
return isPlainRecord(gateway) ? gateway.routing : void 0;
|
|
3294
|
+
} catch {
|
|
3295
|
+
return;
|
|
3296
|
+
}
|
|
3297
|
+
}
|
|
3272
3298
|
var BaseSpan = class BaseSpan {
|
|
3273
3299
|
name;
|
|
3274
3300
|
type;
|
|
@@ -3298,6 +3324,11 @@ var BaseSpan = class BaseSpan {
|
|
|
3298
3324
|
parentSpanId;
|
|
3299
3325
|
/** Parent from an external tracing system (ambient OTel / dd-trace span) that Mastra did not create; carried for external correlation, not part of Mastra's own parent/child linkage */
|
|
3300
3326
|
externalParentSpanId;
|
|
3327
|
+
/** Native span fields (see `SpanData`); carried as supplied and exported only when set */
|
|
3328
|
+
resumedFromSpanId;
|
|
3329
|
+
outcome;
|
|
3330
|
+
stopReason;
|
|
3331
|
+
stopReasonContext;
|
|
3301
3332
|
/** Deep clean options for serialization */
|
|
3302
3333
|
deepCleanOptions;
|
|
3303
3334
|
/**
|
|
@@ -3364,6 +3395,10 @@ var BaseSpan = class BaseSpan {
|
|
|
3364
3395
|
}
|
|
3365
3396
|
this.tags = !options.parent && options.tags?.length ? options.tags : void 0;
|
|
3366
3397
|
this.links = validLinks(options.links);
|
|
3398
|
+
this.resumedFromSpanId = options.resumedFromSpanId;
|
|
3399
|
+
this.outcome = options.outcome;
|
|
3400
|
+
this.stopReason = options.stopReason;
|
|
3401
|
+
this.stopReasonContext = deepClean(options.stopReasonContext, this.deepCleanOptions);
|
|
3367
3402
|
const entityParent = this.getParentSpan(false);
|
|
3368
3403
|
this.entityType = options.entityType ?? entityParent?.entityType;
|
|
3369
3404
|
this.entityId = options.entityId ?? entityParent?.entityId;
|
|
@@ -3396,8 +3431,10 @@ var BaseSpan = class BaseSpan {
|
|
|
3396
3431
|
if (this.type !== _mastra_core_observability.SpanType.MODEL_STEP) return value;
|
|
3397
3432
|
try {
|
|
3398
3433
|
const prepared = { ...value };
|
|
3434
|
+
const gatewayRouting = readGatewayRouting(prepared.providerMetadata);
|
|
3399
3435
|
delete prepared.providerMetadata;
|
|
3400
3436
|
delete prepared.experimental_providerMetadata;
|
|
3437
|
+
if (gatewayRouting !== void 0) prepared.providerMetadata = { gateway: { routing: gatewayRouting } };
|
|
3401
3438
|
return prepared;
|
|
3402
3439
|
} catch {
|
|
3403
3440
|
return value;
|
|
@@ -3630,6 +3667,10 @@ var BaseSpan = class BaseSpan {
|
|
|
3630
3667
|
...definedLineage(correlation),
|
|
3631
3668
|
parentSpanId: this.getParentSpanId(includeInternalSpans),
|
|
3632
3669
|
externalParentSpanId: this.getExportedExternalParentSpanId(includeInternalSpans),
|
|
3670
|
+
...this.resumedFromSpanId !== void 0 ? { resumedFromSpanId: this.resumedFromSpanId } : {},
|
|
3671
|
+
...this.outcome !== void 0 ? { outcome: this.outcome } : {},
|
|
3672
|
+
...this.stopReason !== void 0 ? { stopReason: this.stopReason } : {},
|
|
3673
|
+
...this.stopReasonContext !== void 0 ? { stopReasonContext: this.stopReasonContext } : {},
|
|
3633
3674
|
...nestedUnderParent ? { nestedUnderParent: true } : {},
|
|
3634
3675
|
...links ? { links } : {},
|
|
3635
3676
|
...this.isRootSpan && !nestedUnderParent && this.tags?.length ? { tags: this.tags } : {}
|
|
@@ -3858,7 +3899,7 @@ function isValidSpanId(spanId) {
|
|
|
3858
3899
|
function getOrCreateTraceId(options) {
|
|
3859
3900
|
if (options.traceId) {
|
|
3860
3901
|
if (isValidTraceId(options.traceId)) return options.traceId;
|
|
3861
|
-
|
|
3902
|
+
console.error(`[Mastra Tracing] Invalid traceId: must be 1-32 hexadecimal characters, got "${options.traceId}". Generating new trace ID.`);
|
|
3862
3903
|
}
|
|
3863
3904
|
return generateTraceId();
|
|
3864
3905
|
}
|
|
@@ -4054,6 +4095,8 @@ var EventBuffer = class {
|
|
|
4054
4095
|
#creates = [];
|
|
4055
4096
|
#updates = [];
|
|
4056
4097
|
#allCreatedSpans = /* @__PURE__ */ new Set();
|
|
4098
|
+
/** Spans whose SPAN_STARTED this buffer has seen but whose create isn't confirmed yet */
|
|
4099
|
+
#pendingCreateSpans = /* @__PURE__ */ new Set();
|
|
4057
4100
|
#firstEventTime;
|
|
4058
4101
|
#storageStrategy;
|
|
4059
4102
|
#maxRetries;
|
|
@@ -4104,8 +4147,11 @@ var EventBuffer = class {
|
|
|
4104
4147
|
case _mastra_core_observability.TracingEventType.SPAN_STARTED:
|
|
4105
4148
|
switch (this.#storageStrategy) {
|
|
4106
4149
|
case "realtime":
|
|
4107
|
-
case "
|
|
4108
|
-
|
|
4150
|
+
case "batch-with-updates":
|
|
4151
|
+
if (!event.exportedSpan.isEvent) this.#pendingCreateSpans.add(this.exportedSpanKey(event.exportedSpan));
|
|
4152
|
+
this.pushCreate(event);
|
|
4153
|
+
break;
|
|
4154
|
+
case "event-sourced": this.pushCreate(event);
|
|
4109
4155
|
}
|
|
4110
4156
|
break;
|
|
4111
4157
|
case _mastra_core_observability.TracingEventType.SPAN_UPDATED:
|
|
@@ -4174,23 +4220,40 @@ var EventBuffer = class {
|
|
|
4174
4220
|
buildSpanKey(span) {
|
|
4175
4221
|
return `${span.traceId}:${span.spanId}`;
|
|
4176
4222
|
}
|
|
4223
|
+
exportedSpanKey(span) {
|
|
4224
|
+
return this.buildSpanKey({
|
|
4225
|
+
traceId: span.traceId,
|
|
4226
|
+
spanId: span.id
|
|
4227
|
+
});
|
|
4228
|
+
}
|
|
4177
4229
|
/** Track successfully created spans so updates can verify span existence before flushing. */
|
|
4178
4230
|
addCreatedSpans(args) {
|
|
4179
4231
|
if (this.#storageStrategy === "event-sourced" || this.#storageStrategy === "insert-only") return;
|
|
4180
|
-
for (const createRecord of args.records) if (!createRecord.isEvent)
|
|
4232
|
+
for (const createRecord of args.records) if (!createRecord.isEvent) {
|
|
4233
|
+
const key = this.buildSpanKey(createRecord);
|
|
4234
|
+
this.#pendingCreateSpans.delete(key);
|
|
4235
|
+
this.#allCreatedSpans.add(key);
|
|
4236
|
+
}
|
|
4237
|
+
}
|
|
4238
|
+
/** Stop waiting on creates that were dropped, so their updates aren't held back for them. */
|
|
4239
|
+
forgetDroppedCreates(events) {
|
|
4240
|
+
for (const event of events) if (event.type === _mastra_core_observability.TracingEventType.SPAN_STARTED) this.#pendingCreateSpans.delete(this.exportedSpanKey(event.exportedSpan));
|
|
4181
4241
|
}
|
|
4182
4242
|
/** Check whether a span's create record has already been flushed to storage. */
|
|
4183
4243
|
spanExists(span) {
|
|
4184
|
-
return this.#allCreatedSpans
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4244
|
+
return this.#allCreatedSpans.has(this.exportedSpanKey(span));
|
|
4245
|
+
}
|
|
4246
|
+
/** Check whether this buffer saw the span's SPAN_STARTED but its create hasn't been confirmed yet. */
|
|
4247
|
+
isAwaitingCreate(span) {
|
|
4248
|
+
return this.#pendingCreateSpans.has(this.exportedSpanKey(span));
|
|
4188
4249
|
}
|
|
4189
4250
|
/** Remove completed spans from tracking after their SPAN_ENDED updates are flushed. */
|
|
4190
4251
|
endFinishedSpans(args) {
|
|
4191
4252
|
if (this.#storageStrategy === "event-sourced" || this.#storageStrategy === "insert-only") return;
|
|
4192
4253
|
args.records.forEach((r) => {
|
|
4193
|
-
this
|
|
4254
|
+
const key = this.buildSpanKey(r);
|
|
4255
|
+
this.#allCreatedSpans.delete(key);
|
|
4256
|
+
this.#pendingCreateSpans.delete(key);
|
|
4194
4257
|
});
|
|
4195
4258
|
}
|
|
4196
4259
|
};
|
|
@@ -4632,6 +4695,21 @@ var DefaultExporter = class extends BaseExporter {
|
|
|
4632
4695
|
this.logger.info("DefaultExporter shutdown complete");
|
|
4633
4696
|
}
|
|
4634
4697
|
};
|
|
4698
|
+
// src/exporters/span-previews.ts
|
|
4699
|
+
const buildSpanPreviews = _mastra_core_observability.buildSpanPreviews;
|
|
4700
|
+
/** Older core versions retain their existing storage preview fallback. */
|
|
4701
|
+
function withSpanPreviews(span) {
|
|
4702
|
+
if (!_mastra_core_features.coreFeatures.has("observability-span-previews")) return span;
|
|
4703
|
+
try {
|
|
4704
|
+
const previews = buildSpanPreviews?.(span);
|
|
4705
|
+
return previews ? {
|
|
4706
|
+
...span,
|
|
4707
|
+
...previews
|
|
4708
|
+
} : span;
|
|
4709
|
+
} catch {
|
|
4710
|
+
return span;
|
|
4711
|
+
}
|
|
4712
|
+
}
|
|
4635
4713
|
// src/exporters/mastra-storage.ts
|
|
4636
4714
|
/**
|
|
4637
4715
|
* Resolves the final tracing storage strategy based on config and observability store hints
|
|
@@ -4835,6 +4913,7 @@ var MastraStorageExporter = class extends BaseExporter {
|
|
|
4835
4913
|
return { failed: true };
|
|
4836
4914
|
}
|
|
4837
4915
|
const dropped = this.#eventBuffer.reAddCreates(events);
|
|
4916
|
+
this.#eventBuffer.forgetDroppedCreates(dropped);
|
|
4838
4917
|
const retryAttempt = this.retryAttempt(events);
|
|
4839
4918
|
this.logStorageFailure(signal, events, retryAttempt, error);
|
|
4840
4919
|
this.emitDrop(signal, "retry-exhausted", dropped.length, error);
|
|
@@ -4845,7 +4924,15 @@ var MastraStorageExporter = class extends BaseExporter {
|
|
|
4845
4924
|
}
|
|
4846
4925
|
}
|
|
4847
4926
|
/**
|
|
4848
|
-
* Flush span update/end events
|
|
4927
|
+
* Flush span update/end events.
|
|
4928
|
+
*
|
|
4929
|
+
* When the store supports conditional updates, only spans this exporter started
|
|
4930
|
+
* and hasn't confirmed the create for are deferred. Everything else, including
|
|
4931
|
+
* spans another exporter instance started (such as a durable agent ending spans
|
|
4932
|
+
* it rebuilt after a crash), goes straight to storage, and updates that didn't
|
|
4933
|
+
* apply are counted as drops. Without conditional updates, updates for spans this
|
|
4934
|
+
* exporter hasn't created are deferred until their create is flushed.
|
|
4935
|
+
*
|
|
4849
4936
|
* When `isEnd` is true, successfully flushed spans are removed from tracking.
|
|
4850
4937
|
*/
|
|
4851
4938
|
async flushSpanUpdates(events, deferredUpdates, isEnd) {
|
|
@@ -4855,10 +4942,12 @@ var MastraStorageExporter = class extends BaseExporter {
|
|
|
4855
4942
|
this.emitDrop("tracing", "unsupported-storage", events.length);
|
|
4856
4943
|
return { failed: true };
|
|
4857
4944
|
}
|
|
4945
|
+
const storage = this.#observabilityStorage;
|
|
4946
|
+
const batchUpdateUnendedSpans = typeof storage.batchUpdateUnendedSpans === "function" ? storage.batchUpdateUnendedSpans.bind(storage) : void 0;
|
|
4858
4947
|
const partials = [];
|
|
4859
4948
|
for (const event of events) {
|
|
4860
4949
|
const span = event.exportedSpan;
|
|
4861
|
-
if (this.#eventBuffer.spanExists(span)) partials.push({
|
|
4950
|
+
if (batchUpdateUnendedSpans ? !this.#eventBuffer.isAwaitingCreate(span) : this.#eventBuffer.spanExists(span)) partials.push({
|
|
4862
4951
|
traceId: span.traceId,
|
|
4863
4952
|
spanId: span.id,
|
|
4864
4953
|
updates: (0, _mastra_core_storage.buildUpdateSpanRecord)(span)
|
|
@@ -4867,7 +4956,10 @@ var MastraStorageExporter = class extends BaseExporter {
|
|
|
4867
4956
|
}
|
|
4868
4957
|
if (partials.length === 0) return FLUSH_SUCCEEDED;
|
|
4869
4958
|
try {
|
|
4870
|
-
|
|
4959
|
+
if (batchUpdateUnendedSpans) {
|
|
4960
|
+
const { updated } = await batchUpdateUnendedSpans({ records: partials });
|
|
4961
|
+
this.emitNotApplied(partials, updated);
|
|
4962
|
+
} else await storage.batchUpdateSpans({ records: partials });
|
|
4871
4963
|
if (isEnd) this.#eventBuffer.endFinishedSpans({ records: partials });
|
|
4872
4964
|
return FLUSH_SUCCEEDED;
|
|
4873
4965
|
} catch (error) {
|
|
@@ -4891,6 +4983,27 @@ var MastraStorageExporter = class extends BaseExporter {
|
|
|
4891
4983
|
}
|
|
4892
4984
|
}
|
|
4893
4985
|
/**
|
|
4986
|
+
* Reports conditional span updates that storage didn't apply: the span wasn't
|
|
4987
|
+
* stored, or it had already ended. Ended spans are immutable, so these are dropped.
|
|
4988
|
+
*/
|
|
4989
|
+
emitNotApplied(partials, updated) {
|
|
4990
|
+
const appliedCounts = /* @__PURE__ */ new Map();
|
|
4991
|
+
for (const span of updated) {
|
|
4992
|
+
const key = `${span.traceId}:${span.spanId}`;
|
|
4993
|
+
appliedCounts.set(key, (appliedCounts.get(key) ?? 0) + 1);
|
|
4994
|
+
}
|
|
4995
|
+
let notApplied = 0;
|
|
4996
|
+
for (const partial of partials) {
|
|
4997
|
+
const key = `${partial.traceId}:${partial.spanId}`;
|
|
4998
|
+
const remaining = appliedCounts.get(key) ?? 0;
|
|
4999
|
+
if (remaining > 0) appliedCounts.set(key, remaining - 1);
|
|
5000
|
+
else notApplied++;
|
|
5001
|
+
}
|
|
5002
|
+
if (notApplied === 0) return;
|
|
5003
|
+
this.logger.debug("Span updates not applied: span not stored or already ended", { count: notApplied });
|
|
5004
|
+
this.emitDrop("tracing", "span-not-updatable", notApplied);
|
|
5005
|
+
}
|
|
5006
|
+
/**
|
|
4894
5007
|
* Runs flushBuffer() and tracks its promise so flush() can wait for writes
|
|
4895
5008
|
* that a timer or size trigger started on their own.
|
|
4896
5009
|
*/
|
|
@@ -5000,7 +5113,10 @@ var MastraStorageExporter = class extends BaseExporter {
|
|
|
5000
5113
|
this.logger.debug("Cannot store traces. Observability storage is not initialized");
|
|
5001
5114
|
return;
|
|
5002
5115
|
}
|
|
5003
|
-
this.#eventBuffer.addEvent(
|
|
5116
|
+
this.#eventBuffer.addEvent({
|
|
5117
|
+
...event,
|
|
5118
|
+
exportedSpan: withSpanPreviews(event.exportedSpan)
|
|
5119
|
+
});
|
|
5004
5120
|
await this.handleBatchedFlush();
|
|
5005
5121
|
}
|
|
5006
5122
|
/**
|
|
@@ -5252,6 +5368,10 @@ var BaseObservabilityInstance = class extends _mastra_core_base.MastraBase {
|
|
|
5252
5368
|
spanId: cached.id,
|
|
5253
5369
|
parentSpanId: cached.parentSpanId,
|
|
5254
5370
|
externalParentSpanId: cached.externalParentSpanId,
|
|
5371
|
+
resumedFromSpanId: cached.resumedFromSpanId,
|
|
5372
|
+
outcome: cached.outcome,
|
|
5373
|
+
stopReason: cached.stopReason,
|
|
5374
|
+
stopReasonContext: cached.stopReasonContext,
|
|
5255
5375
|
links: cached.links,
|
|
5256
5376
|
startTime: cached.startTime instanceof Date ? cached.startTime : new Date(cached.startTime),
|
|
5257
5377
|
input: cached.input,
|
|
@@ -8104,7 +8224,7 @@ var MastraPlatformExporter = class extends BaseExporter {
|
|
|
8104
8224
|
}
|
|
8105
8225
|
formatSpan(span) {
|
|
8106
8226
|
return {
|
|
8107
|
-
...span,
|
|
8227
|
+
...withSpanPreviews(span),
|
|
8108
8228
|
spanId: span.id,
|
|
8109
8229
|
spanType: span.type,
|
|
8110
8230
|
startedAt: span.startTime,
|