はじめに
このページでは、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": "http://localhost:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
以下のコマンドでアプリケーションをビルドして実行し、ウェブブラウザーで http://localhost:8080/rolldice を開いて動作を確認してください。
dotnet build
dotnet run
計装
次に、OpenTelemetry .NET 自動計装を使用して、起動時にアプリケーションを計装します。 .NET 自動計装を設定する方法はいくつかありますが、以下の手順では Unix シェルまたは PowerShell スクリプトを使用します。
Note: 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.shImport-Module $download_path Install-OpenTelemetryCoreコンソールエクスポーターを指定する変数を設定してエクスポートし、シェルやターミナル環境に適した表記法を使用して、その他の必要な環境変数を設定するスクリプトを実行します。 ここでは bash 系シェルと 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 rundotnet 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
次のステップ
詳細については、以下を参照してください。
- エクスポーター、サンプラー、リソースなどを設定するには、設定とオプションを参照してください
- 利用可能な計装のリストを確認してください
- 自動計装と手動計装を組み合わせたい場合は、カスタムトレースとメトリクスの作成方法を確認してください
- 問題が発生した場合は、トラブルシューティングガイドを確認してください
フィードバック
このページは役に立ちましたか?
Thank you. Your feedback is appreciated!
Please let us know how we can improve this page. Your feedback is appreciated!