CloudExporter
Sends traces to Mastra Cloud for online visualization and monitoring.
ConstructorDirect link to Constructor
new CloudExporter(config?: CloudExporterConfig)
CloudExporterConfigDirect link to CloudExporterConfig
interface CloudExporterConfig extends BaseExporterConfig {
/** Maximum number of spans per batch. Default: 1000 */
maxBatchSize?: number;
/** Maximum wait time before flushing in milliseconds. Default: 5000 */
maxBatchWaitMs?: number;
/** Maximum retry attempts. Default: 3 */
maxRetries?: number;
/** Cloud access token (from env or config) */
accessToken?: string;
/** Cloud observability endpoint */
endpoint?: string;
}
Extends BaseExporterConfig, which includes:
logger?: IMastraLogger- Logger instancelogLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error'- Log level (default: INFO)
Environment VariablesDirect link to Environment Variables
The exporter reads these environment variables if not provided in config:
MASTRA_CLOUD_ACCESS_TOKEN- Access token for authenticationMASTRA_CLOUD_TRACES_ENDPOINT- Custom endpoint (defaults tohttps://api.mastra.ai/ai/spans/publish)
PropertiesDirect link to Properties
readonly name = 'mastra-cloud-observability-exporter';
MethodsDirect link to Methods
exportTracingEventDirect link to exportTracingEvent
async exportTracingEvent(event: TracingEvent): Promise<void>
Processes tracing events. Only exports SPAN_ENDED events to Cloud.
flushDirect link to flush
async flush(): Promise<void>
Force flushes any buffered events to Mastra Cloud without shutting down the exporter. Useful in serverless environments where you need to ensure spans are exported before the runtime terminates.
shutdownDirect link to shutdown
async shutdown(): Promise<void>
Flushes remaining events and performs cleanup.
BehaviorDirect link to Behavior
AuthenticationDirect link to Authentication
If no access token is provided via config or environment variable, the exporter:
- Logs a warning with sign-up information
- Operates as a no-op (discards all events)
BatchingDirect link to Batching
The exporter batches spans for efficient network usage:
- Flushes when batch size reaches
maxBatchSize - Flushes when
maxBatchWaitMselapsed since first span in batch - Flushes on
shutdown()
Error HandlingDirect link to Error Handling
- Uses exponential backoff retry with
maxRetriesattempts - Drops batches after all retries fail
- Logs errors but continues processing new events
Event ProcessingDirect link to Event Processing
- Only processes
SPAN_ENDEDevents - Ignores
SPAN_STARTEDandSPAN_UPDATEDevents - Formats spans to MastraCloudSpanRecord format
MastraCloudSpanRecordDirect link to MastraCloudSpanRecord
Internal format for cloud spans:
interface MastraCloudSpanRecord {
traceId: string;
spanId: string;
parentSpanId: string | null;
name: string;
spanType: string;
attributes: Record<string, any> | null;
metadata: Record<string, any> | null;
startedAt: Date;
endedAt: Date | null;
input: any;
output: any;
error: any;
isEvent: boolean;
createdAt: Date;
updatedAt: Date | null;
}
UsageDirect link to Usage
import { CloudExporter } from "@mastra/observability";
// Uses environment variable for token
const exporter = new CloudExporter();
// Explicit configuration
const customExporter = new CloudExporter({
accessToken: "your-token",
maxBatchSize: 500,
maxBatchWaitMs: 2000,
logLevel: 'debug'
});
See AlsoDirect link to See Also
DocumentationDirect link to Documentation
- Tracing Overview - Complete guide
- Exporters - Exporter concepts
Other ExportersDirect link to Other Exporters
- DefaultExporter - Storage persistence
- ConsoleExporter - Debug output
- Langfuse - Langfuse integration
- Braintrust - Braintrust integration
ReferenceDirect link to Reference
- Configuration - Configuration options
- Interfaces - Type definitions