@mastra/observability 1.9.1 → 1.9.2-alpha.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
@@ -15050,7 +15050,6 @@ var SIGNAL_PUBLISH_SEGMENTS = {
15050
15050
  scores: "scores",
15051
15051
  feedback: "feedback"
15052
15052
  };
15053
- var DEFAULT_CLOUD_ENDPOINT = "https://api.mastra.ai";
15054
15053
  function trimTrailingSlashes(value) {
15055
15054
  let end = value.length;
15056
15055
  while (end > 0 && value.charCodeAt(end - 1) === 47) {
@@ -15179,7 +15178,7 @@ var CloudExporter = class extends BaseExporter {
15179
15178
  if (tracesEndpointOverride) {
15180
15179
  tracesEndpoint = resolveExplicitSignalEndpoint("traces", tracesEndpointOverride, projectId);
15181
15180
  } else {
15182
- baseEndpoint = resolveBaseEndpoint(config2.endpoint ?? DEFAULT_CLOUD_ENDPOINT);
15181
+ baseEndpoint = resolveBaseEndpoint(config2.endpoint ?? "https://observability.mastra.ai");
15183
15182
  tracesEndpoint = buildSignalEndpoint(baseEndpoint, "traces", projectId);
15184
15183
  }
15185
15184
  const resolveConfiguredSignalEndpoint = (signal, explicitEndpoint) => {
@@ -17428,51 +17427,6 @@ function isJsonSchema(val) {
17428
17427
  }
17429
17428
  return false;
17430
17429
  }
17431
- function compressJsonSchema(schema, depth = 0) {
17432
- if (depth > 3) {
17433
- return schema.type || "object";
17434
- }
17435
- const compositionKeys = ["oneOf", "anyOf", "allOf"].filter((key) => Array.isArray(schema[key]));
17436
- if (compositionKeys.length > 0) {
17437
- const compressed2 = {};
17438
- for (const key of compositionKeys) {
17439
- compressed2[key] = schema[key].map((entry) => compressJsonSchema(entry, depth + 1));
17440
- }
17441
- if (typeof schema.type === "string") {
17442
- compressed2.type = schema.type;
17443
- }
17444
- return compressed2;
17445
- }
17446
- if (schema.type !== "object" || !schema.properties) {
17447
- return schema.type || schema;
17448
- }
17449
- const required2 = new Set(Array.isArray(schema.required) ? schema.required : []);
17450
- const compressed = {};
17451
- for (const [key, propSchema] of Object.entries(schema.properties)) {
17452
- const prop = propSchema;
17453
- let value = prop.type || "unknown";
17454
- if (prop.type === "object" && prop.properties) {
17455
- value = compressJsonSchema(prop, depth + 1);
17456
- if (required2.has(key)) {
17457
- compressed[key + " (required)"] = value;
17458
- continue;
17459
- }
17460
- } else if (prop.type === "array" && prop.items) {
17461
- if (prop.items.type === "object" && prop.items.properties) {
17462
- value = [compressJsonSchema(prop.items, depth + 1)];
17463
- } else {
17464
- value = `${prop.items.type || "any"}[]`;
17465
- }
17466
- } else if (prop.enum) {
17467
- value = prop.enum.map((v) => JSON.stringify(v)).join(" | ");
17468
- }
17469
- if (required2.has(key) && typeof value === "string") {
17470
- value += " (required)";
17471
- }
17472
- compressed[key] = value;
17473
- }
17474
- return compressed;
17475
- }
17476
17430
  function deepClean(value, options = DEFAULT_DEEP_CLEAN_OPTIONS) {
17477
17431
  const { keysToStrip, maxDepth, maxStringLength, maxArrayLength, maxObjectKeys } = options;
17478
17432
  const stripSet = keysToStrip instanceof Set ? keysToStrip : new Set(Array.isArray(keysToStrip) ? keysToStrip : Object.keys(keysToStrip));
@@ -17652,11 +17606,7 @@ function deepClean(value, options = DEFAULT_DEEP_CLEAN_OPTIONS) {
17652
17606
  looksLikeJsonSchema = false;
17653
17607
  }
17654
17608
  if (looksLikeJsonSchema) {
17655
- try {
17656
- const compressed = compressJsonSchema(val);
17657
- return compressed === val ? "[JSONSchema]" : helper(compressed, depth);
17658
- } catch {
17659
- }
17609
+ return val;
17660
17610
  }
17661
17611
  const cleaned = {};
17662
17612
  const keys = Object.keys(val).filter((key) => !stripSet.has(key));
@@ -18759,6 +18709,9 @@ function summarizeRequestBody(body) {
18759
18709
  if (Array.isArray(body.messages)) {
18760
18710
  return normalizeMessages(body.messages);
18761
18711
  }
18712
+ if (Array.isArray(body.input)) {
18713
+ return normalizeMessages(body.input);
18714
+ }
18762
18715
  if (Array.isArray(body.contents)) {
18763
18716
  return body.contents.map((item) => ({
18764
18717
  role: typeof item?.role === "string" ? item.role : "user",
@@ -19278,32 +19231,58 @@ var BaseSpan = class {
19278
19231
  parentSpanId;
19279
19232
  /** Deep clean options for serialization */
19280
19233
  deepCleanOptions;
19234
+ /**
19235
+ * Whether this span is filtered out before export. When true, BaseSpan/
19236
+ * DefaultSpan skip attaching attributes/input/output/errorInfo/requestContext
19237
+ * entirely -- they are never read on excluded spans, and skipping avoids
19238
+ * both the deepClean cost and holding references to large payloads for
19239
+ * the lifetime of the span. Set when excludeSpanTypes drops the type,
19240
+ * when the span is internal and includeInternalSpans is false, or when
19241
+ * the subclass is always excluded (e.g., NoOpSpan).
19242
+ *
19243
+ * Note: metadata is still attached and deepCleaned because it is read in
19244
+ * process by getCorrelationContext() and by getLoggerContext() /
19245
+ * getMetricsContext() (which structuredClone it).
19246
+ */
19247
+ isExcluded;
19281
19248
  /** Cached canonical correlation context for this live span */
19282
19249
  correlationContext;
19250
+ /**
19251
+ * Subclasses can override to unconditionally mark the span as excluded.
19252
+ * NoOpSpan uses this because it is never exported regardless of config.
19253
+ */
19254
+ get alwaysExcluded() {
19255
+ return false;
19256
+ }
19283
19257
  constructor(options, observabilityInstance) {
19284
- const serializationOptions = observabilityInstance.getConfig().serializationOptions;
19285
- this.deepCleanOptions = mergeSerializationOptions(serializationOptions);
19258
+ const observabilityConfig = observabilityInstance.getConfig();
19259
+ this.deepCleanOptions = mergeSerializationOptions(observabilityConfig.serializationOptions);
19286
19260
  this.name = options.name;
19287
19261
  this.type = options.type;
19288
- this.attributes = deepClean(options.attributes, this.deepCleanOptions) || {};
19262
+ this.isInternal = isSpanInternal(this.type, options.tracingPolicy?.internal);
19263
+ this.isExcluded = this.alwaysExcluded || observabilityConfig.excludeSpanTypes?.includes(this.type) === true || this.isInternal && !observabilityConfig.includeInternalSpans;
19289
19264
  this.metadata = deepClean(
19290
19265
  options.parent?.metadata || options.metadata ? { ...options.parent?.metadata, ...options.metadata } : void 0,
19291
19266
  this.deepCleanOptions
19292
19267
  );
19293
- if (options.requestContext && options.requestContext.size() > 0) {
19294
- this.requestContext = deepClean(options.requestContext.all, this.deepCleanOptions);
19295
- }
19296
19268
  this.parent = options.parent;
19297
19269
  this.startTime = options.startTime ?? /* @__PURE__ */ new Date();
19298
19270
  this.observabilityInstance = observabilityInstance;
19299
19271
  this.isEvent = options.isEvent ?? false;
19300
- this.isInternal = isSpanInternal(this.type, options.tracingPolicy?.internal);
19301
19272
  this.traceState = options.traceState;
19302
19273
  this.tags = !options.parent && options.tags?.length ? options.tags : void 0;
19303
19274
  const entityParent = this.getParentSpan(false);
19304
19275
  this.entityType = options.entityType ?? entityParent?.entityType;
19305
19276
  this.entityId = options.entityId ?? entityParent?.entityId;
19306
19277
  this.entityName = options.entityName ?? entityParent?.entityName;
19278
+ if (this.isExcluded) {
19279
+ this.attributes = {};
19280
+ return;
19281
+ }
19282
+ this.attributes = deepClean(options.attributes, this.deepCleanOptions) || {};
19283
+ if (options.requestContext && options.requestContext.size() > 0) {
19284
+ this.requestContext = deepClean(options.requestContext.all, this.deepCleanOptions);
19285
+ }
19307
19286
  if (this.isEvent) {
19308
19287
  this.output = deepClean(options.output, this.deepCleanOptions);
19309
19288
  } else {
@@ -19507,46 +19486,51 @@ var DefaultSpan = class extends BaseSpan {
19507
19486
  return;
19508
19487
  }
19509
19488
  this.endTime = /* @__PURE__ */ new Date();
19489
+ if (options?.metadata) {
19490
+ this.metadata = { ...this.metadata, ...deepClean(options.metadata, this.deepCleanOptions) };
19491
+ }
19492
+ if (this.isExcluded) {
19493
+ return;
19494
+ }
19510
19495
  if (options?.output !== void 0) {
19511
19496
  this.output = deepClean(options.output, this.deepCleanOptions);
19512
19497
  }
19513
19498
  if (options?.attributes) {
19514
19499
  this.attributes = { ...this.attributes, ...deepClean(options.attributes, this.deepCleanOptions) };
19515
19500
  }
19516
- if (options?.metadata) {
19517
- this.metadata = { ...this.metadata, ...deepClean(options.metadata, this.deepCleanOptions) };
19518
- }
19519
19501
  }
19520
19502
  error(options) {
19521
19503
  if (this.isEvent) {
19522
19504
  return;
19523
19505
  }
19524
19506
  const { error: error48, endSpan = true, attributes, metadata } = options;
19525
- this.errorInfo = deepClean(
19526
- error48 instanceof MastraError ? {
19527
- id: error48.id,
19528
- details: error48.details,
19529
- category: error48.category,
19530
- domain: error48.domain,
19531
- message: error48.message,
19532
- name: error48.name,
19533
- // Prefer the original cause's stack when available. MastraError wraps
19534
- // thrown errors, so its own stack points to the wrapping site rather
19535
- // than where the underlying error was thrown.
19536
- stack: error48.cause instanceof Error && error48.cause.stack || error48.stack
19537
- } : {
19538
- message: error48.message,
19539
- name: error48.name,
19540
- stack: error48.stack
19541
- },
19542
- this.deepCleanOptions
19543
- );
19544
- if (attributes) {
19545
- this.attributes = { ...this.attributes, ...deepClean(attributes, this.deepCleanOptions) };
19546
- }
19547
19507
  if (metadata) {
19548
19508
  this.metadata = { ...this.metadata, ...deepClean(metadata, this.deepCleanOptions) };
19549
19509
  }
19510
+ if (!this.isExcluded) {
19511
+ this.errorInfo = deepClean(
19512
+ error48 instanceof MastraError ? {
19513
+ id: error48.id,
19514
+ details: error48.details,
19515
+ category: error48.category,
19516
+ domain: error48.domain,
19517
+ message: error48.message,
19518
+ name: error48.name,
19519
+ // Prefer the original cause's stack when available. MastraError wraps
19520
+ // thrown errors, so its own stack points to the wrapping site rather
19521
+ // than where the underlying error was thrown.
19522
+ stack: error48.cause instanceof Error && error48.cause.stack || error48.stack
19523
+ } : {
19524
+ message: error48.message,
19525
+ name: error48.name,
19526
+ stack: error48.stack
19527
+ },
19528
+ this.deepCleanOptions
19529
+ );
19530
+ if (attributes) {
19531
+ this.attributes = { ...this.attributes, ...deepClean(attributes, this.deepCleanOptions) };
19532
+ }
19533
+ }
19550
19534
  if (endSpan) {
19551
19535
  this.end();
19552
19536
  } else {
@@ -19560,6 +19544,12 @@ var DefaultSpan = class extends BaseSpan {
19560
19544
  if (options.name !== void 0) {
19561
19545
  this.name = options.name;
19562
19546
  }
19547
+ if (options.metadata) {
19548
+ this.metadata = { ...this.metadata, ...deepClean(options.metadata, this.deepCleanOptions) };
19549
+ }
19550
+ if (this.isExcluded) {
19551
+ return;
19552
+ }
19563
19553
  if (options.input !== void 0) {
19564
19554
  this.input = deepClean(options.input, this.deepCleanOptions);
19565
19555
  }
@@ -19569,9 +19559,6 @@ var DefaultSpan = class extends BaseSpan {
19569
19559
  if (options.attributes) {
19570
19560
  this.attributes = { ...this.attributes, ...deepClean(options.attributes, this.deepCleanOptions) };
19571
19561
  }
19572
- if (options.metadata) {
19573
- this.metadata = { ...this.metadata, ...deepClean(options.metadata, this.deepCleanOptions) };
19574
- }
19575
19562
  }
19576
19563
  get isValid() {
19577
19564
  return true;
@@ -19647,6 +19634,10 @@ var NoOpSpan = class extends BaseSpan {
19647
19634
  get isValid() {
19648
19635
  return false;
19649
19636
  }
19637
+ // NoOpSpan is never exported, so treat it as always excluded.
19638
+ get alwaysExcluded() {
19639
+ return true;
19640
+ }
19650
19641
  };
19651
19642
 
19652
19643
  // src/instances/base.ts