Member-only story
Custom Metrics in .NET Using the New IMeterFactory
.NET 8 introduced a powerful API in system diagnostics, making it seamless for developers to add custom application metrics adhering to the open telemetry standard. In this post, we’ll explore this new feature, demonstrating how it can be leveraged to publish metrics from a minimal API. Additionally, we’ll cover various approaches to test metrics and export them to Application Insights for creating insightful dashboards.
IMeterFactory was added to the system diagnostics namespace to help developers easily publish metrics from their application. Metrics can be used to capture valuable insights about your application such as:
- Business metrics like number of order, number of products sold, etc…
- Performance metrics such as API request duration
- Low level system metrics such as number of times a cache was refreshed, number of times a database connection is established, etc…
The IMeterFactory supports various instruments to cater to different situations. In .NET 8, these instruments include:
- Counter
- UpDownCounter