OtelExporter
Sends traces and logs to any OpenTelemetry-compatible observability platform. Traces use the standardized GenAI semantic conventions. Logs carry their original severity and message body and are correlated to traces via both the OTEL log record's native trace context and mastra.traceId / mastra.spanId attributes.
ConstructorConstructor的直接链接
new OtelExporter(config: OtelExporterConfig)
OtelExporterConfigotelexporterconfig的直接链接
interface OtelExporterConfig {
provider?: ProviderConfig
signals?: {
traces?: boolean
logs?: boolean
}
timeout?: number
batchSize?: number
logLevel?: 'debug' | 'info' | 'warn' | 'error'
}
Provider configurationsProvider configurations的直接链接
Dash0Configdash0config的直接链接
interface Dash0Config {
apiKey?: string
endpoint?: string
dataset?: string
}
SignozConfigsignozconfig的直接链接
interface SignozConfig {
apiKey?: string
region?: 'us' | 'eu' | 'in'
endpoint?: string
}
NewRelicConfignewrelicconfig的直接链接
interface NewRelicConfig {
apiKey?: string
endpoint?: string
}
TraceloopConfigtraceloopconfig的直接链接
interface TraceloopConfig {
apiKey?: string
destinationId?: string
endpoint?: string
}
LaminarConfiglaminarconfig的直接链接
interface LaminarConfig {
apiKey?: string
endpoint?: string
}
CustomConfigcustomconfig的直接链接
interface CustomConfig {
endpoint: string
protocol?: 'http/json' | 'http/protobuf' | 'grpc' | 'zipkin'
headers?: Record<string, string>
}
MethodsMethods的直接链接
exportTracingEventexporttracingevent的直接链接
async exportTracingEvent(event: TracingEvent): Promise<void>
Exports a tracing event to the configured OTEL backend.
flushflush的直接链接
async flush(): Promise<void>
Force flushes any buffered spans to the OTEL backend 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 pending traces and shuts down the exporter.
Usage examplesUsage examples的直接链接
Zero-Config (using environment variables)Zero-Config (using environment variables)的直接链接
import { OtelExporter } from '@mastra/otel-exporter'
// Set SIGNOZ_API_KEY, SIGNOZ_REGION environment variables
const exporter = new OtelExporter({ provider: { signoz: {} } })
// Or for other providers:
// Set DASH0_API_KEY, DASH0_ENDPOINT for Dash0
// Set NEW_RELIC_LICENSE_KEY for New Relic
// Set TRACELOOP_API_KEY for Traceloop
// Set LMNR_PROJECT_API_KEY for Laminar
Explicit ConfigurationExplicit Configuration的直接链接
import { OtelExporter } from '@mastra/otel-exporter'
const exporter = new OtelExporter({
provider: {
signoz: {
apiKey: process.env.SIGNOZ_API_KEY,
region: 'us',
},
},
})
With Custom EndpointWith Custom Endpoint的直接链接
const exporter = new OtelExporter({
provider: {
custom: {
endpoint: 'https://my-collector.example.com/v1/traces',
protocol: 'http/protobuf',
headers: {
'x-api-key': process.env.API_KEY,
},
},
},
timeout: 60000,
logLevel: 'debug',
})
Protocol requirementsProtocol requirements的直接链接
Different providers require different OTEL exporter packages. Trace and log export are independent: install only the trace package if you only need traces, or install both the trace and log packages for the chosen protocol if you also want log export. Zipkin doesn't support OTLP logs.
| Protocol | Trace package | Log package |
|---|---|---|
| gRPC | @opentelemetry/exporter-trace-otlp-grpc (+ @grpc/grpc-js) | @opentelemetry/exporter-logs-otlp-grpc (+ @grpc/grpc-js) |
| HTTP/Protobuf | @opentelemetry/exporter-trace-otlp-proto | @opentelemetry/exporter-logs-otlp-proto |
| HTTP/JSON | @opentelemetry/exporter-trace-otlp-http | @opentelemetry/exporter-logs-otlp-http |
| Zipkin | @opentelemetry/exporter-zipkin | not supported |
Tags supportTags support的直接链接
The OtelExporter supports trace tagging for categorization and filtering. Tags are only applied to root spans and are stored as the mastra.tags attribute.
UsageUsage的直接链接
const result = await agent.generate('Hello', {
tracingOptions: {
tags: ['production', 'experiment-v2', 'user-request'],
},
})
How Tags Are StoredHow Tags Are Stored的直接链接
Tags are stored as a JSON-stringified array in the mastra.tags span attribute for maximum backend compatibility:
{
"mastra.tags": "[\"production\",\"experiment-v2\",\"user-request\"]"
}
While the OpenTelemetry specification supports native array attributes, many backends (Jaeger, Zipkin, Tempo) have limited array support. JSON strings ensure consistent behavior across all observability platforms.
RelatedRelated的直接链接
- OtelExporter Guide: Setup guide with provider configurations
- OtelBridge: For bidirectional OTEL context integration
- Tracing Overview: General tracing concepts