@mastra/observability 1.9.2-alpha.0 → 1.10.0-alpha.2

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
@@ -19144,14 +19144,16 @@ var ModelSpanTracker = class {
19144
19144
  dynamic,
19145
19145
  providerExecuted,
19146
19146
  providerMetadata,
19147
- // Output - the actual result
19147
+ // Keep provider-executed results on MODEL_CHUNK because they come
19148
+ // from the model/provider stream and may not have a sibling TOOL_CALL span.
19149
+ // For locally executed tools, the canonical payload lives on TOOL_CALL.
19148
19150
  result} = chunk.payload || {};
19149
19151
  const metadata = { toolCallId, toolName };
19150
19152
  if (isError !== void 0) metadata.isError = isError;
19151
19153
  if (dynamic !== void 0) metadata.dynamic = dynamic;
19152
19154
  if (providerExecuted !== void 0) metadata.providerExecuted = providerExecuted;
19153
19155
  if (providerMetadata !== void 0) metadata.providerMetadata = providerMetadata;
19154
- this.#createEventSpan(chunk.type, result, { metadata });
19156
+ this.#createEventSpan(chunk.type, providerExecuted ? result : void 0, { metadata });
19155
19157
  break;
19156
19158
  }
19157
19159
  }
@@ -19231,32 +19233,58 @@ var BaseSpan = class {
19231
19233
  parentSpanId;
19232
19234
  /** Deep clean options for serialization */
19233
19235
  deepCleanOptions;
19236
+ /**
19237
+ * Whether this span is filtered out before export. When true, BaseSpan/
19238
+ * DefaultSpan skip attaching attributes/input/output/errorInfo/requestContext
19239
+ * entirely -- they are never read on excluded spans, and skipping avoids
19240
+ * both the deepClean cost and holding references to large payloads for
19241
+ * the lifetime of the span. Set when excludeSpanTypes drops the type,
19242
+ * when the span is internal and includeInternalSpans is false, or when
19243
+ * the subclass is always excluded (e.g., NoOpSpan).
19244
+ *
19245
+ * Note: metadata is still attached and deepCleaned because it is read in
19246
+ * process by getCorrelationContext() and by getLoggerContext() /
19247
+ * getMetricsContext() (which structuredClone it).
19248
+ */
19249
+ isExcluded;
19234
19250
  /** Cached canonical correlation context for this live span */
19235
19251
  correlationContext;
19252
+ /**
19253
+ * Subclasses can override to unconditionally mark the span as excluded.
19254
+ * NoOpSpan uses this because it is never exported regardless of config.
19255
+ */
19256
+ get alwaysExcluded() {
19257
+ return false;
19258
+ }
19236
19259
  constructor(options, observabilityInstance) {
19237
- const serializationOptions = observabilityInstance.getConfig().serializationOptions;
19238
- this.deepCleanOptions = mergeSerializationOptions(serializationOptions);
19260
+ const observabilityConfig = observabilityInstance.getConfig();
19261
+ this.deepCleanOptions = mergeSerializationOptions(observabilityConfig.serializationOptions);
19239
19262
  this.name = options.name;
19240
19263
  this.type = options.type;
19241
- this.attributes = deepClean(options.attributes, this.deepCleanOptions) || {};
19264
+ this.isInternal = isSpanInternal(this.type, options.tracingPolicy?.internal);
19265
+ this.isExcluded = this.alwaysExcluded || observabilityConfig.excludeSpanTypes?.includes(this.type) === true || this.isInternal && !observabilityConfig.includeInternalSpans;
19242
19266
  this.metadata = deepClean(
19243
19267
  options.parent?.metadata || options.metadata ? { ...options.parent?.metadata, ...options.metadata } : void 0,
19244
19268
  this.deepCleanOptions
19245
19269
  );
19246
- if (options.requestContext && options.requestContext.size() > 0) {
19247
- this.requestContext = deepClean(options.requestContext.all, this.deepCleanOptions);
19248
- }
19249
19270
  this.parent = options.parent;
19250
19271
  this.startTime = options.startTime ?? /* @__PURE__ */ new Date();
19251
19272
  this.observabilityInstance = observabilityInstance;
19252
19273
  this.isEvent = options.isEvent ?? false;
19253
- this.isInternal = isSpanInternal(this.type, options.tracingPolicy?.internal);
19254
19274
  this.traceState = options.traceState;
19255
19275
  this.tags = !options.parent && options.tags?.length ? options.tags : void 0;
19256
19276
  const entityParent = this.getParentSpan(false);
19257
19277
  this.entityType = options.entityType ?? entityParent?.entityType;
19258
19278
  this.entityId = options.entityId ?? entityParent?.entityId;
19259
19279
  this.entityName = options.entityName ?? entityParent?.entityName;
19280
+ if (this.isExcluded) {
19281
+ this.attributes = {};
19282
+ return;
19283
+ }
19284
+ this.attributes = deepClean(options.attributes, this.deepCleanOptions) || {};
19285
+ if (options.requestContext && options.requestContext.size() > 0) {
19286
+ this.requestContext = deepClean(options.requestContext.all, this.deepCleanOptions);
19287
+ }
19260
19288
  if (this.isEvent) {
19261
19289
  this.output = deepClean(options.output, this.deepCleanOptions);
19262
19290
  } else {
@@ -19460,46 +19488,51 @@ var DefaultSpan = class extends BaseSpan {
19460
19488
  return;
19461
19489
  }
19462
19490
  this.endTime = /* @__PURE__ */ new Date();
19491
+ if (options?.metadata) {
19492
+ this.metadata = { ...this.metadata, ...deepClean(options.metadata, this.deepCleanOptions) };
19493
+ }
19494
+ if (this.isExcluded) {
19495
+ return;
19496
+ }
19463
19497
  if (options?.output !== void 0) {
19464
19498
  this.output = deepClean(options.output, this.deepCleanOptions);
19465
19499
  }
19466
19500
  if (options?.attributes) {
19467
19501
  this.attributes = { ...this.attributes, ...deepClean(options.attributes, this.deepCleanOptions) };
19468
19502
  }
19469
- if (options?.metadata) {
19470
- this.metadata = { ...this.metadata, ...deepClean(options.metadata, this.deepCleanOptions) };
19471
- }
19472
19503
  }
19473
19504
  error(options) {
19474
19505
  if (this.isEvent) {
19475
19506
  return;
19476
19507
  }
19477
19508
  const { error: error48, endSpan = true, attributes, metadata } = options;
19478
- this.errorInfo = deepClean(
19479
- error48 instanceof MastraError ? {
19480
- id: error48.id,
19481
- details: error48.details,
19482
- category: error48.category,
19483
- domain: error48.domain,
19484
- message: error48.message,
19485
- name: error48.name,
19486
- // Prefer the original cause's stack when available. MastraError wraps
19487
- // thrown errors, so its own stack points to the wrapping site rather
19488
- // than where the underlying error was thrown.
19489
- stack: error48.cause instanceof Error && error48.cause.stack || error48.stack
19490
- } : {
19491
- message: error48.message,
19492
- name: error48.name,
19493
- stack: error48.stack
19494
- },
19495
- this.deepCleanOptions
19496
- );
19497
- if (attributes) {
19498
- this.attributes = { ...this.attributes, ...deepClean(attributes, this.deepCleanOptions) };
19499
- }
19500
19509
  if (metadata) {
19501
19510
  this.metadata = { ...this.metadata, ...deepClean(metadata, this.deepCleanOptions) };
19502
19511
  }
19512
+ if (!this.isExcluded) {
19513
+ this.errorInfo = deepClean(
19514
+ error48 instanceof MastraError ? {
19515
+ id: error48.id,
19516
+ details: error48.details,
19517
+ category: error48.category,
19518
+ domain: error48.domain,
19519
+ message: error48.message,
19520
+ name: error48.name,
19521
+ // Prefer the original cause's stack when available. MastraError wraps
19522
+ // thrown errors, so its own stack points to the wrapping site rather
19523
+ // than where the underlying error was thrown.
19524
+ stack: error48.cause instanceof Error && error48.cause.stack || error48.stack
19525
+ } : {
19526
+ message: error48.message,
19527
+ name: error48.name,
19528
+ stack: error48.stack
19529
+ },
19530
+ this.deepCleanOptions
19531
+ );
19532
+ if (attributes) {
19533
+ this.attributes = { ...this.attributes, ...deepClean(attributes, this.deepCleanOptions) };
19534
+ }
19535
+ }
19503
19536
  if (endSpan) {
19504
19537
  this.end();
19505
19538
  } else {
@@ -19513,6 +19546,12 @@ var DefaultSpan = class extends BaseSpan {
19513
19546
  if (options.name !== void 0) {
19514
19547
  this.name = options.name;
19515
19548
  }
19549
+ if (options.metadata) {
19550
+ this.metadata = { ...this.metadata, ...deepClean(options.metadata, this.deepCleanOptions) };
19551
+ }
19552
+ if (this.isExcluded) {
19553
+ return;
19554
+ }
19516
19555
  if (options.input !== void 0) {
19517
19556
  this.input = deepClean(options.input, this.deepCleanOptions);
19518
19557
  }
@@ -19522,9 +19561,6 @@ var DefaultSpan = class extends BaseSpan {
19522
19561
  if (options.attributes) {
19523
19562
  this.attributes = { ...this.attributes, ...deepClean(options.attributes, this.deepCleanOptions) };
19524
19563
  }
19525
- if (options.metadata) {
19526
- this.metadata = { ...this.metadata, ...deepClean(options.metadata, this.deepCleanOptions) };
19527
- }
19528
19564
  }
19529
19565
  get isValid() {
19530
19566
  return true;
@@ -19600,6 +19636,10 @@ var NoOpSpan = class extends BaseSpan {
19600
19636
  get isValid() {
19601
19637
  return false;
19602
19638
  }
19639
+ // NoOpSpan is never exported, so treat it as always excluded.
19640
+ get alwaysExcluded() {
19641
+ return true;
19642
+ }
19603
19643
  };
19604
19644
 
19605
19645
  // src/instances/base.ts