EventCounters 是用于轻型、跨平台和准实时性能指标收集的 .NET API。
使用 EventCounters 检测网络组件以发布基本诊断信息。 它们包括如下信息:
System.Net.Http
>requests-started
System.Net.Http
>requests-failed
System.Net.Http
>http11-connections-current-total
System.Net.Security
>all-tls-sessions-open
System.Net.Sockets
>outgoing-connections-established
System.Net.NameResolution
>dns-lookups-duration
小窍门
有关完整列表,请参阅 已知的计数器。
小窍门
在面向 .NET 8+ 的项目上,请考虑使用更新且功能更丰富的 网络指标 ,而不是 EventCounters。
提供者
网络信息分配给以下提供商:
-
System.Net.Http
(HttpClient
和SocketsHttpHandler
) -
System.Net.NameResolution
(Dns
) -
System.Net.Security
(SslStream
) System.Net.Sockets
Microsoft.AspNetCore.Hosting
Microsoft-AspNetCore-Server-Kestrel
启用遥测会导致一些性能损耗,因此请确保只订阅您真正感兴趣的提供商。
从进程外部监视事件计数器
dotnet-counters
dotnet-counters
是一种跨平台性能监视工具,用于临时运行状况监视和一级性能调查。
dotnet tool install --global dotnet-counters
dotnet-counters monitor --counters System.Net.Http,System.Net.Security --process-id 1234
该命令会持续使用最新数字刷新控制台。
[System.Net.Http]
Current Http 1.1 Connections 3
Current Http 2.0 Connections 1
Current Http 3.0 Connections 0
Current Requests 4
HTTP 1.1 Requests Queue Duration (ms) 0
HTTP 2.0 Requests Queue Duration (ms) 0
HTTP 3.0 Requests Queue Duration (ms) 0
Requests Failed 0
Requests Failed Rate (Count / 1 sec) 0
Requests Started 470
Requests Started Rate (Count / 1 sec) 18
有关所有可用的命令和参数,请参阅 dotnet-counter 文档。
Application Insights
默认情况下,Application Insights 不会收集事件计数器。 有关自定义你感兴趣的计数器集的信息,请参阅 AppInsights EventCounters 文档。
例如:
services.ConfigureTelemetryModule<EventCounterCollectionModule>((module, options) =>
{
module.Counters.Add(new EventCounterCollectionRequest("System.Net.Http", "current-requests"));
module.Counters.Add(new EventCounterCollectionRequest("System.Net.Http", "requests-failed"));
module.Counters.Add(new EventCounterCollectionRequest("System.Net.Http", "http11-connections-current-total"));
module.Counters.Add(new EventCounterCollectionRequest("System.Net.Security", "all-tls-sessions-open"));
});
有关如何订阅多个运行时和 ASP.NET 事件计数器的示例,请参阅 RuntimeEventCounters 示例。 只需为每个条目添加一个 EventCounterCollectionRequest
。
foreach (var (eventSource, counters) in RuntimeEventCounters.EventCounters)
{
foreach (string counter in counters)
{
module.Counters.Add(new EventCounterCollectionRequest(eventSource, counter));
}
}
使用进程内的事件计数器
通过此 Yarp.Telemetry.Consumption
库,可以轻松地从进程内使用事件计数器。
虽然包当前作为 YARP 项目的一部分进行维护,但它可在任何 .NET 应用程序中使用。
若要使用它,请实现 IMetricsConsumer<TMetrics>
接口:
public sealed class MyMetricsConsumer : IMetricsConsumer<SocketsMetrics>
{
public void OnMetrics(SocketsMetrics previous, SocketsMetrics current)
{
var elapsedTime = (current.Timestamp - previous.Timestamp).TotalMilliseconds;
Console.WriteLine($"Received {current.BytesReceived - previous.BytesReceived} bytes in the last {elapsedTime:N2} ms");
}
}
然后将实现注册到 DI 容器中。
services.AddSingleton<IMetricsConsumer<SocketsMetrics>, MyMetricsConsumer>();
services.AddTelemetryListeners();
该库提供以下强类型指标类型:
需要更多的遥测数据?
如果对可以通过事件或指标公开的其他有用信息提供建议,请创建 dotnet/runtime 问题。
如果使用的是 Yarp.Telemetry.Consumption
库并有任何建议,请创建 microsoft/反向代理问题。