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

OpenTelemetry数据导出至NewRelic GRPC端点卡在otlptrace.New步骤求助

问题排查与解决方法

核心错误根因

你代码里的gRPC连接配置存在两处直接导致阻塞的错误:

  1. 配置了HTTPS端点的同时开启了WithInsecure()关闭TLS加密,NewRelic的OTLP gRPC端口4317强制要求TLS连接,握手失败会导致连接无法建立
  2. 加了grpc.WithBlock()配置,该选项会让otlptrace.New方法一直阻塞到连接成功,没有超时中断逻辑的前提下就会出现一直卡着的现象
  3. WithEndpoint参数不需要携带https://协议前缀,gRPC客户端会自动基于TLS配置处理协议

分步排查方案

  • 修正基础配置:删除WithInsecure()配置项,将端点改为不带协议的格式otlp.nr-data.net:4317,如果你的NewRelic账号属于欧盟区域,端点替换为otlp.eu01.nr-data.net:4317
  • 确认API Key类型:你使用的必须是NewRelic的Ingest License Key,而不是个人用户级别的Personal API Key,类型不匹配会导致认证被拒绝
  • 验证网络连通性:在运行代码的主机上执行telnet otlp.nr-data.net 4317确认端口可通,如果处于内网环境需要配置代理,要在gRPC DialOption中添加代理配置
  • 开启调试日志:启动程序前设置环境变量OTEL_LOG_LEVEL=debug,可以打印出详细的连接错误信息,方便定位具体问题
  • 可选优化:如果不需要初始化阶段等待连接建立,可以删除grpc.WithBlock()配置,连接失败会在后台自动重试,不会阻塞主流程

修正后的代码示例

var headers = map[string]string{
    "api-key": "替换为你的NewRelic Ingest License Key",
}

var clientOpts = []otlptracegrpc.Option{
    otlptracegrpc.WithEndpoint("otlp.nr-data.net:4317"),
    // 移除了WithInsecure配置
    otlptracegrpc.WithReconnectionPeriod(2 * time.Second),
    // 不需要阻塞连接的话可以删掉下面这行
    otlptracegrpc.WithDialOption(grpc.WithBlock()),
    otlptracegrpc.WithTimeout(30 * time.Second),
    otlptracegrpc.WithHeaders(headers),
    otlptracegrpc.WithCompressor("gzip"),
}

otlpExporter, err := otlptrace.New(ctx, otlptracegrpc.NewClient(clientOpts...))
if err != nil {
    return nil, fmt.Errorf("creating OTLP trace exporter: %w", err)
}

resource, _ := g.Config.resource(ctx)
tracerProvider := trace.NewTracerProvider(
    trace.WithSampler(trace.AlwaysSample()),
    trace.WithBatcher(otlpExporter),
    trace.WithResource(resource),
)

otel.SetTracerProvider(tracerProvider)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 12:06:05