How do EventCounters and the newer System.Diagnostics.Metrics API (Meter) let you expose custom application-specific performance metrics?
Short answer
EventCounter (older API) and Meter/Instrument (newer, OpenTelemetry-aligned API introduced in .NET 6) let application code publish custom numeric metrics (counters, gauges, histograms) beyond the built-in runtime counters -- like 'orders processed per second' or 'average payment…
Advertisement
AModel answer
EventCounter (older API) and Meter/Instrument (newer, OpenTelemetry-aligned API introduced in .NET 6) let application code publish custom numeric metrics (counters, gauges, histograms) beyond the built-in runtime counters -- like 'orders processed per second' or 'average payment processing latency' -- which tools like dotnet-counters or an OpenTelemetry-based observability platform can then collect and visualize alongside built-in system metrics.
private static readonly Meter _meter = new("MyApp.Orders");
private static readonly Counter<long> _ordersProcessed = _meter.CreateCounter<long>("orders.processed");
private static readonly Histogram<double> _processingDuration = _meter.CreateHistogram<double>("orders.duration");
public void ProcessOrder() {
var sw = Stopwatch.StartNew();
// ... processing logic ...
_ordersProcessed.Add(1);
_processingDuration.Record(sw.Elapsed.TotalMilliseconds);
}Real-world example
An e-commerce platform exposes a custom orders.processed counter and orders.duration histogram via the Meter API, feeding directly into their OpenTelemetry pipeline alongside built-in ASP.NET Core request metrics for a unified operational dashboard.
Likely follow-ups
- What's the difference between a Counter, Gauge, and Histogram instrument type?
- How does dotnet-counters automatically discover custom Meters?
Related topics