@mastra/observability 1.21.0-alpha.2 → 1.21.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
@@ -51,9 +51,7 @@ function routeToHandler(handler, event, logger) {
51
51
  }), handler.name, "score", logger);
52
52
  }
53
53
  break;
54
- case "feedback":
55
- if (handler.onFeedbackEvent) return catchAsyncResult(handler.onFeedbackEvent(event), handler.name, "feedback", logger);
56
- break;
54
+ case "feedback": if (handler.onFeedbackEvent) return catchAsyncResult(handler.onFeedbackEvent(event), handler.name, "feedback", logger);
57
55
  }
58
56
  } catch (err) {
59
57
  logger.error(`[Observability] Handler error [handler=${handler.name}]:`, err);
@@ -202,7 +200,7 @@ const VALUE_STRIP_KEYS = /* @__PURE__ */ new Set([
202
200
  const DEFAULT_DEEP_CLEAN_OPTIONS = Object.freeze({
203
201
  keysToStrip: [],
204
202
  maxDepth: 8,
205
- maxStringLength: 128 * 1024,
203
+ maxStringLength: 131072,
206
204
  maxArrayLength: 50,
207
205
  maxObjectKeys: 50
208
206
  });
@@ -1276,8 +1274,8 @@ function isRecord(value) {
1276
1274
  //#endregion
1277
1275
  //#region src/metrics/pricing-registry.ts
1278
1276
  const DATA_FILE_NAME = "pricing-data.json";
1279
- const REFRESH_INTERVAL_MS = 1440 * 60 * 1e3;
1280
- const MIN_REFRESH_ATTEMPT_INTERVAL_MS = 3600 * 1e3;
1277
+ const REFRESH_INTERVAL_MS = 864e5;
1278
+ const MIN_REFRESH_ATTEMPT_INTERVAL_MS = 36e5;
1281
1279
  const REFRESH_TIMEOUT_MS = 5e3;
1282
1280
  const CACHE_FILE = () => path.join(os.homedir(), ".cache", "mastra", "pricing-data.json");
1283
1281
  const CACHE_VERSION = 1;
@@ -2936,9 +2934,7 @@ var ModelSpanTracker = class {
2936
2934
  if (this.#currentChunkType !== "text") this.#startChunkSpan("text");
2937
2935
  this.#appendToAccumulator("text", chunk.payload.text);
2938
2936
  break;
2939
- case "text-end":
2940
- this.#endChunkSpan();
2941
- break;
2937
+ case "text-end": this.#endChunkSpan();
2942
2938
  }
2943
2939
  }
2944
2940
  /**
@@ -2953,9 +2949,7 @@ var ModelSpanTracker = class {
2953
2949
  if (this.#currentChunkType !== "reasoning") this.#startChunkSpan("reasoning");
2954
2950
  this.#appendToAccumulator("text", chunk.payload.text);
2955
2951
  break;
2956
- case "reasoning-end":
2957
- this.#endChunkSpan();
2958
- break;
2952
+ case "reasoning-end": this.#endChunkSpan();
2959
2953
  }
2960
2954
  }
2961
2955
  /**
@@ -2998,9 +2992,7 @@ var ModelSpanTracker = class {
2998
2992
  case "object":
2999
2993
  if (this.#currentChunkType !== "object") this.#startChunkSpan("object");
3000
2994
  break;
3001
- case "object-result":
3002
- this.#endChunkSpan(chunk.object);
3003
- break;
2995
+ case "object-result": this.#endChunkSpan(chunk.object);
3004
2996
  }
3005
2997
  }
3006
2998
  /**
@@ -3103,7 +3095,6 @@ var ModelSpanTracker = class {
3103
3095
  this.#createEventSpan(chunk.type, spanOutput, { metadata });
3104
3096
  break;
3105
3097
  }
3106
- default: break;
3107
3098
  }
3108
3099
  } }));
3109
3100
  }
@@ -3644,10 +3635,14 @@ var DefaultSpan = class extends BaseSpan {
3644
3635
  }
3645
3636
  this.traceId = getOrCreateTraceId(options);
3646
3637
  this.id = generateSpanId();
3647
- if (options.parentSpanId) if (isValidSpanId(options.parentSpanId)) this.parentSpanId = options.parentSpanId;
3648
- else console.error(`[Mastra Tracing] Invalid parentSpanId: must be 1-16 hexadecimal characters, got "${options.parentSpanId}". Ignoring.`);
3649
- if (options.externalParentSpanId) if (isValidSpanId(options.externalParentSpanId)) this.externalParentSpanId = options.externalParentSpanId;
3650
- else console.error(`[Mastra Tracing] Invalid externalParentSpanId: must be 1-16 hexadecimal characters, got "${options.externalParentSpanId}". Ignoring.`);
3638
+ if (options.parentSpanId) {
3639
+ if (isValidSpanId(options.parentSpanId)) this.parentSpanId = options.parentSpanId;
3640
+ else console.error(`[Mastra Tracing] Invalid parentSpanId: must be 1-16 hexadecimal characters, got "${options.parentSpanId}". Ignoring.`);
3641
+ }
3642
+ if (options.externalParentSpanId) {
3643
+ if (isValidSpanId(options.externalParentSpanId)) this.externalParentSpanId = options.externalParentSpanId;
3644
+ else console.error(`[Mastra Tracing] Invalid externalParentSpanId: must be 1-16 hexadecimal characters, got "${options.externalParentSpanId}". Ignoring.`);
3645
+ }
3651
3646
  }
3652
3647
  end(options) {
3653
3648
  if (this.isEvent) return;
@@ -3769,8 +3764,10 @@ function isValidSpanId(spanId) {
3769
3764
  return /^[0-9a-f]{1,16}$/i.test(spanId);
3770
3765
  }
3771
3766
  function getOrCreateTraceId(options) {
3772
- if (options.traceId) if (isValidTraceId(options.traceId)) return options.traceId;
3773
- else console.error(`[Mastra Tracing] Invalid traceId: must be 1-32 hexadecimal characters, got "${options.traceId}". Generating new trace ID.`);
3767
+ if (options.traceId) {
3768
+ if (isValidTraceId(options.traceId)) return options.traceId;
3769
+ else console.error(`[Mastra Tracing] Invalid traceId: must be 1-32 hexadecimal characters, got "${options.traceId}". Generating new trace ID.`);
3770
+ }
3774
3771
  return generateTraceId();
3775
3772
  }
3776
3773
  //#endregion
@@ -4019,17 +4016,13 @@ var EventBuffer = class {
4019
4016
  switch (this.#storageStrategy) {
4020
4017
  case "realtime":
4021
4018
  case "event-sourced":
4022
- case "batch-with-updates":
4023
- this.pushCreate(event);
4024
- break;
4019
+ case "batch-with-updates": this.pushCreate(event);
4025
4020
  }
4026
4021
  break;
4027
4022
  case TracingEventType.SPAN_UPDATED:
4028
4023
  switch (this.#storageStrategy) {
4029
4024
  case "realtime":
4030
- case "batch-with-updates":
4031
- this.pushUpdate(event);
4032
- break;
4025
+ case "batch-with-updates": this.pushUpdate(event);
4033
4026
  }
4034
4027
  break;
4035
4028
  case TracingEventType.SPAN_ENDED:
@@ -4039,14 +4032,10 @@ var EventBuffer = class {
4039
4032
  case "batch-with-updates":
4040
4033
  this.pushUpdate(event);
4041
4034
  break;
4042
- default:
4043
- this.pushCreate(event);
4044
- break;
4035
+ default: this.pushCreate(event);
4045
4036
  }
4046
4037
  break;
4047
- default:
4048
- this.pushCreate(event);
4049
- break;
4038
+ default: this.pushCreate(event);
4050
4039
  }
4051
4040
  }
4052
4041
  /** Re-add failed create events to the buffer, returning events that exceed max retries. */
@@ -4421,17 +4410,13 @@ var DefaultExporter = class extends BaseExporter {
4421
4410
  case "score":
4422
4411
  createScoreEvents.push(createEvent);
4423
4412
  break;
4424
- default:
4425
- createSpanEvents.push(createEvent);
4426
- break;
4413
+ default: createSpanEvents.push(createEvent);
4427
4414
  }
4428
4415
  for (const updateEvent of updates) switch (updateEvent.type) {
4429
4416
  case TracingEventType.SPAN_UPDATED:
4430
4417
  updateSpanEvents.push(updateEvent);
4431
4418
  break;
4432
- case TracingEventType.SPAN_ENDED:
4433
- endSpanEvents.push(updateEvent);
4434
- break;
4419
+ case TracingEventType.SPAN_ENDED: endSpanEvents.push(updateEvent);
4435
4420
  }
4436
4421
  const flushResults = await Promise.all([
4437
4422
  this.flushCreates("feedback", createFeedbackEvents, (events) => this.#observabilityStorage.batchCreateFeedback({ feedbacks: events.map((f) => buildFeedbackRecord(f)) })),
@@ -4448,13 +4433,15 @@ var DefaultExporter = class extends BaseExporter {
4448
4433
  flushResults.push(await this.flushSpanUpdates(updateSpanEvents, deferredUpdates, false));
4449
4434
  flushResults.push(await this.flushSpanUpdates(endSpanEvents, deferredUpdates, true));
4450
4435
  let deferredDropCount = 0;
4451
- if (deferredUpdates.length > 0) if (this.#unsupportedSignals.has("tracing")) {
4452
- deferredDropCount = deferredUpdates.length;
4453
- this.emitDrop("tracing", "unsupported-storage", deferredDropCount);
4454
- deferredUpdates.length = 0;
4455
- } else {
4456
- deferredDropCount = this.#eventBuffer.reAddUpdates(deferredUpdates).length;
4457
- this.emitDrop("tracing", "retry-exhausted", deferredDropCount);
4436
+ if (deferredUpdates.length > 0) {
4437
+ if (this.#unsupportedSignals.has("tracing")) {
4438
+ deferredDropCount = deferredUpdates.length;
4439
+ this.emitDrop("tracing", "unsupported-storage", deferredDropCount);
4440
+ deferredUpdates.length = 0;
4441
+ } else {
4442
+ deferredDropCount = this.#eventBuffer.reAddUpdates(deferredUpdates).length;
4443
+ this.emitDrop("tracing", "retry-exhausted", deferredDropCount);
4444
+ }
4458
4445
  }
4459
4446
  const retryAttempt = flushResults.reduce((earliestAttempt, result) => {
4460
4447
  if (result.retryAttempt === void 0) return earliestAttempt;
@@ -4872,17 +4859,13 @@ var MastraStorageExporter = class extends BaseExporter {
4872
4859
  case "score":
4873
4860
  createScoreEvents.push(createEvent);
4874
4861
  break;
4875
- default:
4876
- createSpanEvents.push(createEvent);
4877
- break;
4862
+ default: createSpanEvents.push(createEvent);
4878
4863
  }
4879
4864
  for (const updateEvent of updates) switch (updateEvent.type) {
4880
4865
  case TracingEventType.SPAN_UPDATED:
4881
4866
  updateSpanEvents.push(updateEvent);
4882
4867
  break;
4883
- case TracingEventType.SPAN_ENDED:
4884
- endSpanEvents.push(updateEvent);
4885
- break;
4868
+ case TracingEventType.SPAN_ENDED: endSpanEvents.push(updateEvent);
4886
4869
  }
4887
4870
  const flushResults = await Promise.all([
4888
4871
  this.flushCreates("feedback", createFeedbackEvents, (events) => this.#observabilityStorage.batchCreateFeedback({ feedbacks: events.map((f) => buildFeedbackRecord(f)) })),
@@ -4899,13 +4882,15 @@ var MastraStorageExporter = class extends BaseExporter {
4899
4882
  flushResults.push(await this.flushSpanUpdates(updateSpanEvents, deferredUpdates, false));
4900
4883
  flushResults.push(await this.flushSpanUpdates(endSpanEvents, deferredUpdates, true));
4901
4884
  let deferredDropCount = 0;
4902
- if (deferredUpdates.length > 0) if (this.#unsupportedSignals.has("tracing")) {
4903
- deferredDropCount = deferredUpdates.length;
4904
- this.emitDrop("tracing", "unsupported-storage", deferredDropCount);
4905
- deferredUpdates.length = 0;
4906
- } else {
4907
- deferredDropCount = this.#eventBuffer.reAddUpdates(deferredUpdates).length;
4908
- this.emitDrop("tracing", "retry-exhausted", deferredDropCount);
4885
+ if (deferredUpdates.length > 0) {
4886
+ if (this.#unsupportedSignals.has("tracing")) {
4887
+ deferredDropCount = deferredUpdates.length;
4888
+ this.emitDrop("tracing", "unsupported-storage", deferredDropCount);
4889
+ deferredUpdates.length = 0;
4890
+ } else {
4891
+ deferredDropCount = this.#eventBuffer.reAddUpdates(deferredUpdates).length;
4892
+ this.emitDrop("tracing", "retry-exhausted", deferredDropCount);
4893
+ }
4909
4894
  }
4910
4895
  const retryAttempt = flushResults.reduce((earliestAttempt, result) => {
4911
4896
  if (result.retryAttempt === void 0) return earliestAttempt;
@@ -5425,10 +5410,13 @@ var BaseObservabilityInstance = class extends MastraBase {
5425
5410
  const parts = key.split(".");
5426
5411
  const rootKey = parts[0];
5427
5412
  const value = requestContext.get(rootKey);
5428
- if (value !== void 0) if (parts.length > 1) {
5429
- const nestedValue = getNestedValue(value, parts.slice(1).join("."));
5430
- if (nestedValue !== void 0) setNestedValue(result, key, nestedValue);
5431
- } else setNestedValue(result, key, value);
5413
+ if (value !== void 0) {
5414
+ if (parts.length > 1) {
5415
+ const nestedPath = parts.slice(1).join(".");
5416
+ const nestedValue = getNestedValue(value, nestedPath);
5417
+ if (nestedValue !== void 0) setNestedValue(result, key, nestedValue);
5418
+ } else setNestedValue(result, key, value);
5419
+ }
5432
5420
  }
5433
5421
  return result;
5434
5422
  }
@@ -5976,7 +5964,7 @@ function formatBaggage(entries) {
5976
5964
  const DEFAULT_LIMITS = {
5977
5965
  maxSpans: 10,
5978
5966
  maxLogs: 10,
5979
- maxPayloadBytes: 1024 * 1024
5967
+ maxPayloadBytes: 1048576
5980
5968
  };
5981
5969
  var ClientObservabilityProxyImpl = class {
5982
5970
  #resolveInstance;
@@ -6911,36 +6899,38 @@ var TrackingExporter = class extends BaseExporter {
6911
6899
  method
6912
6900
  });
6913
6901
  const { exportedSpan } = await this._preExportTracingEvent(event);
6914
- if (!this.skipBuildRootTask && !traceData.hasRoot()) if (exportedSpan.isRootSpan) {
6915
- this.logger.debug(`${this.name}: Building root`, {
6916
- traceId: exportedSpan.traceId,
6917
- spanId: exportedSpan.id
6918
- });
6919
- const rootData = await this._buildRoot({
6920
- span: exportedSpan,
6921
- traceData
6922
- });
6923
- if (rootData) {
6924
- this.logger.debug(`${this.name}: Adding root`, {
6902
+ if (!this.skipBuildRootTask && !traceData.hasRoot()) {
6903
+ if (exportedSpan.isRootSpan) {
6904
+ this.logger.debug(`${this.name}: Building root`, {
6925
6905
  traceId: exportedSpan.traceId,
6926
6906
  spanId: exportedSpan.id
6927
6907
  });
6928
- traceData.addRoot({
6929
- rootId: exportedSpan.id,
6930
- rootData
6908
+ const rootData = await this._buildRoot({
6909
+ span: exportedSpan,
6910
+ traceData
6931
6911
  });
6932
- this.#scheduleProcessWaitingForRoot(traceId);
6912
+ if (rootData) {
6913
+ this.logger.debug(`${this.name}: Adding root`, {
6914
+ traceId: exportedSpan.traceId,
6915
+ spanId: exportedSpan.id
6916
+ });
6917
+ traceData.addRoot({
6918
+ rootId: exportedSpan.id,
6919
+ rootData
6920
+ });
6921
+ this.#scheduleProcessWaitingForRoot(traceId);
6922
+ }
6923
+ } else {
6924
+ this.logger.debug(`${this.name}: Root does not exist, adding span to waiting queue.`, {
6925
+ traceId: exportedSpan.traceId,
6926
+ spanId: exportedSpan.id
6927
+ });
6928
+ traceData.addToWaitingQueue({
6929
+ event,
6930
+ waitingFor: "root"
6931
+ });
6932
+ return;
6933
6933
  }
6934
- } else {
6935
- this.logger.debug(`${this.name}: Root does not exist, adding span to waiting queue.`, {
6936
- traceId: exportedSpan.traceId,
6937
- spanId: exportedSpan.id
6938
- });
6939
- traceData.addToWaitingQueue({
6940
- event,
6941
- waitingFor: "root"
6942
- });
6943
- return;
6944
6934
  }
6945
6935
  if (exportedSpan.metadata && this.name in exportedSpan.metadata) {
6946
6936
  const metadata = exportedSpan.metadata[this.name];
@@ -7067,7 +7057,6 @@ var TrackingExporter = class extends BaseExporter {
7067
7057
  traceData
7068
7058
  });
7069
7059
  if (traceData.activeSpanCount() === 0) this.#scheduleCleanup(traceId);
7070
- break;
7071
7060
  }
7072
7061
  } catch (error) {
7073
7062
  this.logger.error(`${this.name}: exporter error`, {
@@ -7233,7 +7222,7 @@ function chainFormatters(formatters) {
7233
7222
  //#region src/exporters/auth-failure-cooldown.ts
7234
7223
  const AUTH_FAILURE_STATUSES = /* @__PURE__ */ new Set([401, 403]);
7235
7224
  const AUTH_COOLDOWN_BASE_MS = 6e4;
7236
- const AUTH_COOLDOWN_MAX_MS = 15 * 6e4;
7225
+ const AUTH_COOLDOWN_MAX_MS = 9e5;
7237
7226
  const AUTH_COOLDOWN_JITTER_RATIO = .1;
7238
7227
  var AuthFailureError = class extends Error {
7239
7228
  status;
@@ -7662,8 +7651,9 @@ var CloudExporter = class extends BaseExporter {
7662
7651
  succeeded: false,
7663
7652
  authFailureStatus: error.status
7664
7653
  };
7654
+ const errorId = `CLOUD_EXPORTER_FAILED_TO_BATCH_UPLOAD_${signal.toUpperCase()}`;
7665
7655
  const mastraError = new MastraError({
7666
- id: `CLOUD_EXPORTER_FAILED_TO_BATCH_UPLOAD_${signal.toUpperCase()}`,
7656
+ id: errorId,
7667
7657
  domain: ErrorDomain.MASTRA_OBSERVABILITY,
7668
7658
  category: ErrorCategory.USER,
7669
7659
  details: {
@@ -8252,8 +8242,9 @@ var MastraPlatformExporter = class extends BaseExporter {
8252
8242
  succeeded: false,
8253
8243
  authFailureStatus: error.status
8254
8244
  };
8245
+ const errorId = `MASTRA_PLATFORM_EXPORTER_FAILED_TO_BATCH_UPLOAD_${signal.toUpperCase()}`;
8255
8246
  const mastraError = new MastraError({
8256
- id: `MASTRA_PLATFORM_EXPORTER_FAILED_TO_BATCH_UPLOAD_${signal.toUpperCase()}`,
8247
+ id: errorId,
8257
8248
  domain: ErrorDomain.MASTRA_OBSERVABILITY,
8258
8249
  category: ErrorCategory.USER,
8259
8250
  details: {
@@ -9117,16 +9108,18 @@ var TestExporter = class extends BaseExporter {
9117
9108
  for (let i = 0; i < maxLen; i++) {
9118
9109
  const actualLine = actual[i];
9119
9110
  const expectedLine = expected[i];
9120
- if (actualLine !== expectedLine) if (actualLine === void 0) {
9121
- diffs.push(`Line ${i + 1}: Missing in actual`);
9122
- diffs.push(` Expected: ${expectedLine}`);
9123
- } else if (expectedLine === void 0) {
9124
- diffs.push(`Line ${i + 1}: Extra in actual`);
9125
- diffs.push(` Actual: ${actualLine}`);
9126
- } else {
9127
- diffs.push(`Line ${i + 1}:`);
9128
- diffs.push(` Expected: ${expectedLine}`);
9129
- diffs.push(` Actual: ${actualLine}`);
9111
+ if (actualLine !== expectedLine) {
9112
+ if (actualLine === void 0) {
9113
+ diffs.push(`Line ${i + 1}: Missing in actual`);
9114
+ diffs.push(` Expected: ${expectedLine}`);
9115
+ } else if (expectedLine === void 0) {
9116
+ diffs.push(`Line ${i + 1}: Extra in actual`);
9117
+ diffs.push(` Actual: ${actualLine}`);
9118
+ } else {
9119
+ diffs.push(`Line ${i + 1}:`);
9120
+ diffs.push(` Expected: ${expectedLine}`);
9121
+ diffs.push(` Actual: ${actualLine}`);
9122
+ }
9130
9123
  }
9131
9124
  }
9132
9125
  return diffs;
@@ -9889,9 +9882,10 @@ var SensitiveDataFilter = class {
9889
9882
  const filtered = {};
9890
9883
  for (const key of Object.keys(obj)) {
9891
9884
  const normKey = this.normalizeKey(key);
9892
- if (this.isSensitive(normKey)) if (obj[key] && typeof obj[key] === "object") filtered[key] = this.deepFilter(obj[key], seen, indexedState);
9893
- else filtered[key] = this.redactValue(obj[key], normKey, indexedState);
9894
- else filtered[key] = this.deepFilter(obj[key], seen, indexedState);
9885
+ if (this.isSensitive(normKey)) {
9886
+ if (obj[key] && typeof obj[key] === "object") filtered[key] = this.deepFilter(obj[key], seen, indexedState);
9887
+ else filtered[key] = this.redactValue(obj[key], normKey, indexedState);
9888
+ } else filtered[key] = this.deepFilter(obj[key], seen, indexedState);
9895
9889
  }
9896
9890
  return filtered;
9897
9891
  }