In .NET, metric instruments are accessed via System.Diagnostics.Metrics.Meter. They are categorized into two main types based on how they are triggered:
Synchronous Instruments
These are called directly in your code when a specific event occurs. They are ideal for high-frequency events like request processing.
- Counter: Monotonically increasing (only up). Use for request counts, errors, or bytes sent.
- UpDownCounter: Can increase or decrease. Use for queue sizes or active connections.
- Histogram: Captures distributions (e.g., latency percentiles). Use for request durations or response sizes.
- Gauge (.NET 9+): Records an instantaneous snapshot. Use for non-cumulative measurements like temperature.
Asynchronous/Observable Instruments
These are called by the OpenTelemetry SDK at a defined collection interval via a callback. They are ideal for polling system state.
- ObservableCounter: Reports a monotonically increasing value (e.g., total CPU time).
- ObservableGauge: Reports an instantaneous value (e.g., current memory usage).
- ObservableUpDownCounter: Reports a value that can go up or down (e.g., active tasks by priority).
Is the value a cumulative total that only goes up?
→ YES: Can you increment it on every event?
→ YES: Counter (synchronous)
→ NO (polled periodically): ObservableCounter (async)
→ NO: Is it a distribution/percentile you need?
→ YES: Histogram
→ NO: Can the value go both up AND down?
→ YES: Can you update it on every event?
→ YES: UpDownCounter (synchronous)
→ NO (polled periodically): ObservableUpDownCounter (async)
→ NO (instantaneous snapshot): Can you record it on every event?
→ YES: Gauge (synchronous, .NET 9+)
→ NO (polled periodically): ObservableGauge (async)