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

如何通过OTEL获取NextJS容器应用的Azure App Insights实时指标?

Next.js容器化部署Azure Container App,App Insights Live Metrics无法连接的解决办法

问题根源

Azure App Insights的**Live Metrics(实时指标)**依赖专属WebSocket通道实现双向实时通信,而你当前使用的@azure/monitor-opentelemetry-exporter仅支持批量上报追踪、指标和日志,不包含Live Metrics所需的实时通信能力——这就是普通数据能正常上传,但Live Metrics显示无法连接的核心原因。

关于你提到的Azure文档矛盾:Container App本身支持向App Insights推送Live Metrics,但必须采用正确的集成方式,而非单纯依赖OpenTelemetry标准导出器。

解决办法

办法1:使用App Insights官方Node.js SDK(兼容Next.js)

官方SDK原生支持Live Metrics,配置简单,且能与OpenTelemetry共存(需避免Provider冲突):

  1. 安装依赖:
npm install applicationinsights
  1. 在instrumentation.ts中优先初始化SDK(需在OpenTelemetry配置前执行,或隔离Provider):
import * as appInsights from 'applicationinsights';

// 初始化并开启Live Metrics
appInsights.setup(process.env.APPLICATIONINSIGHTS_CONNECTION_STRING)
  .setAutoCollectLiveMetrics(true) // 开启实时指标的关键配置
  .setAutoCollectDependencies(true)
  .setAutoCollectRequests(true)
  .start();

// 保留原有OpenTelemetry配置,但注意不要重复注册全局Provider
// 若需同时向Axiom导出追踪数据,为Axiom单独配置TracerProvider,避免覆盖全局实例

办法2:修复@azure/monitor-opentelemetry的重复注册问题

若坚持使用OpenTelemetry集成包,可通过隔离实例解决冲突:

  1. 清理依赖,仅保留集成包:
npm uninstall @azure/monitor-opentelemetry-exporter
npm install @azure/monitor-opentelemetry
  1. 创建独立的Azure Monitor SDK实例,不覆盖全局Provider(避免与Axiom冲突):
import { createAzureMonitorOpenTelemetrySdk } from '@azure/monitor-opentelemetry';

// 单独初始化Azure Monitor SDK并开启Live Metrics
const azureMonitorSdk = createAzureMonitorOpenTelemetrySdk({
  connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
  liveMetrics: {
    enabled: true
  }
});

// 为Axiom单独配置专属TracerProvider,不要使用全局的trace.setGlobalTracerProvider

容器App额外检查项

确保你的Container App满足以下条件:

  • 已正确配置APPLICATIONINSIGHTS_CONNECTION_STRING环境变量
  • 容器网络允许出站WebSocket连接(默认环境已允许,自定义网络规则需额外检查)
  • Node.js版本为16及以上(低版本对SDK兼容性较差)

重要提示

  • OpenTelemetry标准生态暂未原生支持App Insights的Live Metrics,必须依赖Azure专属SDK或集成包
  • 同时向多个导出器(如Axiom+App Insights)发送数据时,禁止重复注册全局Provider,需使用独立实例分别配置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 18:05:56