Setting Up the Vercel Log Drain for Datadog

By
Jordan Rivera

The Vercel log drain is one of the fastest paths to production-grade observability for Next.js applications. This guide walks you through connecting it to Datadog and configuring trace correlation so every log entry links back to its APM span.

Step 1: Create the Datadog Log Drain Endpoint

In Datadog, navigate to Logs > Configuration > Indexes and make sure you have an active index. Then go to your Vercel Project Settings > Log Drains and click Add Drain. Select Datadog as the destination and enter your Datadog API key and the regional intake endpoint for your Datadog site.

Step 2: Emit Structured JSON Logs

Vercel forwards anything written to stdout/stderr from your server functions. For Datadog to parse and index your logs correctly, they need to be valid JSON objects. Use the structuredLog helper from MarTech Pulse's telemetry library, or write your own that outputs a JSON object with at minimum: timestamp, level, message, and service fields.

Step 3: Add Trace Correlation Fields

To link log entries to APM traces, include dd.trace_id and dd.span_id fields in your log output using the decimal representation of the 128-bit trace ID and 64-bit span ID. With @vercel/otel, these values are available via trace.getActiveSpan().spanContext(). Once correlated, clicking a log entry in Datadog Logs will show a "View in APM" link that jumps directly to the corresponding trace.