Skip to main content

DatadogExporter

Sends Tracing data to Datadog's LLM Observability product for monitoring and analytics.

note

If you also use dd-trace APM auto-instrumentation and need it correctly parented under Mastra spans, use the DatadogBridge instead. The exporter sends LLM Observability data after execution completes, so it doesn't participate in dd-trace's live scope.

Constructor
Direct link to Constructor

new DatadogExporter(config: DatadogExporterConfig)

DatadogExporterConfig
Direct link to datadogexporterconfig

interface DatadogExporterConfig extends BaseExporterConfig {
apiKey?: string
mlApp?: string
site?: string
service?: string
env?: string
agentless?: boolean
integrationsEnabled?: boolean
flushGraceMs?: number
requestContextKeys?: string[]
}

Extends BaseExporterConfig, which includes:

  • logger?: IMastraLogger - Logger instance
  • logLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error' - Log level (default: INFO)

Methods
Direct link to Methods

exportTracingEvent
Direct link to exporttracingevent

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

Exports a tracing event to Datadog LLM Observability.

onFeedbackEvent
Direct link to onfeedbackevent

async onFeedbackEvent(event: FeedbackEvent): Promise<void>

Exports a feedback event to Datadog LLM Observability. See Feedback export.

flush
Direct link to flush

async flush(): Promise<void>

Force flushes any buffered spans to Datadog without shutting down the exporter. Useful in serverless environments where you need to ensure spans are exported before the runtime terminates.

The flush is best-effort: dd-trace sends LLM Observability data from a background writer and gives no way to await the request, so flush() waits flushGraceMs (default 2000 ms) after asking dd-trace to flush. Each flush that sent new data costs that full wait, so a serverless handler that awaits flush() per request pays it per request. Raise it if spans from short-lived processes are missing in Datadog, or set it to 0 to skip the wait when something else keeps the process alive.

shutdown
Direct link to shutdown

async shutdown(): Promise<void>

Flushes pending data and shuts down the exporter. Cancels any pending cleanup timers and disables LLM Observability.

Usage
Direct link to Usage

Zero-config (using environment variables)
Direct link to Zero-config (using environment variables)

import { DatadogExporter } from '@mastra/datadog'

// Reads from DD_LLMOBS_ML_APP, DD_API_KEY, DD_SITE, DD_ENV
const exporter = new DatadogExporter()

Explicit configuration
Direct link to Explicit configuration

import { DatadogExporter } from '@mastra/datadog'

const exporter = new DatadogExporter({
mlApp: 'my-llm-app',
apiKey: process.env.DD_API_KEY,
site: 'datadoghq.com',
env: 'production',
})

With local Datadog agent
Direct link to With local Datadog agent

const exporter = new DatadogExporter({
mlApp: 'my-llm-app',
agentless: false, // Use local Datadog Agent
env: 'production',
})

Span mapping
Direct link to Span mapping

Mastra span types are mapped to Datadog LLM Observability span kinds. When your @mastra/core and @mastra/observability versions emit MODEL_INFERENCE spans (the model-inference-span feature), MODEL_INFERENCE is the llm span and MODEL_STEP becomes a workflow span that wraps it:

Mastra SpanTypeDatadog Kind
AGENT_RUNagent
MODEL_GENERATIONworkflow
MODEL_STEPllm, or workflow when MODEL_INFERENCE spans are emitted
MODEL_INFERENCEllm
TOOL_CALLtool
MCP_TOOL_CALLtool
PROVIDER_TOOL_CALLtool
WORKFLOW_RUNworkflow
All other typestask

All unmapped span types (including MODEL_CHUNK, WORKFLOW_STEP, GENERIC, and future span types) automatically default to task.

Feedback export
Direct link to Feedback export

Feedback recorded with addFeedback() is exported to Datadog as end-user feedback. Feedback with a spanId targets that span, and feedback with only a traceId targets the trace. No extra configuration is needed.

if (!mastra.observability.addFeedback) {
throw new Error('Feedback is not supported by the active observability implementation')
}

await mastra.observability.addFeedback({
traceId,
feedback: {
feedbackType: 'thumbs',
value: 'down',
comment: 'Wrong answer',
feedbackUserId: 'user-123',
metadata: { datadog: { assessment: 'fail' } },
},
})

The Datadog LLM Observability trace ID is the Mastra trace ID, and each Datadog span ID is the Mastra span ID. Feedback reaches its trace or span even when it arrives hours after the run or from another process. You can also search Datadog for the traceId that a run returns.

Mastra feedback fieldDatadog feedback field
traceIdtrace_id, the target when there is no spanId
spanIdspan_id, the target span
feedbackTypelabel, with each . changed to _
valuescore_value for numbers, text_value for comment and correction feedback, categorical_value for other strings
commentreasoning
feedbackUserId (or metadata.userId)submitter.id, or anonymous when neither is set or feedbackUserId is null
feedbackSourcefeedback_source tag
metadata.datadog.assessmentassessment, when the value is pass or fail

The assessment is optional. Your app decides which feedback passes or fails, and Mastra doesn't derive it from the value. Set it on the feedback, not on the run, because feedback can inherit the metadata of the run it rates. To find failed feedback with the thumbs label in Datadog, search for @feedback.thumbs.assessment:fail.

Feedback without a traceId is dropped. Feedback without a spanId can't reach a run that joined another LLM Observability trace, such as a run inside your own llmobs.trace() call or one continued from an upstream service. Those runs keep the Datadog trace ID that dd-trace gives them.

Score export
Direct link to Score export

Scores recorded with mastra.observability.addScore() are exported to Datadog as evaluations on the span they rate. A score reaches its span even when it arrives after the run or from another process. A late score can't reach a run that joined another LLM Observability trace.

Environment variables
Direct link to Environment variables

The exporter reads configuration from these environment variables:

VariableDescription
DD_API_KEYDatadog API key
DD_LLMOBS_ML_APPML application name
DD_SITEDatadog site
DD_ENVEnvironment name
DD_LLMOBS_AGENTLESS_ENABLEDSet to 'false' or '0' to use local agent