从 Application Insights Classic API SDK 迁移到 Azure Monitor OpenTelemetry

本指南提供将应用程序从 Application Insights SDK(Classic API) 迁移到 Azure Monitor OpenTelemetry 的分步说明。

使用 Azure Monitor OpenTelemetry 检测工具时,你会获得类似于使用 Application Insights SDK 的体验。 有关详细信息和功能比较,请参阅 功能的发布状态

小窍门

若要查看存档.NET或 Node.js 经典 API SDK 信息,请参阅 API 2.x

使用 Application Insights .NET软件开发工具包 (SDK) 3.x 从 Application Insights .NET SDK 2.x 升级到基于 OpenTelemetry(OTel)的实现。 3.x SDK 保留大多数 TelemetryClientTelemetryConfiguration 应用程序编程接口(API),并使用 Azure Monitor OpenTelemetry 导出程序将遥测数据发送到 Application Insights。

大多数经典 Track* 调用在升级后继续工作,但它们通过发出 OpenTelemetry 信号的内部映射层进行路由。

如果生成新应用程序或已使用 Azure Monitor OpenTelemetry 发行版,请改用 Azure Monitor OpenTelemetry Distro。 不要在同一应用程序中使用 Application Insights .NET SDK 3.x 和 Azure Monitor OpenTelemetry Distro。

Application Insights .NET SDK 3.x 概述

Application Insights .NET SDK 3.x 提供以下 NuGet 包:

  • Microsoft.ApplicationInsightsTelemetryClientTelemetryConfiguration
  • Microsoft.ApplicationInsights.AspNetCore 用于 ASP.NET(Active Server Pages .NET)Core Web应用
  • 用于工作服务与控制台应用程序的 Microsoft.ApplicationInsights.WorkerService
  • 在 .NET Framework 上的 ASP.NET 应用的 Microsoft.ApplicationInsights.Web
  • 用于 NLog 集成的 Microsoft.ApplicationInsights.NLogTarget (测试版)

使用存储库文档获取代码示例和详细的迁移指南:

升级到 3.x

步骤 1:删除对不兼容包的引用

删除这些包,因为它们与 SDK 3.x 不兼容:

  • Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel
  • Microsoft.ApplicationInsights.DependencyCollector
  • Microsoft.ApplicationInsights.EventCounterCollector
  • Microsoft.ApplicationInsights.PerfCounterCollector
  • Microsoft.ApplicationInsights.WindowsServer
  • Microsoft.Extensions.Logging.ApplicationInsights
  • Microsoft.ApplicationInsights.Log4NetAppender
  • Microsoft.ApplicationInsights.TraceListener
  • Microsoft.ApplicationInsights.DiagnosticSourceListener
  • Microsoft.ApplicationInsights.EtwCollector
  • Microsoft.ApplicationInsights.EventSourceListener

SDK 3.x 不会发布这些包的 3.x 版本。 请改用 Application Insights .NET SDK 3.x 概述中列出的支持的 3.x 包。 以下部分介绍了这些包的预期替换项。 在某些情况下,该功能内置于支持的 3.x 包中,或由 OpenTelemetry API 替换。

注释

此列表仅包括Microsoft包。 如果使用依赖于 Microsoft.ApplicationInsights 2.x 的第三方包(例如,Serilog.Sinks.ApplicationInsights),请验证这些包在升级之前是否支持 SDK 3.x。 按照包维护者的指南进行操作。

步骤 2:将包版本升级到 3.x

将任何剩余支持的 Application Insights 包升级到最新的 3.x 版本。

重要

不要在同一应用程序中混合 Application Insights 2.x 和 3.x 包。 将所有 Application Insights 包引用一起升级。

步骤 3:更新代码和配置以适应重大更改

请查看重大变更参考文档和详细的迁移指南。 大多数应用程序需要更新以下一个或多个区域中的代码或配置:

