You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用OpenTelemetry Collector将Next.js追踪数据发送至Datadog

问题排查与解决

注释filelog不是追踪数据未发送到Datadog的原因——filelog是日志管道的接收器,和追踪数据的采集、发送完全无关。你的问题出在以下几个核心环节:


1. 应用未将追踪数据发送至OTEL Collector

你的instrumentation.ts里使用的是ConsoleSpanExporter,这个导出器只会将追踪数据打印到应用控制台,根本不会把数据发送到OpenTelemetry Collector。你需要替换为OTLP导出器,配置指向Collector的地址:

步骤:

  1. 安装OTLP导出依赖:
npm install @opentelemetry/exporter-trace-otlp-http @opentelemetry/exporter-metrics-otlp-http
  1. 修改instrumentation.ts:
/*instrumentation.ts*/
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import {
  PeriodicExportingMetricReader,
  OTLPMetricExporter,
} from '@opentelemetry/sdk-metrics';

// Mac环境下,Docker容器内的服务访问宿主机用host.docker.internal
const traceExporter = new OTLPTraceExporter({
  url: 'http://host.docker.internal:4318/v1/traces',
});

const metricExporter = new OTLPMetricExporter({
  url: 'http://host.docker.internal:4318/v1/metrics',
});

const sdk = new NodeSDK({
  traceExporter,
  metricReader: new PeriodicExportingMetricReader({
    exporter: metricExporter,
  }),
  instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();

2. OTEL Collector的OTLP接收器仅允许容器内部访问

你的collector.yaml中OTLP接收器绑定的是localhost:4317/4318,在Docker容器运行时,这个绑定只对容器内部生效,宿主机的应用无法连接。需要修改为绑定0.0.0.0,允许外部访问:

receivers:
  otlp:
    protocols:
      http:
        endpoint: "0.0.0.0:4318" # 替换原localhost
      grpc:
        endpoint: "0.0.0.0:4317" # 替换原localhost

3. Next.js未启用Instrumentation Hook

Next.js默认不会自动加载instrumentation.ts,需要在next.config.js中显式启用:

/** @type {import('next').NextConfig} */
const nextConfig = {
  instrumentationHook: true,
  // 你的其他Next.js配置
};

module.exports = nextConfig;

完成以上修改后,重启OTEL Collector和Next.js应用,追踪数据应该就能正常发送到Datadog了。

内容的提问来源于stack exchange,提问作者keiji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:02:01