When to Use Observable Instruments
Use observable instruments when:- Reading from system APIs or sensors
- Values are expensive to compute
- Measurements should happen on a schedule (not inline)
- Multiple instruments need the same source data
- You don’t control when collection happens
- CPU time from OS APIs
- Memory usage from system calls
- Process statistics
- Sensor readings
- External resource metrics
Observable instruments use callbacks that run during metric collection. The SDK determines when to call your callback, typically during export intervals.
Available Observable Instruments
ObservableCounter
For monotonically increasing values:u64, f64
ObservableUpDownCounter
For values that can increase or decrease:i64, f64
ObservableGauge
For current/instantaneous values:u64, i64, f64
AsyncInstrument Trait
All observable instruments implement theAsyncInstrument trait:
observe method is called within your callback to report measurements.
Callback Type
Callbacks are defined as:AsyncInstrument<T>, which you use to report measurements.
Creating Observable Instruments
Observable instruments are created using the builder pattern withwith_callback:
ObservableCounter
ObservableUpDownCounter
ObservableGauge
Complete Example
Example from Source
From themetrics-basic example:
Multiple Observations in One Callback
A single callback can report multiple measurements with different attributes:Multiple Callbacks
You can register multiple callbacks for the same instrument:Capturing State in Callbacks
Use closures to capture state:Synchronous vs. Observable
Choose the right approach for your use case:Instrument Type Comparison
There is no ObservableHistogram. Histograms are only available as synchronous instruments.
Callback Best Practices
Keep Callbacks Fast
Callbacks should complete quickly:Avoid Blocking I/O
Don’t perform blocking operations in callbacks:Handle Errors Gracefully
Instrument Lifecycle
Common Patterns
System Metrics
Process Statistics
Next Steps
Counter
Learn about synchronous Counter instruments
Gauge
Learn about synchronous Gauge instruments
Meters
Create and configure Meters
Views
Customize metric aggregation