@mastra/observability 1.16.4 → 1.16.6-alpha.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/index.js CHANGED
@@ -929,6 +929,12 @@ function cloneCostContext(costContext) {
929
929
  };
930
930
  }
931
931
  //#endregion
932
+ //#region src/model-id.ts
933
+ /** Return the first model id that contains non-whitespace characters. */
934
+ function resolveModelId(...candidates) {
935
+ return candidates.find((candidate) => candidate?.trim());
936
+ }
937
+ //#endregion
932
938
  //#region src/metrics/pricing-model.ts
933
939
  var PricingTier = class {
934
940
  index;
@@ -1505,7 +1511,7 @@ function emitUsageMetrics(attrs, usage, metrics) {
1505
1511
  if (providedCostContext) metricCosts = providedCostContext;
1506
1512
  else try {
1507
1513
  const provider = attrs.provider;
1508
- const model = attrs.responseModel ?? attrs.model;
1514
+ const model = resolveModelId(attrs.responseModel, attrs.model);
1509
1515
  if (provider && model) {
1510
1516
  metricCosts = estimateCosts({
1511
1517
  provider,
@@ -1542,7 +1548,7 @@ function getProvidedCostContext(attrs, usage) {
1542
1548
  if (typeof costContext?.estimatedCost !== "number") return;
1543
1549
  const carrierMetric = usage.inputTokens !== void 0 ? TokenMetrics.TOTAL_INPUT : TokenMetrics.TOTAL_OUTPUT;
1544
1550
  const provider = costContext.provider ?? attrs.provider;
1545
- const model = costContext.model ?? attrs.responseModel ?? attrs.model;
1551
+ const model = resolveModelId(costContext.model, attrs.responseModel, attrs.model);
1546
1552
  const contexts = /* @__PURE__ */ new Map();
1547
1553
  for (const sample of getTokenMetricSamples(usage)) contexts.set(sample.name, {
1548
1554
  provider,
@@ -2974,7 +2980,7 @@ var BaseObservabilityInstance = class extends MastraBase {
2974
2980
  });
2975
2981
  if (rest.type === SpanType.MODEL_GENERATION && this.config.excludeSpanTypes?.includes(SpanType.MODEL_GENERATION)) {
2976
2982
  const attrs = rest.attributes;
2977
- const model = attrs?.responseModel ?? attrs?.model;
2983
+ const model = resolveModelId(attrs?.responseModel, attrs?.model);
2978
2984
  if (attrs?.provider || model) this.#excludedModelMeta.set(span, {
2979
2985
  provider: attrs?.provider,
2980
2986
  model
@@ -3377,7 +3383,7 @@ var BaseObservabilityInstance = class extends MastraBase {
3377
3383
  ancestor,
3378
3384
  usage,
3379
3385
  provider: endAttrs?.provider ?? liveAttrs?.provider,
3380
- model: endAttrs?.responseModel ?? endAttrs?.model ?? liveAttrs?.responseModel ?? liveAttrs?.model
3386
+ model: resolveModelId(endAttrs?.responseModel, endAttrs?.model, liveAttrs?.responseModel, liveAttrs?.model)
3381
3387
  };
3382
3388
  }
3383
3389
  /**
@@ -3399,7 +3405,7 @@ var BaseObservabilityInstance = class extends MastraBase {
3399
3405
  return {
3400
3406
  usage,
3401
3407
  provider: endAttrs?.provider ?? liveAttrs?.provider ?? stashed?.provider,
3402
- model: endAttrs?.responseModel ?? endAttrs?.model ?? liveAttrs?.responseModel ?? liveAttrs?.model ?? stashed?.model
3408
+ model: resolveModelId(endAttrs?.responseModel, endAttrs?.model, liveAttrs?.responseModel, liveAttrs?.model, stashed?.model)
3403
3409
  };
3404
3410
  }
3405
3411
  /**
@@ -8690,6 +8696,23 @@ function isInstance(obj) {
8690
8696
  return obj instanceof BaseObservabilityInstance;
8691
8697
  }
8692
8698
  /**
8699
+ * Delays (ms) between attempts to rehydrate a trace from storage when an
8700
+ * annotation (score/feedback) targets a span that has not been flushed by
8701
+ * the configured exporters yet. Exporters buffer and flush asynchronously:
8702
+ * by default they flush at 1000 spans or after a 5000ms wait
8703
+ * (`maxBatchWaitMs`), so an annotation emitted right after a span ends can
8704
+ * race the flush and be silently dropped. The schedule below sums to
8705
+ * ~5.85s, covering the default flush interval with margin.
8706
+ */
8707
+ const RECORDED_TRACE_LOOKUP_RETRY_DELAYS_MS = [
8708
+ 100,
8709
+ 250,
8710
+ 500,
8711
+ 1e3,
8712
+ 2e3,
8713
+ 2e3
8714
+ ];
8715
+ /**
8693
8716
  * Top-level observability entrypoint. Manages a registry of ObservabilityInstance
8694
8717
  * configurations and provides instance selection via config selectors.
8695
8718
  */
@@ -8846,14 +8869,15 @@ var Observability = class extends MastraBase {
8846
8869
  return;
8847
8870
  }
8848
8871
  if (!args.traceId) return;
8849
- const trace = await this.#getStoredTrace(args.traceId);
8850
- if (!trace) return;
8851
- const event = buildRecordedScoreEventFromTrace({
8872
+ const event = await this.#buildRecordedEventWithRetry(args.traceId, (trace) => buildRecordedScoreEventFromTrace({
8852
8873
  trace,
8853
8874
  spanId: args.spanId,
8854
8875
  score: args.score
8855
- });
8856
- if (!event) return;
8876
+ }));
8877
+ if (!event) {
8878
+ this.logger?.warn(`Score event was dropped because the target trace/span was not found in observability storage (traceId: ${args.traceId}, spanId: ${args.spanId})`);
8879
+ return;
8880
+ }
8857
8881
  await this.#emitRecordedEvent(event);
8858
8882
  }
8859
8883
  async addFeedback(args) {
@@ -8869,14 +8893,15 @@ var Observability = class extends MastraBase {
8869
8893
  return;
8870
8894
  }
8871
8895
  if (!args.traceId) return;
8872
- const trace = await this.#getStoredTrace(args.traceId);
8873
- if (!trace) return;
8874
- const event = buildRecordedFeedbackEventFromTrace({
8896
+ const event = await this.#buildRecordedEventWithRetry(args.traceId, (trace) => buildRecordedFeedbackEventFromTrace({
8875
8897
  trace,
8876
8898
  spanId: args.spanId,
8877
8899
  feedback: args.feedback
8878
- });
8879
- if (!event) return;
8900
+ }));
8901
+ if (!event) {
8902
+ this.logger?.warn(`Feedback event was dropped because the target trace/span was not found in observability storage (traceId: ${args.traceId}, spanId: ${args.spanId})`);
8903
+ return;
8904
+ }
8880
8905
  await this.#emitRecordedEvent(event);
8881
8906
  }
8882
8907
  /** Register a named observability instance, optionally marking it as default. */
@@ -8941,6 +8966,20 @@ var Observability = class extends MastraBase {
8941
8966
  if (!storage) return null;
8942
8967
  return await storage.getStore("observability") ?? null;
8943
8968
  }
8969
+ /**
8970
+ * Build a recorded score/feedback event from storage, retrying briefly to
8971
+ * ride out the async exporter flush: annotations emitted right after a
8972
+ * span ends can otherwise race the flush and be silently dropped.
8973
+ */
8974
+ async #buildRecordedEventWithRetry(traceId, build) {
8975
+ for (const delayMs of [0, ...RECORDED_TRACE_LOOKUP_RETRY_DELAYS_MS]) {
8976
+ if (delayMs > 0) await new Promise((resolve) => setTimeout(resolve, delayMs));
8977
+ const trace = await this.#getStoredTrace(traceId);
8978
+ const event = trace ? build(trace) : null;
8979
+ if (event) return event;
8980
+ }
8981
+ return null;
8982
+ }
8944
8983
  async #getStoredTrace(traceId) {
8945
8984
  const observabilityStorage = await this.#getObservabilityStorage();
8946
8985
  if (!observabilityStorage) return null;