入门
本页将向您展示如何开始使用 OpenTelemetry .NET 自动检测。
如果您正在寻找一种手动检测应用程序的方法,请查看本指南。
您将了解如何自动检测一个简单的 .NET 应用程序,从而能够将跟踪、指标和日志发送到控制台。
先决条件
请确保您已在本地安装以下软件
- .NET SDK 6+
示例应用程序
以下示例使用了基本的 ASP.NET Core Minimal API 应用程序。如果您不使用 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": "https://:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
使用以下命令构建并运行应用程序,然后在 Web 浏览器中打开 https://:8080/rolldice 以确保其正常工作。
dotnet build
dotnet run
仪表
接下来,您将使用OpenTelemetry .NET 自动检测在应用程序启动时对其进行检测。虽然您可以通过多种方式配置 .NET 自动检测,但下面的步骤使用了 Unix shell 或 PowerShell 脚本。
注意:PowerShell 命令需要管理员权限。
从
opentelemetry-dotnet-instrumentation存储库的发布下载安装脚本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.shImport-Module $download_path Install-OpenTelemetryCore设置并导出指定控制台导出器的变量,然后执行配置其他必要环境变量的脚本,使用适合您的 shell/终端环境的表示法——我们以 bash 类 shell 和 PowerShell 的表示法为例
export OTEL_TRACES_EXPORTER=console \ OTEL_METRICS_EXPORTER=console \ OTEL_LOGS_EXPORTER=console OTEL_SERVICE_NAME=RollDiceService . $HOME/.otel-dotnet-auto/instrument.sh$env:OTEL_TRACES_EXPORTER="console" $env:OTEL_METRICS_EXPORTER="console" $env:OTEL_LOGS_EXPORTER="console" 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: https://: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
下一步?
更多信息
- 要配置导出器、采样器、资源等,请参阅配置和设置
- 查看可用检测列表
- 如果您想结合使用自动和手动检测,请了解如何创建自定义跟踪和指标
- 如果您遇到任何问题,请查看故障排除指南