Since .NET Core 3, the .NET SDK exposes metrics through the 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 use dynamic metric names such as memcache-{myMemcacheClusterName}-gets
. Is it not recommended?
2
Answers
With the release of .NET 6, System.Diagnostics.Metrics, an official .NET implementation of the OpenTelemetry Metrics API is included.
In this conversation dotnet/designs/pull/211 we can see that Metrics is now preferred over EventCounters.
Note that 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.
Sure, of course you could go for something like App Metrics but the nice thing about EventCounters is that it is part of the .Net Framework so no external dependencies are required. Something that is attractive when writing a library.
Dynamic counters are hard to discover and to document (what possible values are there). It used to be the only way to include additional information though. That is, until the introduction of metadata. Nowadays you can add additional information to the counters using AddMetadata():