入门
在不到 5 分钟内为您的应用获取遥测数据!
使用 OpenTelemetry .NET 自动检测,无需修改源代码即可从 .NET 应用程序和服务向可观测性后端发送跟踪和指标。
有关如何检测服务或应用程序代码的说明,请阅读手动检测。
OpenTelemetry .NET 自动检测应支持所有官方支持的操作系统以及 .NET 的版本。
.NET Framework 的最低支持版本是 4.6.2。
支持的处理器架构有
ARM64 版本不支持基于 CentOS 的镜像。
CI 测试在以下操作系统上运行
要自动检测 .NET 应用程序,请下载并运行适用于您操作系统的安装脚本。
下载并运行 .sh 脚本
# Download the bash script
curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -O
# Install core files
sh ./otel-dotnet-auto-install.sh
# Enable execution for the instrumentation script
chmod +x $HOME/.otel-dotnet-auto/instrument.sh
# Setup the instrumentation for the current shell session
. $HOME/.otel-dotnet-auto/instrument.sh
# Run your application with instrumentation
OTEL_SERVICE_NAME=myapp OTEL_RESOURCE_ATTRIBUTES=deployment.environment=staging,service.version=1.0.0 ./MyNetApp
在 Windows 上,请以管理员身份使用 PowerShell 模块。
需要 Windows PowerShell Desktop (v5.1)。目前不支持其他版本,包括 PowerShell Core (v6.0+)。
# PowerShell 5.1 is required
#Requires -PSEdition Desktop
# Download the module
$module_url = "https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/OpenTelemetry.DotNet.Auto.psm1"
$download_path = Join-Path $env:temp "OpenTelemetry.DotNet.Auto.psm1"
Invoke-WebRequest -Uri $module_url -OutFile $download_path -UseBasicParsing
# Import the module to use its functions
Import-Module $download_path
# Install core files (online vs offline method)
Install-OpenTelemetryCore
Install-OpenTelemetryCore -LocalPath "C:\Path\To\OpenTelemetry.zip"
# Set up the instrumentation for the current PowerShell session
Register-OpenTelemetryForCurrentSession -OTelServiceName "MyServiceDisplayName"
# Run your application with instrumentation
.\MyNetApp.exe
# You can get usage information by calling the following commands
# List all available commands
Get-Command -Module OpenTelemetry.DotNet.Auto
# Get command's usage information
Get-Help Install-OpenTelemetryCore -Detailed
使用 OpenTelemetry.DotNet.Auto.psm1 PowerShell 模块为 Windows 服务设置自动检测
# Import the module
Import-Module "OpenTelemetry.DotNet.Auto.psm1"
# Install core files
Install-OpenTelemetryCore
# Set up your Windows Service instrumentation
Register-OpenTelemetryForWindowsService -WindowsServiceName "WindowsServiceName" -OTelServiceName "MyServiceDisplayName"
Register-OpenTelemetryForWindowsService 会执行服务重启。
进行配置更改后,请记住重启 Windows 服务。您可以在 PowerShell 中运行 Restart-Service -Name $WindowsServiceName -Force 来完成此操作。
对于 .NET Framework 应用程序,您可以通过 App.config 中的 appSettings 来配置 最常见的 OTEL_ 设置(例如 OTEL_RESOURCE_ATTRIBUTES)。
另一种方法是在 Windows 注册表中为 Windows 服务设置环境变量。
给定 Windows 服务 (名称为 $svcName) 的注册表项位于
HKLM\SYSTEM\CurrentControlSet\Services\$svcName
环境变量定义在名为 Environment 的 REG_MULTI_SZ (多行注册表值) 中,格式如下:
Var1=Value1
Var2=Value2
以下说明适用于 .NET Framework 应用程序。
使用 OpenTelemetry.DotNet.Auto.psm1 PowerShell 模块为 IIS 设置自动检测
# Import the module
Import-Module "OpenTelemetry.DotNet.Auto.psm1"
# Install core files
Install-OpenTelemetryCore
# Setup IIS instrumentation
Register-OpenTelemetryForIIS
Register-OpenTelemetryForIIS 会执行 IIS 重启。
以下说明适用于 .NET Framework 应用程序。
对于 ASP.NET 应用程序,您可以通过 Web.config 中的 appSettings 来配置 最常见的 OTEL_ 设置(例如 OTEL_SERVICE_NAME)。
如果未显式配置服务名称,将为您生成一个。如果应用程序在 .NET Framework 中托管在 IIS 上,它将使用 SiteName\VirtualDirectoryPath,例如:MySite\MyApp
对于 ASP.NET Core 应用程序,您可以在 Web.config 文件的 <aspNetCore> 块中使用 <environmentVariable> 元素,通过环境变量设置配置。
进行配置更改后,请记住重启 IIS。您可以通过执行 iisreset.exe 来完成此操作。
您可以在 applicationHost.config 中添加 <environmentVariables> 来为给定的应用程序池设置环境变量。
考虑通过为 W3SVC 和 WAS Windows 服务设置环境变量,来为部署到 IIS 的所有应用程序设置通用的环境变量。
对于早于 10.0 的 IIS 版本,您可以考虑创建一个单独的用户,设置其环境变量,并将其用作应用程序池用户。
您可以使用 NuGet 包来检测 自包含 应用程序。有关更多信息,请参阅NuGet 包。
有关 Docker 容器检测的示例,请参阅 GitHub 上的示例。
您还可以使用 Kubernetes 的 OpenTelemetry Operator。
要查看完整的配置选项,请参阅配置和设置。
OpenTelemetry .NET 自动检测提供的自动日志与跟踪关联目前仅适用于使用 Microsoft.Extensions.Logging 的 .NET 应用程序。有关更多详细信息,请参阅#2310。
OpenTelemetry .NET SDK 会自动关联日志与跟踪数据。当日志在活动跟踪的上下文中发出时,跟踪上下文 字段 TraceId、SpanId、TraceState 会自动填充。
以下是示例控制台应用程序生成的日志:
"logRecords": [
{
"timeUnixNano": "1679392614538226700",
"severityNumber": 9,
"severityText": "Information",
"body": {
"stringValue": "Success! Today is: {Date:MMMM dd, yyyy}"
},
"flags": 1,
"traceId": "21df288eada1ce4ace6c40f39a6d7ce1",
"spanId": "a80119e5a05fed5a"
}
]
有关更多信息,请参阅:
OpenTelemetry .NET 自动检测支持各种库。完整列表请参阅检测。
要直接在标准输出中查看应用程序的遥测数据,请在启动应用程序之前将 console 添加到以下环境变量的值中:
OTEL_TRACES_EXPORTEROTEL_METRICS_EXPORTEROTEL_LOGS_EXPORTER有关一般的故障排除步骤和特定问题的解决方案,请参阅故障排除。
配置好应用程序或服务的自动检测后,您可能希望发送自定义跟踪和指标或添加手动检测以收集自定义遥测数据。
在 Linux 和 macOS 上,安装步骤仅影响当前 shell 会话,因此无需显式卸载。
在 Windows 上,请以管理员身份使用 PowerShell 模块。
需要 Windows PowerShell Desktop (v5.1)。目前不支持其他版本,包括 PowerShell Core (v6.0+)。
# PowerShell 5.1 is required
#Requires -PSEdition Desktop
# Import the previously installed module
Import-Module "OpenTelemetry.DotNet.Auto.psm1"
# If IIS was previously registered, unregister it
Unregister-OpenTelemetryForIIS
# If Windows services were previously registered, unregister them
Unregister-OpenTelemetryForWindowsService -WindowsServiceName "WindowsServiceName"
# Finally, uninstall OpenTelemetry instrumentation
Uninstall-OpenTelemetryCore