Compartilhar via

从 Application Insights 经典 API SDK 迁移到 Azure Monitor OpenTelemetry

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

与使用 Application Insights SDK 时一样,可以期待通过 Azure Monitor OpenTelemetry 监测获得类似的体验。 有关详细信息和功能比较,请参阅 功能的发布状态

使用 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.ApplicationInsights 适用于 TelemetryClientTelemetryConfiguration
  • Microsoft.ApplicationInsights.AspNetCore for ASP.NET (Active Server Pages .NET)核心 Web 应用程序
  • Microsoft.ApplicationInsights.WorkerService 适用于后台服务和控制台应用程序
  • 用于 .NET Framework 上的 ASP.NET 应用程序的 Microsoft.ApplicationInsights.Web
  • Microsoft.ApplicationInsights.NLogTarget 用于 NLog 集成 (beta)

使用存储库文档获取代码示例和 OpenTelemetry 集成详细信息:

升级到 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 包。

注释

此列表仅包括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:更新代码和配置以适应重大更改

请查看重大变更参考资料,删除或替换不再受支持的 API 和设置。

最常见的更改包括:

  • 删除 TrackPageView 调用。
  • 更新 Track* 调用以删除自定义指标参数。
  • 使用 TelemetryConfiguration.ConnectionString 将监视密钥配置替换为完整的连接字符串。
  • TelemetryModuleTelemetryInitializerTelemetryProcessor 自定义项替换为 OpenTelemetry 处理器、检测工具库和资源检测器。 此类 ApplicationInsightsServiceOptions 包括 EnableQuickPulseMetricStreamEnablePerformanceCounterCollectionModuleEnableDependencyTrackingTelemetryModuleEnableRequestTrackingTelemetryModule。 这些 ApplicationInsightsServiceOptions 设置配置导出程序行为,并且不使用 TelemetryModule 实现。
  • 将自适应采样 (EnableAdaptiveSampling) 替换为 TracesPerSecondSamplingRatio
  • 面向使用Microsoft.ApplicationInsights.Web的 ASP.NET 应用程序,应选择 .NET Framework 4.6.2 或更高版本。

替换被移除的可扩展性点

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

  • 使用 OpenTelemetry 检测和配置选项来控制自动收集。
  • 使用 OpenTelemetry 处理器丰富或筛选遥测数据。

SDK 3.x 仅保留一部分 TelemetryContext 属性。 可以在单个遥测项上设置这些属性:

背景 属性
User IdAuthenticatedUserIdUserAgent
Operation Name
Location Ip
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 中正确归属。

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