@mastra/observability 1.7.0 → 1.7.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/dist/index.js CHANGED
@@ -15960,7 +15960,8 @@ var TestExporter = class extends BaseExporter {
15960
15960
  this.#trackEvent("score");
15961
15961
  if (this.#config.storeLogs) {
15962
15962
  const score = event.score;
15963
- const logMessage = `[TestExporter] score: ${score.scorerId}=${score.score} (trace: ${score.traceId.slice(-8)}${score.spanId ? `, span: ${score.spanId.slice(-8)}` : ""})`;
15963
+ const traceLabel = score.traceId ? score.traceId.slice(-8) : "unanchored";
15964
+ const logMessage = `[TestExporter] score: ${score.scorerId}=${score.score} (trace: ${traceLabel}${score.spanId ? `, span: ${score.spanId.slice(-8)}` : ""})`;
15964
15965
  this.#debugLogs.push(logMessage);
15965
15966
  }
15966
15967
  this.#scoreEvents.push(event);
@@ -15972,7 +15973,9 @@ var TestExporter = class extends BaseExporter {
15972
15973
  this.#trackEvent("feedback");
15973
15974
  if (this.#config.storeLogs) {
15974
15975
  const fb = event.feedback;
15975
- const logMessage = `[TestExporter] feedback: ${fb.feedbackType} from ${fb.source}=${fb.value} (trace: ${fb.traceId.slice(-8)}${fb.spanId ? `, span: ${fb.spanId.slice(-8)}` : ""})`;
15976
+ const traceLabel = fb.traceId ? fb.traceId.slice(-8) : "unanchored";
15977
+ const feedbackSource = fb.feedbackSource ?? fb.source;
15978
+ const logMessage = `[TestExporter] feedback: ${fb.feedbackType} from ${feedbackSource}=${fb.value} (trace: ${traceLabel}${fb.spanId ? `, span: ${fb.spanId.slice(-8)}` : ""})`;
15976
15979
  this.#debugLogs.push(logMessage);
15977
15980
  }
15978
15981
  this.#feedbackEvents.push(event);
@@ -16122,10 +16125,14 @@ var TestExporter = class extends BaseExporter {
16122
16125
  if (event.log.traceId) traceIds.add(event.log.traceId);
16123
16126
  }
16124
16127
  for (const event of this.#scoreEvents) {
16125
- traceIds.add(event.score.traceId);
16128
+ if (event.score.traceId) {
16129
+ traceIds.add(event.score.traceId);
16130
+ }
16126
16131
  }
16127
16132
  for (const event of this.#feedbackEvents) {
16128
- traceIds.add(event.feedback.traceId);
16133
+ if (event.feedback.traceId) {
16134
+ traceIds.add(event.feedback.traceId);
16135
+ }
16129
16136
  }
16130
16137
  return Array.from(traceIds);
16131
16138
  }
@@ -17428,7 +17435,14 @@ var PricingRegistry = class _PricingRegistry {
17428
17435
  return _PricingRegistry.globalRegistry;
17429
17436
  }
17430
17437
  get(args) {
17431
- return this.pricingModels.get(makePricingKey(args)) ?? null;
17438
+ const key = makePricingKey(args);
17439
+ const exact = this.pricingModels.get(key);
17440
+ if (exact) return exact;
17441
+ const dashedKey = makePricingKey({ provider: args.provider, model: args.model.replace(/\./g, "-") });
17442
+ if (dashedKey !== key) {
17443
+ return this.pricingModels.get(dashedKey) ?? null;
17444
+ }
17445
+ return null;
17432
17446
  }
17433
17447
  };
