MastraStorageExporter
Persists traces to Mastra's configured storage with automatic batching and retry logic.
MastraStorageExporter was previously called DefaultExporter. The original DefaultExporter class is still exported from @mastra/observability so existing imports keep working, but it's deprecated and will be removed in a future major version. New code should use MastraStorageExporter.
ConstructorConstructor的直接链接
new MastraStorageExporter(config?: MastraStorageExporterConfig)
MastraStorageExporterConfigmastrastorageexporterconfig的直接链接
interface MastraStorageExporterConfig extends BaseExporterConfig {
/** Maximum number of spans per batch. Default: 1000 */
maxBatchSize?: number
/** Maximum total buffer size before emergency flush. Default: 10000 */
maxBufferSize?: number
/** Maximum time to wait before flushing batch in milliseconds. Default: 5000 */
maxBatchWaitMs?: number
/** Maximum number of retry attempts. Default: 4 */
maxRetries?: number
/** Base retry delay in milliseconds (uses exponential backoff). Default: 500 */
retryDelayMs?: number
/** Tracing storage strategy or 'auto' for automatic selection. Default: 'auto' */
strategy?: TracingStorageStrategy | 'auto'
}
Extends BaseExporterConfig, which includes:
logger?: IMastraLogger- Logger instancelogLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error'- Log level (default: INFO)
TracingStorageStrategytracingstoragestrategy的直接链接
type TracingStorageStrategy = 'realtime' | 'batch-with-updates' | 'insert-only'
Strategy BehaviorsStrategy Behaviors的直接链接
- realtime: Immediately persists each event to storage
- batch-with-updates: Batches creates and updates separately, applies in order
- insert-only: Only processes SPAN_ENDED events, ignores updates
PropertiesProperties的直接链接
readonly name = 'mastra-storage-exporter';
The deprecated DefaultExporter class continues to use 'mastra-default-observability-exporter' as its name for backward compatibility.
MethodsMethods的直接链接
initinit的直接链接
init(options: InitExporterOptions): void
Initializes the exporter after dependencies are ready. Resolves tracing strategy based on storage capabilities.
exportTracingEventexporttracingevent的直接链接
async exportTracingEvent(event: TracingEvent): Promise<void>
Processes a tracing event according to the resolved strategy.
flushflush的直接链接
async flush(): Promise<void>
Force flushes any buffered events to storage without shutting down the exporter. Useful in serverless environments where you need to ensure spans are exported before the runtime terminates.
shutdownshutdown的直接链接
async shutdown(): Promise<void>
Flushes remaining buffered events and performs cleanup.
Automatic strategy selectionAutomatic strategy selection的直接链接
When strategy: 'auto' (default), the exporter queries the storage adapter for its capabilities:
interface TracingStrategy {
/** Strategies supported by this adapter */
supported: TracingStorageStrategy[]
/** Preferred strategy for optimal performance */
preferred: TracingStorageStrategy
}
The exporter will:
- Use the storage adapter's preferred strategy if available
- Fall back to the first supported strategy if preferred isn't available
- Log a warning if a user-specified strategy isn't supported
Batching behaviorBatching behavior的直接链接
Flush TriggersFlush Triggers的直接链接
The buffer flushes when any of these conditions are met:
- Buffer size reaches
maxBatchSize - Time since first buffered event exceeds
maxBatchWaitMs - Buffer size reaches
maxBufferSize(emergency flush) shutdown()is called
Retry LogicRetry Logic的直接链接
Failed flushes are retried with exponential backoff:
- Retry delay:
retryDelayMs * 2^attempt - Maximum attempts:
maxRetries - Batch is dropped after all retries fail
Out-of-Order HandlingOut-of-Order Handling的直接链接
For batch-with-updates strategy:
- Tracks which spans have been created
- Rejects updates/ends for spans not yet created
- Logs warnings for out-of-order events
- Maintains sequence numbers for ordered updates
UsageUsage的直接链接
import { MastraStorageExporter } from '@mastra/observability'
// Default configuration
const exporter = new MastraStorageExporter()
// Custom batching configuration
const customExporter = new MastraStorageExporter({
maxBatchSize: 500,
maxBatchWaitMs: 2000,
strategy: 'batch-with-updates',
logLevel: 'debug',
})
Migrating from DefaultExportermigrating-from-defaultexporter的直接链接
Both classes share the same constructor signature and behavior. To migrate, replace the import and constructor:
// Before
import { DefaultExporter } from '@mastra/observability'
const exporter = new DefaultExporter()
// After
import { MastraStorageExporter } from '@mastra/observability'
const exporter = new MastraStorageExporter()
The original DefaultExporter is preserved unchanged so dashboards or alert rules that match the previous mastra-default-observability-exporter exporter name keep working until you migrate.
See alsoSee also的直接链接
DocumentationDocumentation的直接链接
- Tracing Overview: Complete guide
Other ExportersOther Exporters的直接链接
- MastraPlatformExporter: Mastra platform
- ConsoleExporter: Debug output
- Langfuse: Langfuse integration
- Braintrust: Braintrust integration
ReferenceReference的直接链接
- Configuration: Configuration options
- Interfaces: Type definitions