@mastra/observability 1.17.1 → 1.17.2-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
@@ -7098,8 +7098,8 @@ const FLUSH_SUCCEEDED = { failed: false };
7098
7098
  var MastraStorageExporter = class extends BaseExporter {
7099
7099
  name = "mastra-storage-exporter";
7100
7100
  #config;
7101
- #isInitializing = false;
7102
- #initPromises = /* @__PURE__ */ new Set();
7101
+ #initializationPromise;
7102
+ #wasUnavailable = false;
7103
7103
  #eventBuffer;
7104
7104
  #storage;
7105
7105
  #observabilityStorage;
@@ -7124,41 +7124,56 @@ var MastraStorageExporter = class extends BaseExporter {
7124
7124
  * Initialize the exporter (called after all dependencies are ready)
7125
7125
  */
7126
7126
  async init(options) {
7127
+ this.#emitDropEvent = options.emitDropEvent;
7128
+ this.#storage = options.mastra?.getStorage();
7129
+ if (!this.#storage) {
7130
+ this.logger.warn("MastraStorageExporter disabled: Storage not available. Traces will not be persisted.");
7131
+ return;
7132
+ }
7133
+ await this.ensureInitialized();
7134
+ }
7135
+ warnUnavailable(message, error) {
7136
+ if (this.#wasUnavailable) return;
7137
+ this.#wasUnavailable = true;
7138
+ if (error === void 0) {
7139
+ this.logger.warn(message);
7140
+ return;
7141
+ }
7142
+ this.logger.warn(message, { error: error instanceof Error ? error.message : String(error) });
7143
+ }
7144
+ async initializeStorage() {
7145
+ const storage = this.#storage;
7146
+ if (!storage) return;
7127
7147
  try {
7128
- this.#isInitializing = true;
7129
- this.#emitDropEvent = options.emitDropEvent;
7130
- this.#storage = options.mastra?.getStorage();
7131
- if (!this.#storage) {
7132
- this.logger.warn("MastraStorageExporter disabled: Storage not available. Traces will not be persisted.");
7133
- return;
7134
- }
7135
- this.#observabilityStorage = await this.#storage.getStore("observability");
7136
- if (!this.#observabilityStorage) {
7137
- this.logger.warn("MastraStorageExporter disabled: Observability storage not available. Traces will not be persisted.");
7138
- return;
7139
- }
7140
- if (!this.#resolvedStrategy) {
7141
- this.#resolvedStrategy = resolveTracingStorageStrategy(this.#config, this.#observabilityStorage, this.#storage.constructor.name, this.logger);
7142
- this.logger.debug("tracing storage exporter initialized", {
7143
- strategy: this.#resolvedStrategy,
7144
- source: this.#config.strategy !== "auto" ? "user" : "auto",
7145
- storageAdapter: this.#storage.constructor.name,
7146
- maxBatchSize: this.#config.maxBatchSize,
7147
- maxBatchWaitMs: this.#config.maxBatchWaitMs
7148
- });
7149
- }
7150
- if (this.#resolvedStrategy) this.#eventBuffer.init({ strategy: this.#resolvedStrategy });
7151
- } finally {
7152
- this.#isInitializing = false;
7153
- /**
7154
- * Assumes caller waits until export of a parent span is completed before calling
7155
- * export for child spans , order is not relevant for resolve
7156
- */
7157
- this.#initPromises.forEach((resolve) => {
7158
- resolve();
7159
- });
7160
- this.#initPromises.clear();
7148
+ this.#observabilityStorage = await storage.getStore("observability");
7149
+ } catch (error) {
7150
+ this.warnUnavailable("MastraStorageExporter unavailable: Failed to initialize observability storage. Traces will not be persisted until storage becomes available.", error);
7151
+ return;
7161
7152
  }
7153
+ if (!this.#observabilityStorage) {
7154
+ this.warnUnavailable("MastraStorageExporter unavailable: Observability storage not available. Traces will not be persisted until storage becomes available.");
7155
+ return;
7156
+ }
7157
+ this.#resolvedStrategy = resolveTracingStorageStrategy(this.#config, this.#observabilityStorage, storage.constructor.name, this.logger);
7158
+ this.#eventBuffer.init({ strategy: this.#resolvedStrategy });
7159
+ this.logger.debug("tracing storage exporter initialized", {
7160
+ strategy: this.#resolvedStrategy,
7161
+ source: this.#config.strategy !== "auto" ? "user" : "auto",
7162
+ storageAdapter: storage.constructor.name,
7163
+ maxBatchSize: this.#config.maxBatchSize,
7164
+ maxBatchWaitMs: this.#config.maxBatchWaitMs
7165
+ });
7166
+ if (this.#wasUnavailable) {
7167
+ this.#wasUnavailable = false;
7168
+ this.logger.info("MastraStorageExporter recovered: Observability storage is available. Traces will be persisted.");
7169
+ }
7170
+ }
7171
+ async ensureInitialized() {
7172
+ if (this.#observabilityStorage || !this.#storage) return;
7173
+ this.#initializationPromise ??= this.initializeStorage().finally(() => {
7174
+ this.#initializationPromise = void 0;
7175
+ });
7176
+ await this.#initializationPromise;
7162
7177
  }
7163
7178
  /**
7164
7179
  * Checks if buffer should be flushed based on size or time triggers
@@ -7410,7 +7425,7 @@ var MastraStorageExporter = class extends BaseExporter {
7410
7425
  }
7411
7426
  }
7412
7427
  async _exportTracingEvent(event) {
7413
- await this.waitForInit();
7428
+ await this.ensureInitialized();
7414
7429
  if (!this.#observabilityStorage) {
7415
7430
  this.logger.debug("Cannot store traces. Observability storage is not initialized");
7416
7431
  return;
@@ -7419,21 +7434,10 @@ var MastraStorageExporter = class extends BaseExporter {
7419
7434
  await this.handleBatchedFlush();
7420
7435
  }
7421
7436
  /**
7422
- * Resolves when an ongoing init call is finished
7423
- * Doesn't wait for the caller to call init
7424
- * @returns
7425
- */
7426
- async waitForInit() {
7427
- if (!this.#isInitializing) return;
7428
- return new Promise((resolve) => {
7429
- this.#initPromises.add(resolve);
7430
- });
7431
- }
7432
- /**
7433
7437
  * Handle metric events — buffer for batch flush.
7434
7438
  */
7435
7439
  async onMetricEvent(event) {
7436
- await this.waitForInit();
7440
+ await this.ensureInitialized();
7437
7441
  if (!this.#observabilityStorage) return;
7438
7442
  this.#eventBuffer.addEvent(event);
7439
7443
  await this.handleBatchedFlush();
@@ -7442,7 +7446,7 @@ var MastraStorageExporter = class extends BaseExporter {
7442
7446
  * Handle log events — buffer for batch flush.
7443
7447
  */
7444
7448
  async onLogEvent(event) {
7445
- await this.waitForInit();
7449
+ await this.ensureInitialized();
7446
7450
  if (!this.#observabilityStorage) return;
7447
7451
  this.#eventBuffer.addEvent(event);
7448
7452
  await this.handleBatchedFlush();
@@ -7451,7 +7455,7 @@ var MastraStorageExporter = class extends BaseExporter {
7451
7455
  * Handle score events — buffer for batch flush.
7452
7456
  */
7453
7457
  async onScoreEvent(event) {
7454
- await this.waitForInit();
7458
+ await this.ensureInitialized();
7455
7459
  if (!this.#observabilityStorage) return;
7456
7460
  this.#eventBuffer.addEvent(event);
7457
7461
  await this.handleBatchedFlush();
@@ -7460,7 +7464,7 @@ var MastraStorageExporter = class extends BaseExporter {
7460
7464
  * Handle feedback events — buffer for batch flush.
7461
7465
  */
7462
7466
  async onFeedbackEvent(event) {
7463
- await this.waitForInit();
7467
+ await this.ensureInitialized();
7464
7468
  if (!this.#observabilityStorage) return;
7465
7469
  this.#eventBuffer.addEvent(event);
7466
7470
  await this.handleBatchedFlush();