Skip to main content

Overview

Metrics allow you to measure and track numerical data about your application’s performance and behavior. This guide demonstrates all the core metric instruments available in OpenTelemetry Rust.

Dependencies

Add these dependencies to your Cargo.toml:
Cargo.toml

Complete Example

1

Initialize the Meter Provider

Set up the meter provider with a stdout exporter and resource attributes.
2

Create and Use Counters

Counters are used for monotonically increasing values.
3

Create Observable Counters

Observable counters allow you to register a callback that reports measurements asynchronously.
4

Use UpDownCounters

UpDownCounters can increase or decrease, useful for tracking values like queue sizes.
5

Create Histograms

Histograms measure the distribution of values, perfect for latencies and sizes.
There is no ObservableHistogram instrument in OpenTelemetry.
6

Use Gauges

Gauges represent a value that can arbitrarily go up and down.

Metric Instruments Summary

Metrics are exported by default every 60 seconds when using the stdout exporter. Shutting down the MeterProvider instantly flushes the metrics instead of waiting for the interval.

Advanced Metrics

For advanced use cases like custom aggregations, views, and exponential histograms, check out the metrics-advanced example in the source repository which covers:
  • Renaming metrics and changing units with Views
  • Controlling cardinality limits
  • Using exponential histograms for unpredictable value ranges
  • Delta vs. Cumulative temporality
Always call shutdown() on the meter provider before your application exits to ensure all metrics are properly flushed to the exporter.