排查 .NET 自动检测问题
通用步骤
如果您在使用 OpenTelemetry .NET 自动检测时遇到任何问题,以下步骤可以帮助您理解问题。
启用详细日志记录
详细的调试日志可以帮助您排查检测问题,并可以附加到此项目的 issue 中以便进行调查。
要从 OpenTelemetry .NET 自动检测中获取详细日志,请在被检测进程启动前将 OTEL_LOG_LEVEL 环境变量设置为 debug。
默认情况下,该库会将日志文件写入预定义的 位置。如果需要,可以通过更新 OTEL_DOTNET_AUTO_LOG_DIRECTORY 环境变量来更改默认位置。
获取日志后,请移除 OTEL_LOG_LEVEL 环境变量,或将其设置为不那么详细的级别,以避免不必要的开销。
启用主机跟踪
主机跟踪 可用于收集信息,以调查与各种问题相关的潜在问题,例如找不到程序集。请设置以下环境变量
COREHOST_TRACE=1
COREHOST_TRACEFILE=corehost_verbose_tracing.log
然后重启应用程序以收集日志。
常见问题
未生成遥测数据
没有生成遥测数据。OpenTelemetry .NET 自动检测的内部日志 位置 中没有日志。
可能会出现 .NET Profiler 无法附加,因此不会发出任何日志的情况。
最常见的原因是,被检测的应用程序没有加载 OpenTelemetry .NET 自动检测程序集的权限。
无法安装程序包 'OpenTelemetry.AutoInstrumentation.Runtime.Native'
将 NuGet 包添加到项目中时,您会收到类似以下内容的错误消息
Could not install package 'OpenTelemetry.AutoInstrumentation.Runtime.Native 1.6.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.7.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
NuGet 包不支持旧式 csproj 项目。请将自动检测部署到机器而不是使用 NuGet 包,或者将您的项目迁移到 SDK 风格的 csproj。
性能问题
如果出现高 CPU 使用率,请确保您没有通过在系统或用户范围内设置环境变量来全局启用自动检测。
如果使用系统或用户范围是故意的,请使用 OTEL_DOTNET_AUTO_EXCLUDE_PROCESSES 环境变量将应用程序排除在自动检测之外。
dotnet CLI 工具崩溃
在运行应用程序(例如使用 dotnet run)时,您会收到类似以下内容的错误消息
PS C:\Users\Administrator\Desktop\OTelConsole-NET6.0> dotnet run My.Simple.Console
Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.TypeInitializationException: The type initializer for 'OpenTelemetry.AutoInstrumentation.Loader.Startup' threw an exception.
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
在版本 v0.6.0-beta.1 及更低版本中,检测 dotnet CLI 工具时存在一些问题。
因此,如果您使用的是这些版本之一,我们建议在检测终端会话之前执行 dotnet build,或者在单独的终端会话中调用它。
有关更多信息,请参阅 #1744。
程序集版本冲突
错误消息类似以下内容
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
File name: 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
at Microsoft.AspNetCore.Builder.WebApplicationBuilder..ctor(WebApplicationOptions options, Action`1 configureDefaults)
at Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(String[] args)
at Program.<Main>$(String[] args) in /Blog.Core/Blog.Core.Api/Program.cs:line 26
OpenTelemetry .NET NuGet 包及其依赖项随 OpenTelemetry .NET 自动检测一起部署。
要处理依赖版本冲突,请更新被检测应用程序的项目引用,以使用与 OpenTelemetry .NET 自动检测相同的版本。
一种确保不会发生此类冲突的简单方法是将 OpenTelemetry.AutoInstrumentation 包添加到您的应用程序中。有关如何将其添加到应用程序的说明,请参阅 使用 OpenTelemetry.AutoInstrumentation NuGet 包。
或者,只将冲突的程序包添加到您的项目中。以下是 OpenTelemetry .NET 自动检测使用的依赖项
在其版本可在以下位置找到
- Directory.Packages.props
- src/Directory.Packages.props
- src/OpenTelemetry.AutoInstrumentation.AdditionalDeps/Directory.Packages.props
默认情况下,.NET Framework 应用程序的程序集引用会在运行时重定向到自动检测使用的版本。此行为可以通过 OTEL_DOTNET_AUTO_NETFX_REDIRECT_ENABLED 设置进行控制。
如果应用程序已为其使用的自动检测程序集提供绑定重定向,则此自动重定向可能会失败,请参阅 #2833。检查是否存在任何现有的绑定重定向阻止重定向到 netfx_assembly_redirection.h 中列出的版本。
为了使上述自动重定向正常工作,有两种特定情况需要将用于检测 .NET Framework 应用程序的程序集(安装目录 netfx 文件夹下的程序集)也安装到全局程序集缓存 (GAC) 中
- Monkey patch instrumentation 加载为域中立的程序集。
- 如果应用程序也提供了一些在
netfx文件夹中发布的程序集的不同版本,则对强命名应用程序进行程序集重定向。
如果您在上述任一场景中遇到问题,请再次运行 PowerShell 安装模块中的 Install-OpenTelemetryCore 命令,以确保更新了所需的 GAC 安装。
有关自动检测使用 GAC 的更多信息,请参阅 pjanotti 的评论。
AdditionalDeps 中的程序集未找到
症状
您会收到类似以下内容的错误消息
An assembly specified in the application dependencies manifest (OpenTelemetry.AutoInstrumentation.AdditionalDeps.deps.json) was not found
这可能与以下问题有关
其他问题
如果您遇到本页面未列出的问题,请参阅 通用步骤 以收集其他诊断信息。这可能有助于排查问题。