Skip to main content

OtelExporter

warning

The OtelExporter is currently experimental. APIs and configuration options may change in future releases.

Sends AI tracing data to any OpenTelemetry-compatible observability platform using standardized GenAI semantic conventions.

Constructor​

new OtelExporter(config: OtelExporterConfig)

OtelExporterConfig​

interface OtelExporterConfig {
provider?: ProviderConfig;
timeout?: number;
batchSize?: number;
logLevel?: "debug" | "info" | "warn" | "error";
}

Provider Configurations​

Dash0Config​

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

SignozConfig​

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

NewRelicConfig​

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

TraceloopConfig​

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

LaminarConfig​

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

CustomConfig​

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

Methods​

exportEvent​

async exportEvent(event: AITracingEvent): Promise<void>

Exports a tracing event to the configured OTEL backend.

shutdown​

async shutdown(): Promise<void>

Flushes pending traces and shuts down the exporter.

Usage Examples​

Basic Usage​

import { OtelExporter } from "@mastra/otel-exporter";

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

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",
});

Span Mapping​

The exporter maps Mastra AI spans to OpenTelemetry spans following GenAI semantic conventions:

Span Names​

  • MODEL_GENERATION → chat {model} or tool_selection {model}
  • TOOL_CALL → tool.execute {tool_name}
  • AGENT_RUN → agent.{agent_id}
  • WORKFLOW_RUN → workflow.{workflow_id}

Span Kinds​

  • Root agent/workflow spans → SERVER
  • LLM calls → CLIENT
  • Tool calls → INTERNAL or CLIENT
  • Workflow steps → INTERNAL

Attributes​

The exporter maps to standard OTEL GenAI attributes:

Mastra AttributeOTEL Attribute
modelgen_ai.request.model
providergen_ai.system
inputTokens / promptTokensgen_ai.usage.input_tokens
outputTokens / completionTokensgen_ai.usage.output_tokens
temperaturegen_ai.request.temperature
maxOutputTokensgen_ai.request.max_tokens
finishReasongen_ai.response.finish_reasons

Protocol Requirements​

Different providers require different OTEL exporter packages:

ProtocolRequired PackageProviders
gRPC@opentelemetry/exporter-trace-otlp-grpcDash0
HTTP/Protobuf@opentelemetry/exporter-trace-otlp-protoSigNoz, New Relic, Laminar
HTTP/JSON@opentelemetry/exporter-trace-otlp-httpTraceloop, Custom
Zipkin@opentelemetry/exporter-zipkinZipkin collectors

Parent-Child Relationships​

The exporter preserves span hierarchy from Mastra's AI tracing:

  • Uses parentSpanId directly from Mastra spans
  • Maintains correct nesting for agents, workflows, LLM calls, and tools
  • Exports complete traces with all relationships intact