EventCounters 是从 .NET 库公开指标的标准方法吗?

Are EventCounters the standard way to expose metrics from .NET libraries?

自 .NET Core 3 起,.NET SDK 通过 EventCounters. It is also possible to implement your own EventCounter. So let's assume I'm developing a memcache client in .NET, would it make sense to expose get/set metrics through EventCounters? Also, I noticed that none of the Well-known EventCounters 使用动态指标名称公开指标,例如 memcache-{myMemcacheClusterName}-gets。不推荐吗?

So let's assume I'm developing a memcache client in .NET, would it make sense to expose get/set metrics through EventCounters?

当然,您当然可以选择 App Metrics 之类的东西,但 EventCounters 的优点在于它是 .Net Framework 的一部分,因此不需要外部依赖项。写库时有吸引力的东西。

Also, I noticed that none of the Well-known EventCounters use dynamic metric names such as memcache-{myMemcacheClusterName}-gets. Is it not recommended?

动态计数器很难发现和记录(有哪些可能的值)。不过,它曾经是包含附加信息的唯一方法。也就是说,直到引入元数据。现在,您可以使用 AddMetadata():

向计数器添加其他信息
EventCounter someCounter = new EventCounter(nameof(someCounter), this);
someCounter.AddMetadata("Environment", "Production");
someCounter.AddMetadata("SomeKey", "SomeValue");

随着 .NET 6 的发布,System.Diagnostics.Metrics, an official .NET implementation of the OpenTelemetry Metrics API 被包含在内。

在此对话中 dotnet/designs/pull/211 我们可以看到 Metrics 现在优于 EventCounters。

We'll recommend using Metrics moving forward but we are not deprecating the Event counters in this release at least.

注意 System.Diagnostics.Metrics is available for .NET < 6 through the Nuget System.Diagnostics.DiagnosticSource >= 6. Here is an example: verdie-g/modern-caching/src/ModernCaching/Instrumentation/OpenTelemetryCacheMetrics.cs.