2.x API、设置或模式 3.x 指南
TrackPageView 删除 TrackPageView 调用。 .NET 3.x SDK 中删除了页面视图跟踪。
TrackEventTrackExceptionTrackAvailability的重载包含IDictionary<string, double> metrics 删除自定义指标参数。 使用TrackMetric()单独跟踪指标。
GetMetric 使用 MetricConfigurationMetricAggregationScope 请使用 GetMetric 的简化重载。 指标配置和聚合在 SDK 3.x 内部管理。
InstrumentationKey 配置或 TelemetryClient.InstrumentationKey 使用 TelemetryConfiguration.ConnectionString 并提供连接字符串,而不是仪器密钥。 SDK 3.x 需要连接字符串,如果未配置,则启动时可能会失败。 对于测试场景,可以使用虚拟连接字符串,例如InstrumentationKey=00000000-0000-0000-0000-000000000000
TelemetryClient()TelemetryConfiguration.Active 使用 TelemetryConfiguration.CreateDefault()显式创建配置,并将其传递给 new TelemetryClient(config)
TelemetryModuleTelemetryInitializerTelemetryProcessor 自定义 应将自定义初始化器或处理器迁移到基于 OpenTelemetry 的处理器。 应删除对内置 2.x 处理器、初始值设定项和模块的引用。 有关详细信息,请参阅 迁移指南
ITelemetryChannelTelemetryConfiguration.TelemetryChannel 由于 3.x 在内部合并了Azure Monitor导出程序,因此删除了经典通道抽象。 对于测试,请使用兼容 OpenTelemetry 的验证,例如内存导出器。
EnableAdaptiveSampling 将自适应采样替换为 TracesPerSecondSamplingRatio
面向 .NET Framework 4.5.2 的 Microsoft.ApplicationInsights.Web 目标.NET Framework 4.6.2 或更高版本。
指标名称和命名空间约定 若要遵循 OpenTelemetry 仪器命名语法,请更新 namemetricIdmetricNamespace 的值以及用于 TrackMetric()GetMetric()MetricIdentifier 的值。 指标名称和命名空间必须以字母开头,并且只能包含字母、数字、_.-/。 不允许包含空格。

替换被移除的可扩展性点

Application Insights .NET SDK 2.x 提供了特定于 Application Insights 的扩展性类型,例如遥测模块、初始值设定项、处理器和通道。 Application Insights .NET SDK 3.x 改用 OpenTelemetry 扩展性。

有关替换 2.x 扩展点(包括边缘情况)的详细指南,请参阅 迁移指南

小窍门

基于资源的值(如角色元数据)可以流经 OpenTelemetry 资源映射,而不是在每个遥测项上显示。 如果需要每个遥测项上的键值对,请使用 GlobalProperties 或自定义处理器。

配置采样

Application Insights .NET SDK 3.x 支持两种跟踪采样模式(请求和依赖项):

  • 为基于百分比的采样设置 SamplingRatio (0.0 到 1.0)。
  • TracesPerSecond设置为限速采样(默认值:每秒 5 个轨迹)。

SDK 3.x 对请求和依赖项应用相同的采样设置。 SDK 3.x 不支持请求和依赖项的单独采样设置。

在请求或依赖项被纳入采样时,SDK 3.x 默认会将父级跟踪的采样决策应用于相关日志。 若要禁用该行为,请设置为 EnableTraceBasedLogsSamplerfalse.

可以在SamplingRatioTracesPerSecondEnableTraceBasedLogsSampler中设置TelemetryConfigurationappsettings.jsonapplicationinsights.config

排查升级问题

使用以下步骤在升级到 SDK 3.x 期间验证遥测数据:

  • 确认在启动之前配置了完整的连接字符串。 如果在测试中验证遥测数据而不使用实际资源,请使用测试用连接字符串。
  • 收集 Application Insights 自我诊断日志,以识别配置错误和导出程序故障。
  • 添加 OpenTelemetry 控制台导出程序,以验证跟踪、指标和日志在依赖 Azure Monitor 摄取之前是否按照预期发出。
  • 如果之前通过模拟 ITelemetryChannel 对遥测进行了单元测试,请切换到适用于 OpenTelemetry 的验证,例如内存中导出程序或用于非生产环境的测试导出程序。
  • 通过验证父子跟踪决策,确认采样设置的行为符合预期。
  • 验证服务名称、角色名称、角色实例和环境等资源属性,以确保在 Application Insights 中正确归属。
  • 如果迁移了自定义扩充,请验证属性是否显示在预期位置。 基于资源的映射可能与 2.x 行为不同。

有关详细的故障排除指南和示例,请使用以下资源:

OpenTelemetry 术语

有关术语,请参阅 OpenTelemetry 规范中的术语表

下表突出显示了 Application Insights 中使用的旧术语及其 OpenTelemetry 替换项。

Application Insights OpenTelemetry
自动收集器 仪表库
频道 出口商
无代码/基于代理 自动检测
Traces 日志
请求 服务器跨度
依赖关系 其他范围类型(客户端、内部等)
操作 ID 跟踪 ID
ID 或父级操作 ID 范围 ID