快速入门
本页面将向您展示如何在 .NET 中开始使用 OpenTelemetry。
您将学习如何自动对一个简单的 .NET 应用程序进行仪表化,以便向控制台发出 跟踪、指标和 日志。
准备工作
请确保您的本地环境已安装以下内容:
- .NET SDK 6+
示例应用程序
下面的示例使用基本的 Minimal API with ASP.NET Core 应用程序。如果您不使用 ASP.NET Core,那没问题 —— 您仍然可以使用 OpenTelemetry .NET 自动仪表化。
有关更详尽的示例,请参阅 示例。
创建并启动一个 HTTP 服务器
首先,在一个名为 dotnet-simple
的新目录中设置环境,然后执行以下命令:
dotnet new web
接着,在同一目录下,将 Program.cs
文件的内容替换为以下代码:
using System.Globalization;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var logger = app.Logger;
int RollDice()
{
return Random.Shared.Next(1, 7);
}
string HandleRollDice(string? player)
{
var result = RollDice();
if (string.IsNullOrEmpty(player))
{
logger.LogInformation("Anonymous player is rolling the dice: {result}", result);
}
else
{
logger.LogInformation("{player} is rolling the dice: {result}", player, result);
}
return result.ToString(CultureInfo.InvariantCulture);
}
app.MapGet("/rolldice/{player?}", HandleRollDice);
app.Run();
在 Properties
子目录中,将 launchSettings.json
文件的内容替换为以下内容:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
使用以下命令构建并运行该应用程序,然后在您的 Web 浏览器中打开 http://localhost:8080/rolldice 来确保它正常工作:
dotnet build
dotnet run
仪表化
接下来,您将使用 OpenTelemetry .NET 自动仪表化 来在应用程序启动时仪表化应用程序。虽然您可以通过多种方式来配置 .NET 自动仪表化,但下面的步骤使用了 Unix Shell 或 PowerShell 脚本。
注意:PowerShell 命令需要管理员权限。
-
从
opentelemetry-dotnet-instrumentation
存储库的 Releases 下载安装脚本:curl -L -O https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh
$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
-
执行以下脚本来下载适用于您的开发环境的自动仪表化工具:
./otel-dotnet-auto-install.sh
Import-Module $download_path Install-OpenTelemetryCore
-
设置并导出变量以指定 控制台导出器,然后使用适合您的 Shell/Terminal 环境的表示方法来执行脚本配置其他必要的环境变量。下面我们演示的是适合类似于 Bash 的 Shell 和 PowerShell 的表示方法:
export OTEL_TRACES_EXPORTER=none \ OTEL_METRICS_EXPORTER=none \ OTEL_LOGS_EXPORTER=none \ OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED=true \ OTEL_DOTNET_AUTO_METRICS_CONSOLE_EXPORTER_ENABLED=true \ OTEL_DOTNET_AUTO_LOGS_CONSOLE_EXPORTER_ENABLED=true OTEL_SERVICE_NAME=RollDiceService . $HOME/.otel-dotnet-auto/instrument.sh
$env:OTEL_TRACES_EXPORTER="none" $env:OTEL_METRICS_EXPORTER="none" $env:OTEL_LOGS_EXPORTER="none" $env:OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED="true" $env:OTEL_DOTNET_AUTO_METRICS_CONSOLE_EXPORTER_ENABLED="true" $env:OTEL_DOTNET_AUTO_LOGS_CONSOLE_EXPORTER_ENABLED="true" Register-OpenTelemetryForCurrentSession -OTelServiceName "RollDiceService"
-
再次运行您的应用程序:
dotnet run
请注意
dotnet run
的输出。 -
在 另一个 终端中,使用
curl
发送一个请求:curl localhost:8080/rolldice
-
约过 30 秒后,停止服务器进程。
此时,您应该可以看到服务器和客户端的跟踪和日志输出,内容如下(为了便于阅读,输出已换行):
跟踪和日志
LogRecord.Timestamp: 2023-08-14T06:44:53.9279186Z
LogRecord.TraceId: 3961d22b5f90bf7662ad4933318743fe
LogRecord.SpanId: 93d5fcea422ff0ac
LogRecord.TraceFlags: Recorded
LogRecord.CategoryName: simple-dotnet
LogRecord.LogLevel: Information
LogRecord.StateValues (Key:Value):
result: 1
OriginalFormat (a.k.a Body): Anonymous player is rolling the dice: {result}
Resource associated with LogRecord:
service.name: simple-dotnet
telemetry.auto.version: 0.7.0
telemetry.sdk.name: opentelemetry
telemetry.sdk.language: dotnet
telemetry.sdk.version: 1.4.0.802
info: simple-dotnet[0]
Anonymous player is rolling the dice: 1
Activity.TraceId: 3961d22b5f90bf7662ad4933318743fe
Activity.SpanId: 93d5fcea422ff0ac
Activity.TraceFlags: Recorded
Activity.ActivitySourceName: OpenTelemetry.Instrumentation.AspNetCore
Activity.DisplayName: /rolldice
Activity.Kind: Server
Activity.StartTime: 2023-08-14T06:44:53.9278162Z
Activity.Duration: 00:00:00.0049754
Activity.Tags:
net.host.name: localhost
net.host.port: 8080
http.method: GET
http.scheme: http
http.target: /rolldice
http.url: http://localhost:8080/rolldice
http.flavor: 1.1
http.user_agent: curl/8.0.1
http.status_code: 200
Resource associated with Activity:
service.name: simple-dotnet
telemetry.auto.version: 0.7.0
telemetry.sdk.name: opentelemetry
telemetry.sdk.language: dotnet
telemetry.sdk.version: 1.4.0.802
此外,当停止服务器时,您应该可以看到收集到的所有指标的输出(下面显示了部分样例):
指标
Export process.runtime.dotnet.gc.collections.count, Number of garbage collections that have occurred since process start., Meter: OpenTelemetry.Instrumentation.Runtime/1.1.0.2
(2023-08-14T06:12:05.8500776Z, 2023-08-14T06:12:23.7750288Z] generation: gen2 LongSum
Value: 2
(2023-08-14T06:12:05.8500776Z, 2023-08-14T06:12:23.7750288Z] generation: gen1 LongSum
Value: 2
(2023-08-14T06:12:05.8500776Z, 2023-08-14T06:12:23.7750288Z] generation: gen0 LongSum
Value: 6
...
Export http.client.duration, Measures the duration of outbound HTTP requests., Unit: ms, Meter: OpenTelemetry.Instrumentation.Http/1.0.0.0
(2023-08-14T06:12:06.2661140Z, 2023-08-14T06:12:23.7750388Z] http.flavor: 1.1 http.method: POST http.scheme: https http.status_code: 200 net.peer.name: dc.services.visualstudio.com Histogram
Value: Sum: 1330.4766000000002 Count: 5 Min: 50.0333 Max: 465.7936
(-Infinity,0]:0
(0,5]:0
(5,10]:0
(10,25]:0
(25,50]:0
(50,75]:2
(75,100]:0
(100,250]:0
(250,500]:3
(500,750]:0
(750,1000]:0
(1000,2500]:0
(2500,5000]:0
(5000,7500]:0
(7500,10000]:0
(10000,+Infinity]:0
接下来做什么?
更多内容:
- 使用其他导出器为遥测数据运行此示例。
- 尝试在您自己的应用程序上进行自动仪表化。
- 了解手动仪表化并尝试更多的 示例。
- 查看 OpenTelemetry 演示,其中包括基于 .NET 的 Cart Service。