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

DatadogExporter

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

备注

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
Constructor的直接链接

new DatadogExporter(config: DatadogExporterConfig)

DatadogExporterConfig
datadogexporterconfig的直接链接

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

Extends BaseExporterConfig, which includes:

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

Methods
Methods的直接链接

exportTracingEvent
exporttracingevent的直接链接

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

Exports a tracing event to Datadog LLM Observability.

flush
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
shutdown的直接链接

async shutdown(): Promise<void>

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

Usage
Usage的直接链接

Zero-Config (using environment variables)
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
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
With Local Datadog Agent的直接链接

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

Span mapping
Span mapping的直接链接

Mastra span types are mapped to Datadog LLM Observability span kinds:

Mastra SpanTypeDatadog Kind
AGENT_RUNagent
MODEL_GENERATIONworkflow
MODEL_STEPllm
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.

Environment variables
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