Instrument Categories
Instruments fall into two main categories:Synchronous Instruments
Synchronous instruments record measurements inline with your application code. They implement theSyncInstrument trait:
- Counter: Monotonically increasing values
- UpDownCounter: Values that can increase or decrease
- Histogram: Distribution of values
- Gauge: Independent point-in-time values
Asynchronous Instruments
Asynchronous instruments use callbacks that are invoked during metric collection. They implement theAsyncInstrument trait:
- ObservableCounter: Observes monotonically increasing values
- ObservableUpDownCounter: Observes values that can increase or decrease
- ObservableGauge: Observes current values
Instrument Types Overview
Counter
Records monotonically increasing values like request counts or bytes sent
UpDownCounter
Records values that go up and down like active connections or queue depth
Histogram
Records distributions of values like request duration or response size
Gauge
Records independent measurements like CPU usage or temperature
Choosing the Right Instrument
Use this decision tree to select the appropriate instrument:Counter vs. UpDownCounter
Use Counter when:- Values only increase (e.g., requests served, errors, bytes sent)
- You’re counting discrete events
- Resets should only happen on restart
- Values can increase or decrease (e.g., active connections, items in queue)
- You’re tracking a resource that changes bidirectionally
- Current total is meaningful
Histogram vs. Gauge
Use Histogram when:- You need to understand the distribution (percentiles, min, max)
- Values represent durations, sizes, or other measurements
- Examples: latency, request size, temperature readings
- You only care about the current value
- The last value is sufficient
- Examples: current CPU usage, current memory, cache size
Synchronous vs. Asynchronous
Use Synchronous when:- Recording measurements in request handlers
- Events happen as part of application logic
- You control when measurements occur
- Reading from system APIs or sensors
- Values are expensive to compute
- Measurements should happen on a schedule
- Multiple instruments need the same source data
Instrument Builders
All instruments are created using a builder pattern:InstrumentBuilder (Synchronous)
HistogramBuilder
Histograms have additional configuration for bucket boundaries:Default boundaries:
[0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 7500.0, 10000.0]AsyncInstrumentBuilder (Observable)
Asynchronous instruments require callbacks:Instrument Naming
Follow these conventions when naming instruments:- Use lowercase letters
- Use underscores to separate words
- Choose descriptive, meaningful names
- Include the unit in the name when appropriate (e.g.,
duration_seconds,size_bytes) - Use plural for counters (e.g.,
requests_total,errors_count)
Units
Specify units using standard abbreviations:- Time:
ms,s,min,h,d - Bytes:
By,KiBy,MiBy - Percentages:
% - Counts:
{items},{requests},{errors}
Cloning Instruments
All synchronous instruments can be cloned:Next Steps
Counter
Learn about Counter instruments in detail
Histogram
Understand how to use Histograms
Gauge
Record independent measurements with Gauges
Observable Instruments
Use callbacks for asynchronous measurements