Send traces to Datadog
Six steps to agent traces in Datadog and its LLM Observability, through the agentless OTLP intake or dd-trace's provider.
Six steps, from the packages to one trace per turn in Datadog: the same six on every backend’s page. The framework picker above the code picks your routes; the backend stays picked for the pages after this one.
Install the packages
The telemetry layer and its OpenTelemetry packages. Datadog’s agentless intake takes OTLP over HTTP as JSON or protobuf (not gRPC), so the built-in JSON exporter needs nothing more.
npm install @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otelpnpm add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otelyarn add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otelbun add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otelnub add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otelaube add @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otelSet the environment
# Datadog agentless OTLP intake (datadoghq.com; EU: otlp.datadoghq.eu)
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://otlp.datadoghq.com/v1/traces
OTEL_EXPORTER_OTLP_TRACES_HEADERS=dd-api-key=<DD_API_KEY>,compute_stats=true
OTEL_SERVICE_NAME=field-store
# Turns the traces into LLM Observability traces too.
OTEL_RESOURCE_ATTRIBUTES=dd_llmobs_enabled=true- Endpoint:
https://otlp.<site>/v1/traces, for the sitesotlp.datadoghq.com,otlp.datadoghq.eu,otlp.us5.datadoghq.comandotlp.ddog-gov.com. - Headers:
dd-api-key: <key>.compute_stats=truecomputes trace metrics, which the direct intake does not do by default. - A Datadog Agent receives OTLP on 4317 and 4318: point
OTEL_EXPORTER_OTLP_ENDPOINTat it instead, and the Agent holds the key.
Add the server layer
The endpoint and key come from the environment; the layer adds the key LLM Observability builds sessions from. Keep the default conventions: "genai": LLM Observability maps the AI SDK’s older ai.* spans only partly.
import { defineObservability } from "@webmcp-agents/telemetry/server";
// The endpoint and key come from the environment.
export const observability = defineObservability({
// LLM Observability builds sessions from this key.
metadataMapping: {
"webmcp.conversation.id": "gen_ai.conversation.id",
},
});In Astro, process.env does not hold .env.local in development: read the variables with getSecret from astro:env/server and pass them as env, as the Astro view of the generic layer does. The chat route passes the layer to the handler:
import { createChatHandler } from "@webmcp-agents/chat/server";
import { observability } from "../src/lib/observability";
import { agents } from "../src/agents";
const handler = createChatHandler({ agents, observability });
export const GET = handler;
export const POST = handler;import { createChatHandler } from "@webmcp-agents/chat/server";
import { agents } from "@/agents";
import { observability } from "@/lib/observability";
const handler = createChatHandler({ agents, observability });
export const GET = handler;
export const POST = handler;import { createFileRoute } from "@tanstack/react-router";
import { createChatHandler } from "@webmcp-agents/chat/server";
import { agents } from "@/agents";
import { observability } from "@/lib/observability";
const handler = createChatHandler({ agents, observability });
export const Route = createFileRoute("/api/chat")({
server: {
handlers: {
GET: ({ request }) => handler(request),
POST: ({ request }) => handler(request),
},
},
});import type { APIRoute } from "astro";
import { getSecret } from "astro:env/server";
import { createChatHandler } from "@webmcp-agents/chat/server";
import { agents } from "@/agents";
import { observability } from "@/lib/observability";
export const prerender = false;
const handler = createChatHandler({
agents,
observability,
env: {
ANTHROPIC_API_KEY: getSecret("ANTHROPIC_API_KEY"),
WEB_AGENTS_MODEL: getSecret("WEB_AGENTS_MODEL"),
},
});
export const GET: APIRoute = ({ request }) => handler(request);
export const POST: APIRoute = ({ request }) => handler(request);With dd-trace
If the app already runs dd-trace, register its OpenTelemetry provider, before any other import, and the layer uses it (rule 3):
import tracer from "dd-trace";
// Loaded before anything else (node --import ./tracer.js): dd-trace
// as the process's OpenTelemetry provider, which the layer finds.
const dd = tracer.init({ service: "field-store" });
new dd.TracerProvider().register();Add the relay route
The page’s spans come to your own endpoint, so the backend’s key never reaches the browser. The relay refuses other origins and requests with no Origin (a script, not a page); the guard is the per-IP limit for the rest. Keep its counts in a shared store in production, as for the chat (Limits and budgets).
import { observability } from "../src/lib/observability";
// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).
// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
const who = req.headers.get("x-forwarded-for") ?? "local";
const now = Date.now();
const last = seen.get(who);
const entry =
last && now - last.since < 60_000 ? last : { since: now, count: 0 };
entry.count++;
seen.set(who, entry);
// about one batch per run: 60 a minute is plenty for a person
if (entry.count <= 60) return undefined;
return new Response(null, { status: 429 });
};
export const POST = observability.relay({ guard: perIp });import { observability } from "@/lib/observability";
// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).
// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
const who = req.headers.get("x-forwarded-for") ?? "local";
const now = Date.now();
const last = seen.get(who);
const entry =
last && now - last.since < 60_000 ? last : { since: now, count: 0 };
entry.count++;
seen.set(who, entry);
// about one batch per run: 60 a minute is plenty for a person
if (entry.count <= 60) return undefined;
return new Response(null, { status: 429 });
};
export const POST = observability.relay({ guard: perIp });import { createFileRoute } from "@tanstack/react-router";
import { observability } from "@/lib/observability";
// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).
// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
const who = req.headers.get("x-forwarded-for") ?? "local";
const now = Date.now();
const last = seen.get(who);
const entry =
last && now - last.since < 60_000 ? last : { since: now, count: 0 };
entry.count++;
seen.set(who, entry);
// about one batch per run: 60 a minute is plenty for a person
if (entry.count <= 60) return undefined;
return new Response(null, { status: 429 });
};
const relay = observability.relay({ guard: perIp });
export const Route = createFileRoute("/api/telemetry")({
server: {
handlers: {
POST: ({ request }) => relay(request),
},
},
});import type { APIRoute } from "astro";
import { observability } from "@/lib/observability";
export const prerender = false;
// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).
// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
const who = req.headers.get("x-forwarded-for") ?? "local";
const now = Date.now();
const last = seen.get(who);
const entry =
last && now - last.since < 60_000 ? last : { since: now, count: 0 };
entry.count++;
seen.set(who, entry);
// about one batch per run: 60 a minute is plenty for a person
if (entry.count <= 60) return undefined;
return new Response(null, { status: 429 });
};
const relay = observability.relay({ guard: perIp });
export const POST: APIRoute = ({ request }) => relay(request);Observe the page
One component turns each run and tool call on the page into spans and posts them to the relay. Render it once, inside <WebAgentsProvider>, next to the chat. Without React, call observeAgents() once instead.
import { ObserveAgents } from "@webmcp-agents/telemetry/react";
import { consent } from "@/lib/consent";
// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
return <ObserveAgents enabled={() => consent.analytics} />;
}"use client";
import { ObserveAgents } from "@webmcp-agents/telemetry/react";
import { consent } from "@/lib/consent";
// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
return <ObserveAgents enabled={() => consent.analytics} />;
}import { ObserveAgents } from "@webmcp-agents/telemetry/react";
import { consent } from "@/lib/consent";
// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
return <ObserveAgents enabled={() => consent.analytics} />;
}import { ObserveAgents } from "@webmcp-agents/telemetry/react";
import { consent } from "@/lib/consent";
// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
return <ObserveAgents enabled={() => consent.analytics} />;
}Check it in Datadog
Start the app and send one message in the chat. The trace appears in APM and, with dd_llmobs_enabled=true, in LLM Observability, which builds sessions from gen_ai.conversation.id. A 403 from the intake means its host does not match your site.
Sources, and what was not confirmed
- OTLP intake endpoint
- OTLP traces
- LLM Observability with OpenTelemetry
- OTLP in the Agent
- OpenTelemetry API support in dd-trace (Node.js)
Not confirmed: the intake hosts of US3, AP1 and AP2 (they follow the same pattern).
Next
The telemetry APIs
What you just set up, one API per page: the layer, where spans go, the relay, the page’s observer, sampling and the conversation key, each showing what differs for Datadog.