Skip to main content
A Histogram is a synchronous instrument that records a distribution of values. Histograms are ideal for understanding the statistical distribution of measurements like request duration, response sizes, or temperatures.

When to Use Histogram

Use a Histogram when:
  • You need to calculate percentiles (p50, p95, p99)
  • You want to understand the distribution of values
  • Min, max, sum, and count are all valuable
  • Values represent measurements or observations
Common examples:
  • HTTP request duration
  • Response payload size
  • Database query latency
  • Temperature readings
  • Request processing time
Histograms are more expensive than counters because they record individual measurements and calculate statistics. Use them only when you need distribution data.

API Reference

The record method adds a value to the distribution.

Creating a Histogram

Histograms support u64 and f64 data types:

Recording Measurements

Use the record method to add values to the histogram:

Configuring Bucket Boundaries

Histogram accuracy depends on bucket boundaries. You can customize them when creating the histogram:

Default Boundaries

If you don’t specify boundaries, the default is:

Boundary Requirements

Boundaries must:
  • Not contain f64::NAN, f64::INFINITY, or f64::NEG_INFINITY
  • Be in strictly increasing order
  • Not contain duplicate values
Invalid boundaries will cause the instrument to not report measurements.

Choosing Boundaries

Choose boundaries based on your expected value range:
Use more buckets for ranges where you expect most values. This improves percentile accuracy but increases memory and network usage.

Complete Example

Example from Source

From the metrics-basic example:

Exponential Histograms

For unpredictable value ranges, use exponential histograms via Views. They automatically adjust bucket widths:
See Views for more details on customizing aggregation.

Attributes and Cardinality

Like counters, histograms support attributes:
Each unique attribute combination creates a separate histogram. Keep cardinality low to avoid memory issues.

Cloning Histograms

Histograms can be cloned to share across your application:
Clone histograms instead of creating duplicates. Multiple histograms with the same name can lower SDK performance.

Histogram vs. Gauge

Best Practices

  1. Choose appropriate boundaries: Match your expected value range
  2. Use consistent units: Seconds for duration, bytes for size
  3. Keep cardinality low: Limit unique attribute combinations
  4. Clone when sharing: Don’t create duplicate histograms
  5. Use f64 for most cases: Better precision for measurements
  6. Consider exponential histograms: For unpredictable ranges

Next Steps

Counter

Record monotonically increasing values

Gauge

Record independent point-in-time values

Views

Customize histogram aggregation and boundaries

Observable Instruments

Use callbacks to report measurements