Azure Application Insights は、要求、トレース、例外、およびパフォーマンス メトリックを自動的にキャプチャするアプリケーション パフォーマンス監視 (APM) サービスです。 データ API ビルダー (DAB) と統合すると、実行時の動作の監視、問題の診断、運用環境でのパフォーマンスの最適化に役立ちます。
Warnung
Application Insights と DAB の統合には、二重インストルメンテーションが原因で Azure App Service Web アプリでホストされている場合、制限がある場合があります。 Application Insights は、DAB をコンテナー、Azure Container Apps、または Azure Kubernetes Service (AKS) でセルフホストする場合に最適です。 App Service を使用する必要がある場合は、十分にテストするか、代替の監視方法を検討してください。
[前提条件]
- 既存の DAB 構成ファイル。
- Azure Application Insights リソース。
- Application Insights 接続文字列。
- データ API ビルダー CLI。 CLI をインストールする
接続文字列を取得する
DAB を構成する前に、Azure から Application Insights 接続文字列を取得します。
Azure portal
- Azure ポータルで Application Insights のリソースを参照します。
- [ 概要 ] または [プロパティ] に移動します。
- 接続文字列をコピーします (インストルメンテーション キーではありません)。
Azure CLI
az monitor app-insights component show \
--app my-app-insights \
--resource-group my-rg \
--query connectionString -o tsv
接続文字列の形式
InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://<region>.in.applicationinsights.azure.com/;LiveEndpoint=https://<region>.livediagnostics.monitor.azure.com/
注
リージョン固有のエンドポイントとパフォーマンスを向上させるには、(インストルメンテーション キーだけでなく) 完全な接続文字列を使用します。
Application Insights の構成
構成ファイルのapplication-insightsの下にruntime.telemetryセクションを追加します。
{
"runtime": {
"telemetry": {
"application-insights": {
"enabled": true,
"connection-string": "@env('app-insights-connection-string')"
}
}
}
}
この構成では、接続文字列に環境変数が使用されます。
.env ファイルで定義します。
app-insights-connection-string="InstrumentationKey=...;IngestionEndpoint=...;LiveEndpoint=..."
Warnung
接続文字列をソース管理にコミットしないでください。 環境変数または Azure Key Vault を常に使用します。
Command-line
dab add-telemetryを使用して Application Insights を構成します。
| Option | 説明 |
|---|---|
--app-insights-enabled |
Application Insights (true または false) を有効または無効にします。 |
--app-insights-conn-string |
Application Insights の接続文字列。 |
Application Insights を有効にする
dab add-telemetry \
--app-insights-enabled true \
--app-insights-conn-string "@env('app-insights-connection-string')"
Application Insights を無効にする
dab add-telemetry \
--app-insights-enabled false
注
Application Insights の設定では、dab add-telemetryではなく、dab configureが使用されます。
DAB を実行する
構成ファイルを使用して DAB を起動します。
dab start
スタートアップ ログで確認を確認します。
Application Insights telemetry is enabled with connection string from config.
動作方法
Application Insights が有効になっている場合、DAB:
-
AddApplicationInsightsTelemetry()を使用して Application Insights SDK を登録します。 - DAB 固有のプロパティを使用してすべてのテレメトリをエンリッチするカスタム テレメトリ初期化子を登録します。
- config から接続文字列を使用して
TelemetryClientを構成します。 - ASP.NET Core ログと統合して、コンソール ログをトレースとしてキャプチャします。
データ フロー
DAB Application
↓
ILogger (ASP.NET Core)
↓
ApplicationInsightsLoggerProvider
↓
AppInsightsTelemetryInitializer (adds custom properties)
↓
TelemetryClient
↓
Application Insights (Azure)
キャプチャされる内容
| テレメトリの種類 | 情報源 | 例示 |
|---|---|---|
| Requests | ASP.NET Core ミドルウェア | REST/GraphQL 要求、応答時間、状態コード |
| 痕跡 |
ILogger DAB での呼び出し |
スタートアップ ログ、クエリ実行ログ、警告 |
| Exceptions | ハンドルされない例外 | ランタイム エラー、構成エラー、データベース エラー |
| 依存関係 | データベース呼び出し | SQL クエリ、Azure Cosmos DB 操作、期間 |
| 性能カウンター | ランタイム | CPU 使用率、メモリ使用量、要求率 |
テレメトリデータの強化
DAB は、カスタム プロパティを使用して、すべての Application Insights テレメトリを自動的に強化します。
| プロパティ | 説明 | 値の例 |
|---|---|---|
ProductName |
DAB ユーザー エージェント識別子 | dab-1.2.3 |
UserAgent |
完全な DAB ユーザー エージェント文字列 | data-api-builder/1.2.3 |
Cloud.RoleName |
DAB クラウド ロール名 | DataApiBuilder |
Component.Version |
DAB バージョン | 1.2.3 |
Session.Id |
一意のセッション識別子 | guid |
これらのプロパティは、Application Insights で DAB 固有のテレメトリをフィルター処理して関連付けるのに役立ちます。
Azure でテレメトリのクエリを実行する
トレース (ログ)
traces
| where customDimensions["ProductName"] startswith "dab-"
| order by timestamp desc
| project timestamp, message, severityLevel
LogLevel マッピング:
| LogLevel | 深刻さ | 価値 |
|---|---|---|
| トレース/デバッグ | 詳細 | 0 |
| 情報 | 情報 | 1 |
| Warnung | Warnung | 2 |
| エラー | エラー | 3 |
| 危うい | 危うい | 4 |
Requests
requests
| where customDimensions["ProductName"] startswith "dab-"
| order by timestamp desc
| project timestamp, name, duration, resultCode, success
Exceptions
exceptions
| where customDimensions["ProductName"] startswith "dab-"
| order by timestamp desc
| project timestamp, type, outerMessage, details
DAB バージョンでフィルターする
traces
| where customDimensions["Component.Version"] == "1.2.3"
| project timestamp, message, severityLevel
低速の GraphQL クエリを検索する
requests
| where name contains "/graphql"
| where duration > 1000
| project timestamp, name, duration, resultCode
| order by duration desc
要求の成功率
requests
| where customDimensions["ProductName"] startswith "dab-"
| summarize
Total = count(),
Success = countif(success == true),
Failed = countif(success == false)
| extend SuccessRate = (Success * 100.0) / Total
最も低速なデータベース操作
dependencies
| where type == "SQL" or type == "Azure Cosmos DB"
| top 10 by duration desc
| project timestamp, name, duration, target, data
ライブ メトリック
ライブ メトリックは、 < 1 秒の待機時間でリアルタイムの監視を提供します。 Application Insights が構成されると、自動的に有効になります。
ライブ メトリックにアクセスする
- Azure portal に Application Insights のリソースを開きます。
- 左側のメニューの [ライブ メトリック] に移動します。
- DAB アプリケーションを起動します。
- 数秒でリアルタイム データが表示されます。
あなたが見るもの
| メトリック | 説明 |
|---|---|
| 受信要求 | 1 秒あたりの REST/GraphQL 要求数 |
| 送信要求 | 1 秒あたりのデータベース呼び出し数 |
| 全体的な健康状態 | 成功率、1 秒あたりの失敗数 |
| メモリ/CPU | リソース消費 |
| 例外率 | 1 秒あたりの例外数 |
ヒント
開発中にライブ メトリックを使用して、API 要求とデータベース操作に関するフィードバックをすぐに確認できます。
サンプリングとデータリテンション
アダプティブ サンプリング
Application Insights SDK は、ボリュームが大きいときにテレメトリを自動的にサンプリングしてコストを削減し、レート制限内に留めます。 サンプリング レートは Application Insights UI に表示されます。
既定の動作:
- 低通信量: すべてのテレメトリが送信される (100%)
- トラフィックが多い: サンプリングによってボリュームが自動的に減少する
- 維持されている代表的なデータ
データの保持
| Plan | 既定の保持期間 | 最大リテンション |
|---|---|---|
| Free レベル | 90 日間 | 90 日間 |
| 従量課金制 | 90 日間 | 730 日 (2 年) |
リテンション期間を構成する: Application Insights → 使用状況と推定コスト → データ保有期間。
パフォーマンスに関する考慮事項
テレメトリのオーバーヘッド
Application Insights では、最小限のオーバーヘッドが追加されます。
- メモリ: トラフィックに応じて最大 10 ~ 50 MB
- CPU: <1% (通常の負荷)
- 待機時間: 要求あたりの < 1 ミリ秒 (非同期)
ベスト プラクティス
- 接続文字列には環境変数を使用します。
- 必要でない場合は、ローカル開発で無効にします。
- 運用環境のサンプリング レートを監視します。
- コストを管理するための適切なデータ保有期間を設定します。
開発中に無効にする
{
"runtime": {
"telemetry": {
"application-insights": {
"enabled": false
}
}
}
}
エクスポートと視覚化
テレメトリは、Application Insights SDK を介してエクスポートされます。 SDK はデータを定期的にバッチ処理して送信します。
注
SDK はエクスポートのタイミングを制御します。 既定の動作では、テレメトリが数秒ごとにバッチで送信されます。
Warnung
短時間でシャットダウンするエフェメラル コンテナーは、エクスポートが完了する前に終了する可能性があります。 グレースフル シャットダウン ウィンドウを構成し、強制的な終了を回避して、保留中のテレメトリデータのフラッシュを完了させます。
接続文字列とインストルメンテーション キー
接続文字列を使用する (推奨)
{
"connection-string": "InstrumentationKey=...;IngestionEndpoint=https://eastus.in.applicationinsights.azure.com/"
}
メリット:
- リージョン固有のエンドポイント (待機時間の短縮)
- 主権クラウドをサポートします
- 将来に備えて (Microsoft が推奨するアプローチ)
レガシ インストルメンテーション キー
引き続きサポートされていますが、新しい実装には接続文字列をお勧めします。
{
"connection-string": "InstrumentationKey=00000000-0000-0000-0000-000000000000"
}
注
インストルメンテーション キーのみを指定した場合、Application Insights はグローバル インジェスト エンドポイントを使用します。このエンドポイントの待機時間が長くなる可能性があります。
トラブルシューティング
エラー: "Application Insights 接続文字列を null または空にすることはできません (有効な場合)
原因: enabled は true に設定されていますが、 connection-string が見つからないか空です。
解決策: Application Insights を有効にするときに有効な接続文字列を指定するか、 enabled を false に設定します。
{
"runtime": {
"telemetry": {
"application-insights": {
"enabled": true,
"connection-string": "@env('app-insights-connection-string')"
}
}
}
}
DAB は起動しますが、テレメトリは表示されません
スタートアップ ログで次のメッセージを確認します。
dab start --LogLevel Information
成功メッセージ:
Application Insights telemetry is enabled with connection string from config.
警告メッセージ:
Logs won't be sent to Application Insights because an Application Insights connection string is not available in the runtime config.
Application Insights are disabled.
エラー メッセージ:
Telemetry client is not initialized.
環境変数を確認する
echo $app-insights-connection-string
直接接続文字列を使用してテストする
(環境変数ではなく) 直接接続文字列を一時的に使用して、文字列が有効であることを確認します。
{
"connection-string": "InstrumentationKey=...;IngestionEndpoint=..."
}
これが機能する場合、問題は環境変数の読み込みにあります。