@mastra/observability 1.16.0 → 1.16.1-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/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @mastra/observability
2
2
 
3
+ ## 1.16.1-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed cost estimation for Amazon Bedrock inference profiles, including geographic prefixes and versioned model IDs. ([#19360](https://github.com/mastra-ai/mastra/pull/19360))
8
+
9
+ - Added PROVIDER_TOOL_CALL to exporter span-type mappings and duration metrics so provider-executed tool spans are classified as tool spans across all observability platforms. ([#19261](https://github.com/mastra-ai/mastra/pull/19261))
10
+
11
+ - Updated dependencies [[`25e7c12`](https://github.com/mastra-ai/mastra/commit/25e7c126a770069ae7fb7ecf1d2adb40e017b009), [`1ce5121`](https://github.com/mastra-ai/mastra/commit/1ce512155d122bb21f47d98383e82ffbf84b39e8), [`3cfc47a`](https://github.com/mastra-ai/mastra/commit/3cfc47a6b89940aadd0f46fb01ae9624a73a865d), [`2bb7817`](https://github.com/mastra-ai/mastra/commit/2bb78176112fde628483de2830528f7eee911e56), [`51d9870`](https://github.com/mastra-ai/mastra/commit/51d987032c689c2855374d0f244f5d654da809d1), [`5cab274`](https://github.com/mastra-ai/mastra/commit/5cab2744250e22d12fefa7b32637dce224233cee), [`7fa27d3`](https://github.com/mastra-ai/mastra/commit/7fa27d3b6f5ed68cd34e454a4d3ad9c482a0cfbc), [`a58dcbb`](https://github.com/mastra-ai/mastra/commit/a58dcbb546d7e1d65ebdc1f39e55f0908fcd9391), [`153bd3b`](https://github.com/mastra-ai/mastra/commit/153bd3b396bdfed6b74cf43de12db8fd2d83c04a), [`07bb863`](https://github.com/mastra-ai/mastra/commit/07bb8631919c6f7cf377dccd45b096e0f17fbed0), [`8a586ec`](https://github.com/mastra-ai/mastra/commit/8a586eca9a4914f31dff6140d0d45ac375b00669), [`3927473`](https://github.com/mastra-ai/mastra/commit/392747323ddb10c643d12be7b9ae913159dfaeed), [`dce50dc`](https://github.com/mastra-ai/mastra/commit/dce50dc9a1c1fcd0f427bb5f6250ec74910cb04b), [`634caff`](https://github.com/mastra-ai/mastra/commit/634caff29a9200ad058b67d53f96d9e5832fb8a2)]:
12
+ - @mastra/core@1.51.0-alpha.7
13
+
14
+ ## 1.16.1-alpha.0
15
+
16
+ ### Patch Changes
17
+
18
+ - Fixed storage exporters to report persistence failures immediately and retry failed batches automatically with the configured exponential backoff. ([#19259](https://github.com/mastra-ai/mastra/pull/19259))
19
+
20
+ - Updated dependencies [[`fb8aea3`](https://github.com/mastra-ai/mastra/commit/fb8aea384291e77311be3a64ee1717320d5c3c73), [`4ce0163`](https://github.com/mastra-ai/mastra/commit/4ce0163dc86e675a86809685c8ce6c49f1aeb87e)]:
21
+ - @mastra/core@1.51.0-alpha.5
22
+
3
23
  ## 1.16.0
4
24
 
5
25
  ### Minor Changes
@@ -42,6 +42,9 @@ export declare class DefaultExporter extends BaseExporter {
42
42
  * Schedules a flush using setTimeout
43
43
  */
44
44
  private scheduleFlush;
45
+ private retryDelay;
46
+ private retryAttempt;
47
+ private logStorageFailure;
45
48
  /**
46
49
  * Checks flush triggers and schedules/triggers flush as needed.
47
50
  * Called after adding any event to the buffer.
@@ -1 +1 @@
1
- {"version":3,"file":"default.d.ts","sourceRoot":"","sources":["../../src/exporters/default.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACX,QAAQ,EACR,UAAU,EACV,aAAa,EAId,MAAM,4BAA4B,CAAC;AAEpC,OAAO,KAAK,EAAwB,sBAAsB,EAAwB,MAAM,sBAAsB,CAAC;AAS/G,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAItC;;;;;;GAMG;AACH,UAAU,qBAAsB,SAAQ,kBAAkB;IACxD,YAAY,CA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AW;IAOzB;;OAEG;IACG,aAAa,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAQtD;;OAEG;IACG,UAAU,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAQhD;;OAEG;IACG,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAQpD;;OAEG;IACG,eAAe,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ1D;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAStB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAiBhC"}
@@ -31,6 +31,9 @@ export declare class MastraStorageExporter extends BaseExporter {
31
31
  * Schedules a flush using setTimeout
32
32
  */
33
33
  private scheduleFlush;
34
+ private retryDelay;
35
+ private retryAttempt;
36
+ private logStorageFailure;
34
37
  /**
35
38
  * Checks flush triggers and schedules/triggers flush as needed.
36
39
  * Called after adding any event to the buffer.
@@ -1 +1 @@
1
- {"version":3,"file":"mastra-storage.d.ts","sourceRoot":"","sources":["../../src/exporters/mastra-storage.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACX,QAAQ,EACR,UAAU,EACV,aAAa,EAId,MAAM,4BAA4B,CAAC;AAEpC,OAAO,KAAK,EAAwB,sBAAsB,EAAwB,MAAM,sBAAsB,CAAC;AAS/G,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAItC,4FAA4F;AAC5F,MAAM,WAAW,2BAA4B,SAAQ,kBAAkB;IACrE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;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,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAQhD;;OAEG;IACG,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAQpD;;OAEG;IACG,eAAe,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ1D;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAStB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAiBhC"}
package/dist/index.cjs CHANGED
@@ -1006,6 +1006,7 @@ var PricingModel = class {
1006
1006
 
1007
1007
  // src/metrics/pricing-registry.ts
1008
1008
  var DATA_FILE_NAME = "pricing-data.jsonl";
1009
+ var BEDROCK_GEOGRAPHY_PREFIXES = /* @__PURE__ */ new Set(["global", "us", "eu", "apac", "jp", "au"]);
1009
1010
  var MINIFIED_METER_TO_CANONICAL = {
1010
1011
  it: "input_tokens",
1011
1012
  ot: "output_tokens",
@@ -1040,7 +1041,7 @@ var PricingRegistry = class _PricingRegistry {
1040
1041
  return _PricingRegistry.globalRegistry;
1041
1042
  }
1042
1043
  get(args) {
1043
- const variants = getModelVariants(args.model);
1044
+ const variants = getModelVariants(args.model, normalizeProvider(args.provider));
1044
1045
  for (const variant of variants) {
1045
1046
  const key = makePricingKey({ provider: args.provider, model: variant });
1046
1047
  const match = this.pricingModels.get(key);
@@ -1134,7 +1135,7 @@ function normalizeProvider(provider) {
1134
1135
  const dotIndex = normalized.indexOf(".");
1135
1136
  return dotIndex !== -1 ? normalized.substring(0, dotIndex) : normalized;
1136
1137
  }
1137
- function getModelVariants(model) {
1138
+ function getModelVariants(model, provider) {
1138
1139
  const variants = /* @__PURE__ */ new Set();
1139
1140
  const add = (v) => {
1140
1141
  variants.add(v);
@@ -1149,6 +1150,22 @@ function getModelVariants(model) {
1149
1150
  add(withoutVendor);
1150
1151
  add(withoutVendor.replace(/\./g, "-"));
1151
1152
  }
1153
+ if (provider === "amazon-bedrock") {
1154
+ const addBedrockVariant = (v) => {
1155
+ add(v);
1156
+ add(v.replace(/-(?:v)?\d+(?::\d+)?$/, ""));
1157
+ };
1158
+ const segments = model.split(".");
1159
+ const withoutGeography = BEDROCK_GEOGRAPHY_PREFIXES.has(segments[0] ?? "") ? segments.slice(1).join(".") : model;
1160
+ addBedrockVariant(withoutGeography);
1161
+ addBedrockVariant(withoutGeography.replace(/\./g, "-"));
1162
+ const vendorSeparator = withoutGeography.indexOf(".");
1163
+ if (vendorSeparator !== -1) {
1164
+ const withoutVendor = withoutGeography.substring(vendorSeparator + 1);
1165
+ addBedrockVariant(withoutVendor);
1166
+ addBedrockVariant(withoutVendor.replace(/\./g, "-"));
1167
+ }
1168
+ }
1152
1169
  return [...variants];
1153
1170
  }
1154
1171
  function stripDateSuffix(model) {
@@ -1513,6 +1530,7 @@ function getDurationMetricName(span) {
1513
1530
  return "mastra_agent_duration_ms";
1514
1531
  case observability.SpanType.TOOL_CALL:
1515
1532
  case observability.SpanType.MCP_TOOL_CALL:
1533
+ case observability.SpanType.PROVIDER_TOOL_CALL:
1516
1534
  return "mastra_tool_duration_ms";
1517
1535
  case observability.SpanType.CLIENT_TOOL_CALL:
1518
1536
  return null;
@@ -2472,6 +2490,7 @@ function isSpanInternal(spanType, flags) {
2472
2490
  // Tool-related spans
2473
2491
  case observability.SpanType.TOOL_CALL:
2474
2492
  case observability.SpanType.MCP_TOOL_CALL:
2493
+ case observability.SpanType.PROVIDER_TOOL_CALL:
2475
2494
  return (flags & observability.InternalSpans.TOOL) !== 0;
2476
2495
  // Model-related spans
2477
2496
  case observability.SpanType.MODEL_GENERATION:
@@ -5954,6 +5973,7 @@ function resolveTracingStorageStrategy(config, observabilityStorage, storageName
5954
5973
  }
5955
5974
  return observabilityStrategy.preferred;
5956
5975
  }
5976
+ var FLUSH_SUCCEEDED = { failed: false };
5957
5977
  var DefaultExporter = class extends BaseExporter {
5958
5978
  name = "mastra-default-observability-exporter";
5959
5979
  #config;
@@ -6048,17 +6068,37 @@ var DefaultExporter = class extends BaseExporter {
6048
6068
  /**
6049
6069
  * Schedules a flush using setTimeout
6050
6070
  */
6051
- scheduleFlush() {
6071
+ scheduleFlush(delayMs = this.#config.maxBatchWaitMs) {
6052
6072
  if (this.#flushTimer) {
6053
6073
  clearTimeout(this.#flushTimer);
6054
6074
  }
6055
6075
  this.#flushTimer = setTimeout(() => {
6076
+ this.#flushTimer = void 0;
6056
6077
  this.flushBuffer().catch((error) => {
6057
6078
  this.logger.error("Scheduled flush failed", {
6058
6079
  error: error instanceof Error ? error.message : String(error)
6059
6080
  });
6060
6081
  });
6061
- }, this.#config.maxBatchWaitMs);
6082
+ }, delayMs);
6083
+ }
6084
+ retryDelay(retryAttempt) {
6085
+ return this.#config.retryDelayMs * 2 ** (retryAttempt - 1);
6086
+ }
6087
+ retryAttempt(events) {
6088
+ return events.reduce((earliestAttempt, event) => {
6089
+ if (event.retryCount > this.#config.maxRetries) return earliestAttempt;
6090
+ return earliestAttempt === void 0 ? event.retryCount : Math.min(earliestAttempt, event.retryCount);
6091
+ }, void 0);
6092
+ }
6093
+ logStorageFailure(signal, events, retryAttempt, error) {
6094
+ this.logger.warn("Failed to persist observability events", {
6095
+ signal,
6096
+ eventCount: events.length,
6097
+ retryAttempt: retryAttempt ?? events.reduce((latestAttempt, event) => Math.max(latestAttempt, event.retryCount), 0),
6098
+ maxRetries: this.#config.maxRetries,
6099
+ ...retryAttempt === void 0 ? {} : { nextRetryDelayMs: this.retryDelay(retryAttempt) },
6100
+ error: error instanceof Error ? error.message : String(error)
6101
+ });
6062
6102
  }
6063
6103
  /**
6064
6104
  * Checks flush triggers and schedules/triggers flush as needed.
@@ -6105,22 +6145,26 @@ var DefaultExporter = class extends BaseExporter {
6105
6145
  * On other errors, re-adds events to the buffer for retry.
6106
6146
  */
6107
6147
  async flushCreates(signal, events, storageCall) {
6108
- if (events.length === 0) return;
6148
+ if (events.length === 0) return FLUSH_SUCCEEDED;
6109
6149
  if (this.#unsupportedSignals.has(signal)) {
6110
6150
  this.emitDrop(signal, "unsupported-storage", events.length);
6111
- return;
6151
+ return { failed: true };
6112
6152
  }
6113
6153
  try {
6114
6154
  await storageCall(events);
6155
+ return FLUSH_SUCCEEDED;
6115
6156
  } catch (error$1) {
6116
6157
  if (error$1 instanceof error.MastraError && error$1.domain === error.ErrorDomain.MASTRA_OBSERVABILITY && error$1.id.endsWith("_NOT_IMPLEMENTED")) {
6117
6158
  this.logger.warn(error$1.message);
6118
6159
  this.#unsupportedSignals.add(signal);
6119
6160
  this.emitDrop(signal, "unsupported-storage", events.length, error$1);
6120
- } else {
6121
- const dropped = this.#eventBuffer.reAddCreates(events);
6122
- this.emitDrop(signal, "retry-exhausted", dropped.length, error$1);
6161
+ return { failed: true };
6123
6162
  }
6163
+ const dropped = this.#eventBuffer.reAddCreates(events);
6164
+ const retryAttempt = this.retryAttempt(events);
6165
+ this.logStorageFailure(signal, events, retryAttempt, error$1);
6166
+ this.emitDrop(signal, "retry-exhausted", dropped.length, error$1);
6167
+ return { failed: true, retryAttempt };
6124
6168
  }
6125
6169
  }
6126
6170
  /**
@@ -6129,10 +6173,10 @@ var DefaultExporter = class extends BaseExporter {
6129
6173
  */
6130
6174
  async flushSpanUpdates(events, deferredUpdates, isEnd) {
6131
6175
  const deferredCountAtEntry = deferredUpdates.length;
6132
- if (events.length === 0) return;
6176
+ if (events.length === 0) return FLUSH_SUCCEEDED;
6133
6177
  if (this.#unsupportedSignals.has("tracing")) {
6134
6178
  this.emitDrop("tracing", "unsupported-storage", events.length);
6135
- return;
6179
+ return { failed: true };
6136
6180
  }
6137
6181
  const partials = [];
6138
6182
  for (const event of events) {
@@ -6147,26 +6191,30 @@ var DefaultExporter = class extends BaseExporter {
6147
6191
  deferredUpdates.push(event);
6148
6192
  }
6149
6193
  }
6150
- if (partials.length === 0) return;
6194
+ if (partials.length === 0) return FLUSH_SUCCEEDED;
6151
6195
  try {
6152
6196
  await this.#observabilityStorage.batchUpdateSpans({ records: partials });
6153
6197
  if (isEnd) {
6154
6198
  this.#eventBuffer.endFinishedSpans({ records: partials });
6155
6199
  }
6200
+ return FLUSH_SUCCEEDED;
6156
6201
  } catch (error$1) {
6157
6202
  if (error$1 instanceof error.MastraError && error$1.domain === error.ErrorDomain.MASTRA_OBSERVABILITY && error$1.id.endsWith("_NOT_IMPLEMENTED")) {
6158
6203
  this.logger.warn(error$1.message);
6159
6204
  this.#unsupportedSignals.add("tracing");
6160
6205
  deferredUpdates.length = 0;
6161
6206
  this.emitDrop("tracing", "unsupported-storage", events.length + deferredCountAtEntry, error$1);
6162
- } else {
6163
- const newlyDeferred = deferredUpdates.length - deferredCountAtEntry;
6164
- if (newlyDeferred > 0) {
6165
- deferredUpdates.splice(deferredUpdates.length - newlyDeferred, newlyDeferred);
6166
- }
6167
- const dropped = this.#eventBuffer.reAddUpdates(events);
6168
- this.emitDrop("tracing", "retry-exhausted", dropped.length, error$1);
6207
+ return { failed: true };
6208
+ }
6209
+ const newlyDeferred = deferredUpdates.length - deferredCountAtEntry;
6210
+ if (newlyDeferred > 0) {
6211
+ deferredUpdates.splice(deferredUpdates.length - newlyDeferred, newlyDeferred);
6169
6212
  }
6213
+ const dropped = this.#eventBuffer.reAddUpdates(events);
6214
+ const retryAttempt = this.retryAttempt(events);
6215
+ this.logStorageFailure("tracing", events, retryAttempt, error$1);
6216
+ this.emitDrop("tracing", "retry-exhausted", dropped.length, error$1);
6217
+ return { failed: true, retryAttempt };
6170
6218
  }
6171
6219
  }
6172
6220
  /**
@@ -6234,7 +6282,7 @@ var DefaultExporter = class extends BaseExporter {
6234
6282
  break;
6235
6283
  }
6236
6284
  }
6237
- await Promise.all([
6285
+ const flushResults = await Promise.all([
6238
6286
  this.flushCreates(
6239
6287
  "feedback",
6240
6288
  createFeedbackEvents,
@@ -6262,25 +6310,36 @@ var DefaultExporter = class extends BaseExporter {
6262
6310
  })
6263
6311
  ]);
6264
6312
  const deferredUpdates = [];
6265
- await this.flushSpanUpdates(updateSpanEvents, deferredUpdates, false);
6266
- await this.flushSpanUpdates(endSpanEvents, deferredUpdates, true);
6313
+ flushResults.push(await this.flushSpanUpdates(updateSpanEvents, deferredUpdates, false));
6314
+ flushResults.push(await this.flushSpanUpdates(endSpanEvents, deferredUpdates, true));
6315
+ let deferredDropCount = 0;
6267
6316
  if (deferredUpdates.length > 0) {
6268
6317
  if (this.#unsupportedSignals.has("tracing")) {
6269
- this.emitDrop("tracing", "unsupported-storage", deferredUpdates.length);
6318
+ deferredDropCount = deferredUpdates.length;
6319
+ this.emitDrop("tracing", "unsupported-storage", deferredDropCount);
6270
6320
  deferredUpdates.length = 0;
6271
6321
  } else {
6272
6322
  const dropped = this.#eventBuffer.reAddUpdates(deferredUpdates);
6273
- this.emitDrop("tracing", "retry-exhausted", dropped.length);
6323
+ deferredDropCount = dropped.length;
6324
+ this.emitDrop("tracing", "retry-exhausted", deferredDropCount);
6274
6325
  }
6275
6326
  }
6276
- const elapsed = Date.now() - startTime;
6277
- this.logger.debug("Batch flushed", {
6278
- strategy: this.#resolvedStrategy,
6279
- batchSize,
6280
- durationMs: elapsed,
6281
- deferredUpdates: deferredUpdates.length > 0 ? deferredUpdates.length : void 0
6282
- });
6283
- return;
6327
+ const retryAttempt = flushResults.reduce((earliestAttempt, result) => {
6328
+ if (result.retryAttempt === void 0) return earliestAttempt;
6329
+ return earliestAttempt === void 0 ? result.retryAttempt : Math.min(earliestAttempt, result.retryAttempt);
6330
+ }, void 0);
6331
+ if (retryAttempt !== void 0) {
6332
+ this.scheduleFlush(this.retryDelay(retryAttempt));
6333
+ }
6334
+ if (!flushResults.some((result) => result.failed) && deferredDropCount === 0) {
6335
+ const elapsed = Date.now() - startTime;
6336
+ this.logger.debug("Batch flushed", {
6337
+ strategy: this.#resolvedStrategy,
6338
+ batchSize,
6339
+ durationMs: elapsed,
6340
+ deferredUpdates: deferredUpdates.length > 0 ? deferredUpdates.length : void 0
6341
+ });
6342
+ }
6284
6343
  }
6285
6344
  async _exportTracingEvent(event) {
6286
6345
  await this.waitForInit();
@@ -6357,6 +6416,10 @@ var DefaultExporter = class extends BaseExporter {
6357
6416
  this.#flushTimer = void 0;
6358
6417
  }
6359
6418
  await this.flush();
6419
+ if (this.#flushTimer) {
6420
+ clearTimeout(this.#flushTimer);
6421
+ this.#flushTimer = void 0;
6422
+ }
6360
6423
  this.logger.info("DefaultExporter shutdown complete");
6361
6424
  }
6362
6425
  };
@@ -6889,6 +6952,7 @@ function resolveTracingStorageStrategy2(config, observabilityStorage, storageNam
6889
6952
  }
6890
6953
  return observabilityStrategy.preferred;
6891
6954
  }
6955
+ var FLUSH_SUCCEEDED2 = { failed: false };
6892
6956
  var MastraStorageExporter = class extends BaseExporter {
6893
6957
  name = "mastra-storage-exporter";
6894
6958
  #config;
@@ -6983,17 +7047,37 @@ var MastraStorageExporter = class extends BaseExporter {
6983
7047
  /**
6984
7048
  * Schedules a flush using setTimeout
6985
7049
  */
6986
- scheduleFlush() {
7050
+ scheduleFlush(delayMs = this.#config.maxBatchWaitMs) {
6987
7051
  if (this.#flushTimer) {
6988
7052
  clearTimeout(this.#flushTimer);
6989
7053
  }
6990
7054
  this.#flushTimer = setTimeout(() => {
7055
+ this.#flushTimer = void 0;
6991
7056
  this.flushBuffer().catch((error) => {
6992
7057
  this.logger.error("Scheduled flush failed", {
6993
7058
  error: error instanceof Error ? error.message : String(error)
6994
7059
  });
6995
7060
  });
6996
- }, this.#config.maxBatchWaitMs);
7061
+ }, delayMs);
7062
+ }
7063
+ retryDelay(retryAttempt) {
7064
+ return this.#config.retryDelayMs * 2 ** (retryAttempt - 1);
7065
+ }
7066
+ retryAttempt(events) {
7067
+ return events.reduce((earliestAttempt, event) => {
7068
+ if (event.retryCount > this.#config.maxRetries) return earliestAttempt;
7069
+ return earliestAttempt === void 0 ? event.retryCount : Math.min(earliestAttempt, event.retryCount);
7070
+ }, void 0);
7071
+ }
7072
+ logStorageFailure(signal, events, retryAttempt, error) {
7073
+ this.logger.warn("Failed to persist observability events", {
7074
+ signal,
7075
+ eventCount: events.length,
7076
+ retryAttempt: retryAttempt ?? events.reduce((latestAttempt, event) => Math.max(latestAttempt, event.retryCount), 0),
7077
+ maxRetries: this.#config.maxRetries,
7078
+ ...retryAttempt === void 0 ? {} : { nextRetryDelayMs: this.retryDelay(retryAttempt) },
7079
+ error: error instanceof Error ? error.message : String(error)
7080
+ });
6997
7081
  }
6998
7082
  /**
6999
7083
  * Checks flush triggers and schedules/triggers flush as needed.
@@ -7040,22 +7124,26 @@ var MastraStorageExporter = class extends BaseExporter {
7040
7124
  * On other errors, re-adds events to the buffer for retry.
7041
7125
  */
7042
7126
  async flushCreates(signal, events, storageCall) {
7043
- if (events.length === 0) return;
7127
+ if (events.length === 0) return FLUSH_SUCCEEDED2;
7044
7128
  if (this.#unsupportedSignals.has(signal)) {
7045
7129
  this.emitDrop(signal, "unsupported-storage", events.length);
7046
- return;
7130
+ return { failed: true };
7047
7131
  }
7048
7132
  try {
7049
7133
  await storageCall(events);
7134
+ return FLUSH_SUCCEEDED2;
7050
7135
  } catch (error$1) {
7051
7136
  if (error$1 instanceof error.MastraError && error$1.domain === error.ErrorDomain.MASTRA_OBSERVABILITY && error$1.id.endsWith("_NOT_IMPLEMENTED")) {
7052
7137
  this.logger.warn(error$1.message);
7053
7138
  this.#unsupportedSignals.add(signal);
7054
7139
  this.emitDrop(signal, "unsupported-storage", events.length, error$1);
7055
- } else {
7056
- const dropped = this.#eventBuffer.reAddCreates(events);
7057
- this.emitDrop(signal, "retry-exhausted", dropped.length, error$1);
7140
+ return { failed: true };
7058
7141
  }
7142
+ const dropped = this.#eventBuffer.reAddCreates(events);
7143
+ const retryAttempt = this.retryAttempt(events);
7144
+ this.logStorageFailure(signal, events, retryAttempt, error$1);
7145
+ this.emitDrop(signal, "retry-exhausted", dropped.length, error$1);
7146
+ return { failed: true, retryAttempt };
7059
7147
  }
7060
7148
  }
7061
7149
  /**
@@ -7064,10 +7152,10 @@ var MastraStorageExporter = class extends BaseExporter {
7064
7152
  */
7065
7153
  async flushSpanUpdates(events, deferredUpdates, isEnd) {
7066
7154
  const deferredCountAtEntry = deferredUpdates.length;
7067
- if (events.length === 0) return;
7155
+ if (events.length === 0) return FLUSH_SUCCEEDED2;
7068
7156
  if (this.#unsupportedSignals.has("tracing")) {
7069
7157
  this.emitDrop("tracing", "unsupported-storage", events.length);
7070
- return;
7158
+ return { failed: true };
7071
7159
  }
7072
7160
  const partials = [];
7073
7161
  for (const event of events) {
@@ -7082,26 +7170,30 @@ var MastraStorageExporter = class extends BaseExporter {
7082
7170
  deferredUpdates.push(event);
7083
7171
  }
7084
7172
  }
7085
- if (partials.length === 0) return;
7173
+ if (partials.length === 0) return FLUSH_SUCCEEDED2;
7086
7174
  try {
7087
7175
  await this.#observabilityStorage.batchUpdateSpans({ records: partials });
7088
7176
  if (isEnd) {
7089
7177
  this.#eventBuffer.endFinishedSpans({ records: partials });
7090
7178
  }
7179
+ return FLUSH_SUCCEEDED2;
7091
7180
  } catch (error$1) {
7092
7181
  if (error$1 instanceof error.MastraError && error$1.domain === error.ErrorDomain.MASTRA_OBSERVABILITY && error$1.id.endsWith("_NOT_IMPLEMENTED")) {
7093
7182
  this.logger.warn(error$1.message);
7094
7183
  this.#unsupportedSignals.add("tracing");
7095
7184
  deferredUpdates.length = 0;
7096
7185
  this.emitDrop("tracing", "unsupported-storage", events.length + deferredCountAtEntry, error$1);
7097
- } else {
7098
- const newlyDeferred = deferredUpdates.length - deferredCountAtEntry;
7099
- if (newlyDeferred > 0) {
7100
- deferredUpdates.splice(deferredUpdates.length - newlyDeferred, newlyDeferred);
7101
- }
7102
- const dropped = this.#eventBuffer.reAddUpdates(events);
7103
- this.emitDrop("tracing", "retry-exhausted", dropped.length, error$1);
7186
+ return { failed: true };
7187
+ }
7188
+ const newlyDeferred = deferredUpdates.length - deferredCountAtEntry;
7189
+ if (newlyDeferred > 0) {
7190
+ deferredUpdates.splice(deferredUpdates.length - newlyDeferred, newlyDeferred);
7104
7191
  }
7192
+ const dropped = this.#eventBuffer.reAddUpdates(events);
7193
+ const retryAttempt = this.retryAttempt(events);
7194
+ this.logStorageFailure("tracing", events, retryAttempt, error$1);
7195
+ this.emitDrop("tracing", "retry-exhausted", dropped.length, error$1);
7196
+ return { failed: true, retryAttempt };
7105
7197
  }
7106
7198
  }
7107
7199
  /**
@@ -7169,7 +7261,7 @@ var MastraStorageExporter = class extends BaseExporter {
7169
7261
  break;
7170
7262
  }
7171
7263
  }
7172
- await Promise.all([
7264
+ const flushResults = await Promise.all([
7173
7265
  this.flushCreates(
7174
7266
  "feedback",
7175
7267
  createFeedbackEvents,
@@ -7197,25 +7289,36 @@ var MastraStorageExporter = class extends BaseExporter {
7197
7289
  })
7198
7290
  ]);
7199
7291
  const deferredUpdates = [];
7200
- await this.flushSpanUpdates(updateSpanEvents, deferredUpdates, false);
7201
- await this.flushSpanUpdates(endSpanEvents, deferredUpdates, true);
7292
+ flushResults.push(await this.flushSpanUpdates(updateSpanEvents, deferredUpdates, false));
7293
+ flushResults.push(await this.flushSpanUpdates(endSpanEvents, deferredUpdates, true));
7294
+ let deferredDropCount = 0;
7202
7295
  if (deferredUpdates.length > 0) {
7203
7296
  if (this.#unsupportedSignals.has("tracing")) {
7204
- this.emitDrop("tracing", "unsupported-storage", deferredUpdates.length);
7297
+ deferredDropCount = deferredUpdates.length;
7298
+ this.emitDrop("tracing", "unsupported-storage", deferredDropCount);
7205
7299
  deferredUpdates.length = 0;
7206
7300
  } else {
7207
7301
  const dropped = this.#eventBuffer.reAddUpdates(deferredUpdates);
7208
- this.emitDrop("tracing", "retry-exhausted", dropped.length);
7302
+ deferredDropCount = dropped.length;
7303
+ this.emitDrop("tracing", "retry-exhausted", deferredDropCount);
7209
7304
  }
7210
7305
  }
7211
- const elapsed = Date.now() - startTime;
7212
- this.logger.debug("Batch flushed", {
7213
- strategy: this.#resolvedStrategy,
7214
- batchSize,
7215
- durationMs: elapsed,
7216
- deferredUpdates: deferredUpdates.length > 0 ? deferredUpdates.length : void 0
7217
- });
7218
- return;
7306
+ const retryAttempt = flushResults.reduce((earliestAttempt, result) => {
7307
+ if (result.retryAttempt === void 0) return earliestAttempt;
7308
+ return earliestAttempt === void 0 ? result.retryAttempt : Math.min(earliestAttempt, result.retryAttempt);
7309
+ }, void 0);
7310
+ if (retryAttempt !== void 0) {
7311
+ this.scheduleFlush(this.retryDelay(retryAttempt));
7312
+ }
7313
+ if (!flushResults.some((result) => result.failed) && deferredDropCount === 0) {
7314
+ const elapsed = Date.now() - startTime;
7315
+ this.logger.debug("Batch flushed", {
7316
+ strategy: this.#resolvedStrategy,
7317
+ batchSize,
7318
+ durationMs: elapsed,
7319
+ deferredUpdates: deferredUpdates.length > 0 ? deferredUpdates.length : void 0
7320
+ });
7321
+ }
7219
7322
  }
7220
7323
  async _exportTracingEvent(event) {
7221
7324
  await this.waitForInit();
@@ -7292,6 +7395,10 @@ var MastraStorageExporter = class extends BaseExporter {
7292
7395
  this.#flushTimer = void 0;
7293
7396
  }
7294
7397
  await this.flush();
7398
+ if (this.#flushTimer) {
7399
+ clearTimeout(this.#flushTimer);
7400
+ this.#flushTimer = void 0;
7401
+ }
7295
7402
  this.logger.info("MastraStorageExporter shutdown complete");
7296
7403
  }
7297
7404
  };