EventCounter の概要

EventCounter は、カウンターまたは統計情報を発行および使用するための .NET/.NET Core メカニズムです。 EventCounter は、Windows、Linux、および macOS のすべての OS プラットフォームでサポートされています。 これは、Windows システムでのみサポートされている PerformanceCounter のクロスプラットフォームの同等のものと考えることができます。

ユーザーは必要に応じてカスタムの EventCounters を発行できますが、.NET によってこれらのカウンターのセットが既定で発行されます。 このドキュメントでは、Azure Application Insights での (システム定義またはユーザー定義の) EventCounters の収集および表示に必要な手順について説明します。

Note

以下のドキュメントは、Application Insights クラシック API に関するものです。 Application Insights の長期的な計画は、OpenTelemetry を使用してデータを収集することです。 詳しくは、「.NET、Node.js、Python、Java アプリケーション用の Azure Monitor OpenTelemetry を有効にする」をご覧ください。

Application Insights を使用した EventCounter の収集

Application Insights では、その EventCounterCollectionModule (新しくリリースされた NuGet パッケージである Microsoft.ApplicationInsights.EventCounterCollector の一部) を使用した EventCounters の収集をサポートしています。 EventCounterCollectionModule は、AspNetCore または WorkerService のいずれかを使用すると、自動的に有効になります。 EventCounterCollectionModule は、60 秒の収集頻度 (構成不可) でカウンターを収集します。 EventCounter を収集するために特別なアクセス許可は必要ありません。 ASP.NET Core アプリケーションの場合は、Microsoft.ApplicationInsights.AspNetCore パッケージも追加することをお勧めします。

dotnet add package Microsoft.ApplicationInsights.EventCounterCollector
dotnet add package Microsoft.ApplicationInsights.AspNetCore

収集される既定のカウンター

AspNetCore SDK または WorkerService SDK の 2.15.0 バージョン以降では、カウンターの収集は既定で行われません。 モジュール自体が有効になっているので、ユーザーは、目的のカウンターを追加して収集することができます。

.NET Runtime で発行されている既知のカウンターの一覧を取得するには、利用できるカウンターのドキュメントを参照してください。

収集されるカウンターのカスタマイズ

次の例は、カウンターを追加または削除する方法を示しています。 このカスタマイズは、AddApplicationInsightsTelemetry() または AddApplicationInsightsWorkerService() のいずれかを使用して Application Insights のテレメトリの収集を有効にした後に、アプリケーション サービスの構成の一環として行います。 ASP.NET Core アプリケーションのコード例を次に示します。 その他の種類のアプリケーションについては、こちらのドキュメントを参照してください。

using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector;
using Microsoft.Extensions.DependencyInjection;

builder.Services.ConfigureTelemetryModule<EventCounterCollectionModule>(
        (module, o) =>
        {
            // Removes all default counters, if any.
            module.Counters.Clear();

            // Adds a user defined counter "MyCounter" from EventSource named "MyEventSource"
            module.Counters.Add(
                new EventCounterCollectionRequest("MyEventSource", "MyCounter"));

            // Adds the system counter "gen-0-size" from "System.Runtime"
            module.Counters.Add(
                new EventCounterCollectionRequest("System.Runtime", "gen-0-size"));
        }
    );

EventCounter コレクション モジュールの無効化

EventCounterCollectionModule を無効にするには、ApplicationInsightsServiceOptions を使用します。

次の例では、ASP.NET Core SDK を使用します。

using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.DependencyInjection;

var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;
builder.Services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions);

WorkerService SDK にも同様の方法を使用できますが、次の例に示すように、名前空間を変更する必要があります。

using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.DependencyInjection;

var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;
builder.Services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions);

メトリック エクスプローラーのイベント カウンター

メトリック エクスプローラーで EventCounter メトリックを表示するには、Application Insights リソースを選択し、メトリック名前空間として [ログベースのメトリック] を選択します。 これにより、EventCounter メトリックがカスタム カテゴリの下に表示されます。

Event counters reported in Application Insights Metric Explorer

Analytics のイベント カウンター

また、Analytics 内で customMetrics テーブルのイベント カウンター レポートを検索して表示することもできます。

たとえば、次のクエリを実行して、どのカウンターが収集され、クエリに使用できるかを確認します。

customMetrics | summarize avg(value) by name

Event counters reported in Application Insights Analytics

最近の期間おける特定のカウンター (例: ThreadPool Completed Work Item Count) のグラフを取得するには、次のクエリを実行します。

customMetrics 
| where name contains "System.Runtime|ThreadPool Completed Work Item Count"
| where timestamp >= ago(1h)
| summarize  avg(value) by cloud_RoleInstance, bin(timestamp, 1m)
| render timechart

Chat of a single counter in Application Insights

他のテレメトリと同様に、customMetrics にも、アプリを実行しているホスト サーバー インスタンスの ID を示す列 cloud_RoleInstance があります。 上記のクエリは、インスタンスごとのカウンター値を示しており、さまざまなサーバー インスタンスのパフォーマンスを比較するために使用できます。

警告

他のメトリックと同様に、指定した制限をイベント カウンターが超えた場合に警告するアラートを設定できます。 [アラート] ウィンドウを開き、[アラートの追加] を選択します。

よく寄せられる質問

Live Metrics で EventCounter を表示できますか。

現時点では、ライブ メトリックで EventCounter は表示されません。 テレメトリを確認するには、メトリック エクスプローラーまたは Analytics を使用してください。

Azure Web アプリ ポータルから Application Insights を有効にしました。 EventCounter を表示できないのはなぜですか?

この機能は、ASP.NET Core 向けの Application Insights 拡張機能 ではまだサポートされていません。

次のステップ