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.

ConstructorDirect link to Constructor

new OtelExporter(config: OtelExporterConfig)

OtelExporterConfigDirect link to OtelExporterConfig

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

Provider ConfigurationsDirect link to Provider Configurations

Dash0ConfigDirect link to Dash0Config

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

SignozConfigDirect link to SignozConfig

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

NewRelicConfigDirect link to NewRelicConfig

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

TraceloopConfigDirect link to TraceloopConfig

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

LaminarConfigDirect link to LaminarConfig

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

CustomConfigDirect link to CustomConfig

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

MethodsDirect link to Methods

exportEventDirect link to exportEvent

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

Exports a tracing event to the configured OTEL backend.

shutdownDirect link to shutdown

async shutdown(): Promise<void>

Flushes pending traces and shuts down the exporter.

Usage ExamplesDirect link to Usage Examples

Basic UsageDirect link to Basic Usage

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

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

With Custom EndpointDirect link to 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 MappingDirect link to Span Mapping

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

Span NamesDirect link to Span Names

  • MODEL_GENERATIONchat {model} or tool_selection {model}
  • TOOL_CALLtool.execute {tool_name}
  • AGENT_RUNagent.{agent_id}
  • WORKFLOW_RUNworkflow.{workflow_id}

Span KindsDirect link to Span Kinds

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

AttributesDirect link to 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 RequirementsDirect link to 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 RelationshipsDirect link to 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