@mastra/observability 1.18.3 → 1.18.4-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
@@ -1384,6 +1384,13 @@ const PricingMeter = {
1384
1384
  OUTPUT_IMAGE_TOKENS: "output_image_tokens",
1385
1385
  OUTPUT_REASONING_TOKENS: "output_reasoning_tokens"
1386
1386
  };
1387
+ /**
1388
+ * Local copy of `TokenMetrics` from `@mastra/core/observability`. Core added that export after
1389
+ * this package's minimum core version, so re-exporting it would raise the peer floor here and in
1390
+ * every exporter that depends on this package. `types.test.ts` keeps the two copies identical.
1391
+ *
1392
+ * TODO(mastra-v2): replace this with `export { TokenMetrics } from '@mastra/core/observability'`.
1393
+ */
1387
1394
  const TokenMetrics = {
1388
1395
  TOTAL_INPUT: "mastra_model_total_input_tokens",
1389
1396
  TOTAL_OUTPUT: "mastra_model_total_output_tokens",
@@ -1696,10 +1703,11 @@ function emitTokenMetrics(span, metrics) {
1696
1703
  * ancestor span: the metric labels come from the ancestor's context, the
1697
1704
  * cost calculation still uses the original model that incurred the tokens.
1698
1705
  */
1699
- function emitTokenMetricsForUsage(usage, provider, model, metrics) {
1706
+ function emitTokenMetricsForUsage(usage, provider, model, usageIncomplete, metrics) {
1700
1707
  emitUsageMetrics({
1701
1708
  provider,
1702
- model
1709
+ model,
1710
+ usageIncomplete
1703
1711
  }, usage, metrics);
1704
1712
  }
1705
1713
  /** Emit all auto-extracted metrics for a live span end. */
@@ -1733,13 +1741,14 @@ function emitUsageMetrics(attrs, usage, metrics) {
1733
1741
  } catch {
1734
1742
  metricCosts = /* @__PURE__ */ new Map();
1735
1743
  }
1744
+ const labels = attrs.usageIncomplete ? { usageIncomplete: "true" } : void 0;
1736
1745
  const emit = (name, value) => {
1737
1746
  const costContext = metricCosts.get(name);
1738
1747
  if (!costContext) {
1739
- metrics.emit(name, value);
1748
+ metrics.emit(name, value, labels);
1740
1749
  return;
1741
1750
  }
1742
- metrics.emit(name, value, void 0, { costContext });
1751
+ metrics.emit(name, value, labels, { costContext });
1743
1752
  };
1744
1753
  for (const sample of getTokenMetricSamples(usage)) emit(sample.name, sample.value);
1745
1754
  }
@@ -2428,6 +2437,7 @@ var ModelSpanTracker = class {
2428
2437
  ...ctx?.parameters !== void 0 ? { parameters: ctx.parameters } : {},
2429
2438
  ...ctx?.providerOptions !== void 0 ? { providerOptions: ctx.providerOptions } : {},
2430
2439
  ...ctx?.availableTools !== void 0 ? { availableTools: ctx.availableTools } : {},
2440
+ ...ctx?.tools !== void 0 ? { tools: ctx.tools } : {},
2431
2441
  ...ctx?.toolChoice !== void 0 ? { toolChoice: ctx.toolChoice } : {},
2432
2442
  ...ctx?.responseFormat !== void 0 ? { responseFormat: ctx.responseFormat } : {}
2433
2443
  },
@@ -5091,7 +5101,7 @@ var BaseObservabilityInstance = class extends MastraBase {
5091
5101
  if (processedSpan) {
5092
5102
  emitAutoExtractedMetrics(processedSpan, this.getMetricsContext(processedSpan));
5093
5103
  const processedAttrs = processedSpan.attributes;
5094
- if (excludedModelUsage && !processedAttrs?.usage) emitTokenMetricsForUsage(excludedModelUsage.usage, excludedModelUsage.provider, excludedModelUsage.model, this.getMetricsContext(processedSpan));
5104
+ if (excludedModelUsage && !processedAttrs?.usage) emitTokenMetricsForUsage(excludedModelUsage.usage, excludedModelUsage.provider, excludedModelUsage.model, excludedModelUsage.usageIncomplete, this.getMetricsContext(processedSpan));
5095
5105
  }
5096
5106
  } catch (err) {
5097
5107
  this.logger.error("[Observability] Auto-extraction error:", err);
@@ -5154,7 +5164,8 @@ var BaseObservabilityInstance = class extends MastraBase {
5154
5164
  ancestor,
5155
5165
  usage,
5156
5166
  provider: endAttrs?.provider ?? liveAttrs?.provider,
5157
- model: resolveModelId(endAttrs?.responseModel, endAttrs?.model, liveAttrs?.responseModel, liveAttrs?.model)
5167
+ model: resolveModelId(endAttrs?.responseModel, endAttrs?.model, liveAttrs?.responseModel, liveAttrs?.model),
5168
+ usageIncomplete: endAttrs?.usageIncomplete ?? liveAttrs?.usageIncomplete
5158
5169
  };
5159
5170
  }
5160
5171
  /**
@@ -5176,7 +5187,8 @@ var BaseObservabilityInstance = class extends MastraBase {
5176
5187
  return {
5177
5188
  usage,
5178
5189
  provider: endAttrs?.provider ?? liveAttrs?.provider ?? stashed?.provider,
5179
- model: resolveModelId(endAttrs?.responseModel, endAttrs?.model, liveAttrs?.responseModel, liveAttrs?.model, stashed?.model)
5190
+ model: resolveModelId(endAttrs?.responseModel, endAttrs?.model, liveAttrs?.responseModel, liveAttrs?.model, stashed?.model),
5191
+ usageIncomplete: endAttrs?.usageIncomplete ?? liveAttrs?.usageIncomplete
5180
5192
  };
5181
5193
  }
5182
5194
  /**
@@ -5186,11 +5198,11 @@ var BaseObservabilityInstance = class extends MastraBase {
5186
5198
  * span instead of the hidden agent that incurred them.
5187
5199
  */
5188
5200
  applyUsageRollup(target) {
5189
- const { ancestor, usage, provider, model } = target;
5201
+ const { ancestor, usage, provider, model, usageIncomplete } = target;
5190
5202
  const attrs = ancestor.attributes;
5191
5203
  attrs.internalUsage = addUsageStats(attrs.internalUsage, usage);
5192
5204
  try {
5193
- emitTokenMetricsForUsage(usage, provider, model, this.getMetricsContext(ancestor));
5205
+ emitTokenMetricsForUsage(usage, provider, model, usageIncomplete, this.getMetricsContext(ancestor));
5194
5206
  } catch (err) {
5195
5207
  this.logger.error("[Observability] Usage rollup metric emission error:", err);
5196
5208
  }