跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

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.

Constructor
Constructor的直接链接

new OtelExporter(config: OtelExporterConfig)

OtelExporterConfig
otelexporterconfig的直接链接

interface OtelExporterConfig {
provider?: ProviderConfig
signals?: {
traces?: boolean
logs?: boolean
}
timeout?: number
batchSize?: number
logLevel?: 'debug' | 'info' | 'warn' | 'error'
}

Provider configurations
Provider configurations的直接链接

Dash0Config
dash0config的直接链接

interface Dash0Config {
apiKey?: string
endpoint?: string
dataset?: string
}

SignozConfig
signozconfig的直接链接

interface SignozConfig {
apiKey?: string
region?: 'us' | 'eu' | 'in'
endpoint?: string
}

NewRelicConfig
newrelicconfig的直接链接

interface NewRelicConfig {
apiKey?: string
endpoint?: string
}

TraceloopConfig
traceloopconfig的直接链接

interface TraceloopConfig {
apiKey?: string
destinationId?: string
endpoint?: string
}

LaminarConfig
laminarconfig的直接链接

interface LaminarConfig {
apiKey?: string
endpoint?: string
}

CustomConfig
customconfig的直接链接

interface CustomConfig {
endpoint: string
protocol?: 'http/json' | 'http/protobuf' | 'grpc' | 'zipkin'
headers?: Record<string, string>
}

Methods
Methods的直接链接

exportTracingEvent
exporttracingevent的直接链接

async exportTracingEvent(event: TracingEvent): Promise<void>

Exports a tracing event to the configured OTEL backend.

flush
flush的直接链接

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.

shutdown
shutdown的直接链接

async shutdown(): Promise<void>

Flushes pending traces and shuts down the exporter.

Usage examples
Usage 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 Configuration
Explicit Configuration的直接链接

import { OtelExporter } from '@mastra/otel-exporter'

const exporter = new OtelExporter({
provider: {
signoz: {
apiKey: process.env.SIGNOZ_API_KEY,
region: 'us',
},
},
})

With Custom Endpoint
With 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 requirements
Protocol 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.

ProtocolTrace packageLog 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-zipkinnot supported

Tags support
Tags 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.

Usage
Usage的直接链接

const result = await agent.generate('Hello', {
tracingOptions: {
tags: ['production', 'experiment-v2', 'user-request'],
},
})

How Tags Are Stored
How 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.