Skip to main content

DatadogBridge

warning

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

Enables bidirectional integration between Mastra tracing and Datadog. Creates native dd-trace APM spans in real time so that auto-instrumented operations inside tools and processors are correctly nested under their parent Mastra span. Emits LLM Observability data through dd-trace's pipeline when spans end.

Constructor
Direct link to Constructor

new DatadogBridge(config?: DatadogBridgeConfig)

DatadogBridgeConfig
Direct link to datadogbridgeconfig

interface DatadogBridgeConfig extends BaseExporterConfig {
apiKey?: string
mlApp?: string
site?: string
service?: string
env?: string
agentless?: boolean
integrationsEnabled?: boolean
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

createSpan
Direct link to createspan

createSpan(options: CreateSpanOptions<SpanType>): SpanIds | undefined

Called by the Mastra observability instance during span construction. Creates a dd-trace APM span eagerly via tracer.startSpan() and returns Mastra-compatible identifiers. The returned IDs are used by Mastra throughout the span's lifetime; the dd-trace span object is stored internally and used for scope activation.

Returns: SpanIds | undefined - { spanId, traceId, parentSpanId }, or undefined if the bridge is disabled.

executeInContext
Direct link to executeincontext

executeInContext<T>(spanId: string, fn: () => Promise<T>): Promise<T>

Executes an async function within the dd-trace context of a Mastra span. dd-trace auto-instrumented operations running inside the function (HTTP, database, etc.) will be parented to this span.

Returns: Promise<T> - The result of the function execution.

executeInContextSync
Direct link to executeincontextsync

executeInContextSync<T>(spanId: string, fn: () => T): T

Executes a synchronous function within the dd-trace context of a Mastra span.

Returns: T - The result of the function execution.

flush
Direct link to flush

async flush(): Promise<void>

Force-flushes any buffered LLM Observability data to Datadog without shutting down the bridge. Useful in serverless environments where you need to ensure data is exported before the runtime terminates.

shutdown
Direct link to shutdown

async shutdown(): Promise<void>

Force-finishes any APM spans that weren't properly closed, flushes pending LLM Observability data, disables the LLM Observability integration, and clears all internal state.

Usage examples
Direct link to Usage examples

Basic Usage
Direct link to Basic Usage

import tracer from 'dd-trace'

tracer.init({
service: process.env.DD_SERVICE || 'my-mastra-app',
env: process.env.DD_ENV || 'production',
})

import { Mastra } from '@mastra/core'
import { Observability } from '@mastra/observability'
import { DatadogBridge } from '@mastra/datadog'

const mastra = new Mastra({
observability: new Observability({
configs: {
default: {
serviceName: 'my-mastra-app',
bridge: new DatadogBridge({
mlApp: process.env.DD_LLMOBS_ML_APP!,
}),
},
},
}),
agents: { myAgent },
})

Agentless Mode (LLM Observability only, no local agent)
Direct link to Agentless Mode (LLM Observability only, no local agent)

If you don't have a local Datadog Agent and only want LLM Observability data, enable agentless mode:

new DatadogBridge({
mlApp: process.env.DD_LLMOBS_ML_APP!,
apiKey: process.env.DD_API_KEY!,
agentless: true,
})

Note: APM data cannot be sent in agentless mode. If you only need LLM Observability data without dd-trace APM, the Datadog Exporter is a simpler choice.

With Additional Exporters
Direct link to With Additional Exporters

The bridge can be combined with non-Datadog exporters to send traces to additional destinations:

import { Mastra } from '@mastra/core'
import { Observability, MastraStorageExporter } from '@mastra/observability'
import { DatadogBridge } from '@mastra/datadog'

const mastra = new Mastra({
observability: new Observability({
configs: {
default: {
serviceName: 'my-mastra-app',
bridge: new DatadogBridge({
mlApp: process.env.DD_LLMOBS_ML_APP!,
}),
exporters: [
new MastraStorageExporter(), // Studio access
],
},
},
}),
})
note

Don't combine DatadogBridge with DatadogExporter in the same configuration — both emit to LLM Observability and would double-write the same data.

Setup requirements
Direct link to Setup requirements

The DatadogBridge requires dd-trace to be initialized before any other imports so its auto-instrumentation can patch HTTP, database, and framework libraries at load time.

See the DatadogBridge Guide for complete setup instructions, including dd-trace initialization, bundler externals, and agent configuration.

Span mapping
Direct link to 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
WORKFLOW_RUNworkflow
All other typestask

Tags support
Direct link to Tags support

tracingOptions.tags values become structured LLM Observability annotation tags: key:value entries are split into key/value pairs, and tags without a colon are set to true.

const result = await agent.generate('Hello', {
tracingOptions: {
tags: ['production', 'instance_name:career-scout-api'],
},
})

This produces:

{
"production": true,
"instance_name": "career-scout-api"
}

Environment variables
Direct link to Environment variables

The bridge reads configuration from these environment variables:

VariableDescription
DD_API_KEYDatadog API key (only required for agentless mode)
DD_LLMOBS_ML_APPML application name
DD_SITEDatadog site
DD_ENVEnvironment name
DD_LLMOBS_AGENTLESS_ENABLEDSet to 'true' or '1' to enable agentless mode