@mastra/observability 1.0.0-beta.11 → 1.0.0-beta.12

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
@@ -4161,6 +4161,8 @@ var observabilityRegistryConfigSchema = external_exports.object({
4161
4161
  var BaseExporter = class {
4162
4162
  /** Mastra logger instance */
4163
4163
  logger;
4164
+ /** Base configuration (accessible by subclasses) */
4165
+ baseConfig;
4164
4166
  /** Whether this exporter is disabled */
4165
4167
  #disabled = false;
4166
4168
  /** Public getter for disabled state */
@@ -4171,6 +4173,7 @@ var BaseExporter = class {
4171
4173
  * Initialize the base exporter with logger
4172
4174
  */
4173
4175
  constructor(config = {}) {
4176
+ this.baseConfig = config;
4174
4177
  const logLevel = this.resolveLogLevel(config.logLevel);
4175
4178
  this.logger = config.logger ?? new ConsoleLogger({ level: logLevel, name: this.constructor.name });
4176
4179
  }
@@ -4208,17 +4211,47 @@ var BaseExporter = class {
4208
4211
  this.#disabled = true;
4209
4212
  this.logger.warn(`${this.name} disabled: ${reason}`);
4210
4213
  }
4214
+ /**
4215
+ * Apply the customSpanFormatter if configured.
4216
+ * This is called automatically by exportTracingEvent before _exportTracingEvent.
4217
+ *
4218
+ * Supports both synchronous and asynchronous formatters. If the formatter
4219
+ * returns a Promise, it will be awaited.
4220
+ *
4221
+ * @param event - The incoming tracing event
4222
+ * @returns The (possibly modified) event to process
4223
+ */
4224
+ async applySpanFormatter(event) {
4225
+ if (this.baseConfig.customSpanFormatter) {
4226
+ try {
4227
+ const formattedSpan = await this.baseConfig.customSpanFormatter(event.exportedSpan);
4228
+ return {
4229
+ ...event,
4230
+ exportedSpan: formattedSpan
4231
+ };
4232
+ } catch (error) {
4233
+ this.logger.error(`${this.name}: Error in customSpanFormatter`, {
4234
+ error,
4235
+ spanId: event.exportedSpan.id,
4236
+ traceId: event.exportedSpan.traceId
4237
+ });
4238
+ }
4239
+ }
4240
+ return event;
4241
+ }
4211
4242
  /**
4212
4243
  * Export a tracing event
4213
4244
  *
4214
- * This method checks if the exporter is disabled before calling _exportEvent.
4215
- * Subclasses should implement _exportEvent instead of overriding this method.
4245
+ * This method checks if the exporter is disabled, applies the customSpanFormatter,
4246
+ * then calls _exportTracingEvent.
4247
+ * Subclasses should implement _exportTracingEvent instead of overriding this method.
4216
4248
  */
4217
4249
  async exportTracingEvent(event) {
4218
4250
  if (this.isDisabled) {
4219
4251
  return;
4220
4252
  }
4221
- await this._exportTracingEvent(event);
4253
+ const processedEvent = await this.applySpanFormatter(event);
4254
+ await this._exportTracingEvent(processedEvent);
4222
4255
  }
4223
4256
  /**
4224
4257
  * Shutdown the exporter and clean up resources
@@ -4890,6 +4923,10 @@ var TrackingExporter = class extends BaseExporter {
4890
4923
  /**
4891
4924
  * Hook called before processing each tracing event.
4892
4925
  * Override to transform or enrich the event before processing.
4926
+ *
4927
+ * Note: The customSpanFormatter is applied at the BaseExporter level before this hook.
4928
+ * Subclasses can override this to add additional pre-processing logic.
4929
+ *
4893
4930
  * @param event - The incoming tracing event
4894
4931
  * @returns The (possibly modified) event to process
4895
4932
  */
@@ -5162,9 +5199,20 @@ var TrackingExporter = class extends BaseExporter {
5162
5199
  await super.shutdown();
5163
5200
  }
5164
5201
  };
5202
+
5203
+ // src/exporters/span-formatters.ts
5204
+ function chainFormatters(formatters) {
5205
+ return async (span) => {
5206
+ let currentSpan = span;
5207
+ for (const formatter of formatters) {
5208
+ currentSpan = await formatter(currentSpan);
5209
+ }
5210
+ return currentSpan;
5211
+ };
5212
+ }
5165
5213
  var CloudExporter = class extends BaseExporter {
5166
5214
  name = "mastra-cloud-observability-exporter";
5167
- config;
5215
+ cloudConfig;
5168
5216
  buffer;
5169
5217
  flushTimer = null;
5170
5218
  constructor(config = {}) {
@@ -5174,7 +5222,7 @@ var CloudExporter = class extends BaseExporter {
5174
5222
  this.setDisabled("MASTRA_CLOUD_ACCESS_TOKEN environment variable not set.");
5175
5223
  }
5176
5224
  const endpoint = config.endpoint ?? process.env.MASTRA_CLOUD_TRACES_ENDPOINT ?? "https://api.mastra.ai/ai/spans/publish";
5177
- this.config = {
5225
+ this.cloudConfig = {
5178
5226
  logger: this.logger,
5179
5227
  logLevel: config.logLevel ?? LogLevel.INFO,
5180
5228
  maxBatchSize: config.maxBatchSize ?? 1e3,
@@ -5232,12 +5280,12 @@ var CloudExporter = class extends BaseExporter {
5232
5280
  return spanRecord;
5233
5281
  }
5234
5282
  shouldFlush() {
5235
- if (this.buffer.totalSize >= this.config.maxBatchSize) {
5283
+ if (this.buffer.totalSize >= this.cloudConfig.maxBatchSize) {
5236
5284
  return true;
5237
5285
  }
5238
5286
  if (this.buffer.firstEventTime && this.buffer.totalSize > 0) {
5239
5287
  const elapsed = Date.now() - this.buffer.firstEventTime.getTime();
5240
- if (elapsed >= this.config.maxBatchWaitMs) {
5288
+ if (elapsed >= this.cloudConfig.maxBatchWaitMs) {
5241
5289
  return true;
5242
5290
  }
5243
5291
  }
@@ -5260,7 +5308,7 @@ var CloudExporter = class extends BaseExporter {
5260
5308
  this.logger.trackException(mastraError);
5261
5309
  this.logger.error("Scheduled flush failed", mastraError);
5262
5310
  });
5263
- }, this.config.maxBatchWaitMs);
5311
+ }, this.cloudConfig.maxBatchWaitMs);
5264
5312
  }
5265
5313
  async flush() {
5266
5314
  if (this.flushTimer) {
@@ -5272,7 +5320,7 @@ var CloudExporter = class extends BaseExporter {
5272
5320
  }
5273
5321
  const startTime = Date.now();
5274
5322
  const spansCopy = [...this.buffer.spans];
5275
- const flushReason = this.buffer.totalSize >= this.config.maxBatchSize ? "size" : "time";
5323
+ const flushReason = this.buffer.totalSize >= this.cloudConfig.maxBatchSize ? "size" : "time";
5276
5324
  this.resetBuffer();
5277
5325
  try {
5278
5326
  await this.batchUpload(spansCopy);
@@ -5303,7 +5351,7 @@ var CloudExporter = class extends BaseExporter {
5303
5351
  */
5304
5352
  async batchUpload(spans) {
5305
5353
  const headers = {
5306
- Authorization: `Bearer ${this.config.accessToken}`,
5354
+ Authorization: `Bearer ${this.cloudConfig.accessToken}`,
5307
5355
  "Content-Type": "application/json"
5308
5356
  };
5309
5357
  const options = {
@@ -5311,7 +5359,7 @@ var CloudExporter = class extends BaseExporter {
5311
5359
  headers,
5312
5360
  body: JSON.stringify({ spans })
5313
5361
  };
5314
- await fetchWithRetry(this.config.endpoint, options, this.config.maxRetries);
5362
+ await fetchWithRetry(this.cloudConfig.endpoint, options, this.cloudConfig.maxRetries);
5315
5363
  }
5316
5364
  resetBuffer() {
5317
5365
  this.buffer.spans = [];
@@ -7715,6 +7763,6 @@ function buildTracingOptions(...updaters) {
7715
7763
  return updaters.reduce((opts, updater) => updater(opts), {});
7716
7764
  }
7717
7765
 
7718
- export { BaseExporter, BaseObservabilityInstance, BaseSpan, CloudExporter, ConsoleExporter, DEFAULT_DEEP_CLEAN_OPTIONS, DEFAULT_KEYS_TO_STRIP, DefaultExporter, DefaultObservabilityInstance, DefaultSpan, ModelSpanTracker, NoOpSpan, Observability, SamplingStrategyType, SensitiveDataFilter, TestExporter, TraceData, TrackingExporter, buildTracingOptions, deepClean, getExternalParentId, mergeSerializationOptions, observabilityConfigValueSchema, observabilityInstanceConfigSchema, observabilityRegistryConfigSchema, samplingStrategySchema, serializationOptionsSchema, truncateString };
7766
+ export { BaseExporter, BaseObservabilityInstance, BaseSpan, CloudExporter, ConsoleExporter, DEFAULT_DEEP_CLEAN_OPTIONS, DEFAULT_KEYS_TO_STRIP, DefaultExporter, DefaultObservabilityInstance, DefaultSpan, ModelSpanTracker, NoOpSpan, Observability, SamplingStrategyType, SensitiveDataFilter, TestExporter, TraceData, TrackingExporter, buildTracingOptions, chainFormatters, deepClean, getExternalParentId, mergeSerializationOptions, observabilityConfigValueSchema, observabilityInstanceConfigSchema, observabilityRegistryConfigSchema, samplingStrategySchema, serializationOptionsSchema, truncateString };
7719
7767
  //# sourceMappingURL=index.js.map
7720
7768
  //# sourceMappingURL=index.js.map