17434
17448
  function loadPricingModels() {
@@ -18881,15 +18895,22 @@ var DefaultSpan = class extends BaseSpan {
18881
18895
  return;
18882
18896
  }
18883
18897
  const { error: error48, endSpan = true, attributes, metadata } = options;
18884
- this.errorInfo = error48 instanceof MastraError ? {
18885
- id: error48.id,
18886
- details: error48.details,
18887
- category: error48.category,
18888
- domain: error48.domain,
18889
- message: error48.message
18890
- } : {
18891
- message: error48.message
18892
- };
18898
+ this.errorInfo = deepClean(
18899
+ error48 instanceof MastraError ? {
18900
+ id: error48.id,
18901
+ details: error48.details,
18902
+ category: error48.category,
18903
+ domain: error48.domain,
18904
+ message: error48.message,
18905
+ name: error48.name,
18906
+ stack: error48.stack
18907
+ } : {
18908
+ message: error48.message,
18909
+ name: error48.name,
18910
+ stack: error48.stack
18911
+ },
18912
+ this.deepCleanOptions
18913
+ );
18893
18914
  if (attributes) {
18894
18915
  this.attributes = { ...this.attributes, ...deepClean(attributes, this.deepCleanOptions) };
18895
18916
  }
@@ -19555,6 +19576,8 @@ function normalizeErrorInfo(error48) {
19555
19576
  return {
19556
19577
  message: error48.message,
19557
19578
  id: "id" in error48 && typeof error48.id === "string" ? error48.id : void 0,
19579
+ name: "name" in error48 && typeof error48.name === "string" ? error48.name : void 0,
19580
+ stack: "stack" in error48 && typeof error48.stack === "string" ? error48.stack : void 0,
19558
19581
  domain: "domain" in error48 && typeof error48.domain === "string" ? error48.domain : void 0,
19559
19582
  category: "category" in error48 && typeof error48.category === "string" ? error48.category : void 0,
19560
19583
  details: "details" in error48 && error48.details && typeof error48.details === "object" ? error48.details : void 0
@@ -19585,13 +19608,13 @@ function buildCorrelationContext(span, rootSpan, parent) {
19585
19608
  experimentId: nullToUndefined(span.experimentId)
19586
19609
  };
19587
19610
  }
19588
- function buildRecordedScoreEvent(args) {
19589
- const { span, rootSpan, parent, score, spanId } = args;
19611
+ function buildScoreEvent(args) {
19612
+ const { traceId, spanId, correlationContext, score, inheritedMetadata } = args;
19590
19613
  return {
19591
19614
  type: "score",
19592
19615
  score: {
19593
19616
  timestamp: /* @__PURE__ */ new Date(),
19594
- traceId: span.traceId,
19617
+ traceId,
19595
19618
  spanId,
19596
19619
  scorerId: score.scorerId,
19597
19620
  scorerVersion: score.scorerVersion,
@@ -19601,18 +19624,18 @@ function buildRecordedScoreEvent(args) {
19601
19624
  reason: score.reason,
19602
19625
  experimentId: score.experimentId,
19603
19626
  scoreTraceId: score.scoreTraceId,
19604
- correlationContext: buildCorrelationContext(span, rootSpan, parent),
19605
- metadata: mergeMetadata(span.metadata, score.metadata)
19627
+ correlationContext,
19628
+ metadata: mergeMetadata(inheritedMetadata, score.metadata)
19606
19629
  }
19607
19630
  };
19608
19631
  }
19609
- function buildRecordedFeedbackEvent(args) {
19610
- const { span, rootSpan, parent, feedback, spanId } = args;
19632
+ function buildFeedbackEvent(args) {
19633
+ const { traceId, spanId, correlationContext, feedback, inheritedMetadata } = args;
19611
19634
  return {
19612
19635
  type: "feedback",
19613
19636
  feedback: {
19614
19637
  timestamp: /* @__PURE__ */ new Date(),
19615
- traceId: span.traceId,
19638
+ traceId,
19616
19639
  spanId,
19617
19640
  source: feedback.source,
19618
19641
  feedbackSource: feedback.feedbackSource,
@@ -19623,8 +19646,8 @@ function buildRecordedFeedbackEvent(args) {
19623
19646
  comment: feedback.comment,
19624
19647
  sourceId: feedback.sourceId,
19625
19648
  experimentId: feedback.experimentId,
19626
- correlationContext: buildCorrelationContext(span, rootSpan, parent),
19627
- metadata: mergeMetadata(span.metadata, feedback.metadata)
19649
+ correlationContext,
19650
+ metadata: mergeMetadata(inheritedMetadata, feedback.metadata)
19628
19651
  }
19629
19652
  };
19630
19653
  }
@@ -19638,12 +19661,12 @@ function buildRecordedScoreEventFromTrace(args) {
19638
19661
  const span = args.spanId ? findSpanById(args.trace.spans, args.spanId) : rootSpan;
19639
19662
  if (!span) return null;
19640
19663
  const parent = span.parentSpanId ? findSpanById(args.trace.spans, span.parentSpanId) : void 0;
19641
- return buildRecordedScoreEvent({
19642
- span,
19643
- rootSpan,
19644
- parent,
19664
+ return buildScoreEvent({
19665
+ traceId: span.traceId,
19666
+ spanId: args.spanId,
19667
+ correlationContext: buildCorrelationContext(span, rootSpan, parent),
19645
19668
  score: args.score,
19646
- spanId: args.spanId
19669
+ inheritedMetadata: span.metadata
19647
19670
  });
19648
19671
  }
19649
19672
  function buildRecordedFeedbackEventFromTrace(args) {
@@ -19652,12 +19675,12 @@ function buildRecordedFeedbackEventFromTrace(args) {
19652
19675
  const span = args.spanId ? findSpanById(args.trace.spans, args.spanId) : rootSpan;
19653
19676
  if (!span) return null;
19654
19677
  const parent = span.parentSpanId ? findSpanById(args.trace.spans, span.parentSpanId) : void 0;
19655
- return buildRecordedFeedbackEvent({
19656
- span,
19657
- rootSpan,
19658
- parent,
19678
+ return buildFeedbackEvent({
19679
+ traceId: span.traceId,
19680
+ spanId: args.spanId,
19681
+ correlationContext: buildCorrelationContext(span, rootSpan, parent),
19659
19682
  feedback: args.feedback,
19660
- spanId: args.spanId
19683
+ inheritedMetadata: span.metadata
19661
19684
  });
19662
19685
  }
19663
19686
  var RecordedSpanImpl = class {
@@ -19720,12 +19743,12 @@ var RecordedSpanImpl = class {
19720
19743
  return;
19721
19744
  }
19722
19745
  await this.#emitRecordedEvent(
19723
- buildRecordedScoreEvent({
19724
- span: this.#raw,
19725
- rootSpan: this.#rootSpan,
19726
- parent: this.parent,
19746
+ buildScoreEvent({
19747
+ traceId: this.#raw.traceId,
19748
+ spanId: this.id,
19749
+ correlationContext: buildCorrelationContext(this.#raw, this.#rootSpan, this.parent),
19727
19750
  score,
19728
- spanId: this.id
19751
+ inheritedMetadata: this.#raw.metadata
19729
19752
  })
19730
19753
  );
19731
19754
  }
@@ -19735,12 +19758,12 @@ var RecordedSpanImpl = class {
19735
19758
  return;
19736
19759
  }
19737
19760
  await this.#emitRecordedEvent(
19738
- buildRecordedFeedbackEvent({
19739
- span: this.#raw,
19740
- rootSpan: this.#rootSpan,
19741
- parent: this.parent,
19761
+ buildFeedbackEvent({
19762
+ traceId: this.#raw.traceId,
19763
+ spanId: this.id,
19764
+ correlationContext: buildCorrelationContext(this.#raw, this.#rootSpan, this.parent),
19742
19765
  feedback,
19743
- spanId: this.id
19766
+ inheritedMetadata: this.#raw.metadata
19744
19767
  })
19745
19768
  );
19746
19769
  }
@@ -19773,10 +19796,11 @@ var RecordedTraceImpl = class {
19773
19796
  return;
19774
19797
  }
19775
19798
  await this.#emitRecordedEvent(
19776
- buildRecordedScoreEvent({
19777
- span: this.#rootRecord,
19778
- rootSpan: this.#rootRecord,
19779
- score
19799
+ buildScoreEvent({
19800
+ traceId: this.#rootRecord.traceId,
19801
+ correlationContext: buildCorrelationContext(this.#rootRecord, this.#rootRecord),
19802
+ score,
19803
+ inheritedMetadata: this.#rootRecord.metadata
19780
19804
  })
19781
19805
  );
19782
19806
  }
@@ -19786,10 +19810,11 @@ var RecordedTraceImpl = class {
19786
19810
  return;
19787
19811
  }
19788
19812
  await this.#emitRecordedEvent(
19789
- buildRecordedFeedbackEvent({
19790
- span: this.#rootRecord,
19791
- rootSpan: this.#rootRecord,
19792
- feedback
19813
+ buildFeedbackEvent({
19814
+ traceId: this.#rootRecord.traceId,
19815
+ correlationContext: buildCorrelationContext(this.#rootRecord, this.#rootRecord),
19816
+ feedback,
19817
+ inheritedMetadata: this.#rootRecord.metadata
19793
19818
  })
19794
19819
  );
19795
19820
  }
@@ -20211,6 +20236,22 @@ var Observability = class extends MastraBase {
20211
20236
  });
20212
20237
  }
20213
20238
  async addScore(args) {
20239
+ const targetTraceId = args.traceId ?? args.correlationContext?.traceId;
20240
+ const targetSpanId = args.spanId ?? args.correlationContext?.spanId;
20241
+ if (args.correlationContext) {
20242
+ await this.#emitRecordedEvent(
20243
+ buildScoreEvent({
20244
+ ...targetTraceId ? { traceId: targetTraceId } : {},
20245
+ ...targetSpanId ? { spanId: targetSpanId } : {},
20246
+ correlationContext: args.correlationContext,
20247
+ score: args.score
20248
+ })
20249
+ );
20250
+ return;
20251
+ }
20252
+ if (!args.traceId) {
20253
+ return;
20254
+ }
20214
20255
  const trace = await this.#getStoredTrace(args.traceId);
20215
20256
  if (!trace) {
20216
20257
  return;
@@ -20226,6 +20267,22 @@ var Observability = class extends MastraBase {
20226
20267
  await this.#emitRecordedEvent(event);
20227
20268
  }
20228
20269
  async addFeedback(args) {
20270
+ const targetTraceId = args.traceId ?? args.correlationContext?.traceId;
20271
+ const targetSpanId = args.spanId ?? args.correlationContext?.spanId;
20272
+ if (args.correlationContext) {
20273
+ await this.#emitRecordedEvent(
20274
+ buildFeedbackEvent({
20275
+ ...targetTraceId ? { traceId: targetTraceId } : {},
20276
+ ...targetSpanId ? { spanId: targetSpanId } : {},
20277
+ correlationContext: args.correlationContext,
20278
+ feedback: args.feedback
20279
+ })
20280
+ );
20281
+ return;
20282
+ }
20283
+ if (!args.traceId) {
20284
+ return;
20285
+ }
20229
20286
  const trace = await this.#getStoredTrace(args.traceId);
20230
20287
  if (!trace) {
20231
20288
  return;