Skip to main content
Observable instruments (also called asynchronous instruments) report measurements via callbacks that are invoked during metric collection. They’re ideal for values that are expensive to compute, read from external sources, or already being tracked elsewhere.

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
Common examples:
  • 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:
Supports: u64, f64

ObservableUpDownCounter

For values that can increase or decrease:
Supports: i64, f64

ObservableGauge

For current/instantaneous values:
Supports: u64, i64, f64

AsyncInstrument Trait

All observable instruments implement the AsyncInstrument trait:
The observe method is called within your callback to report measurements.

Callback Type

Callbacks are defined as:
Your callback receives an observer that implements AsyncInstrument<T>, which you use to report measurements.

Creating Observable Instruments

Observable instruments are created using the builder pattern with with_callback:

ObservableCounter

ObservableUpDownCounter

ObservableGauge

Complete Example

Example from Source

From the metrics-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

Keep observable instruments in scope! The callback will not be invoked if the instrument is dropped:

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