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
- 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
record method adds a value to the distribution.
Creating a Histogram
Histograms supportu64 and f64 data types:
Recording Measurements
Use therecord 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
Choosing Boundaries
Choose boundaries based on your expected value range:Complete Example
Example from Source
From themetrics-basic example:
Exponential Histograms
For unpredictable value ranges, use exponential histograms via Views. They automatically adjust bucket widths:Attributes and Cardinality
Like counters, histograms support attributes:Cloning Histograms
Histograms can be cloned to share across your application:Histogram vs. Gauge
Best Practices
- Choose appropriate boundaries: Match your expected value range
- Use consistent units: Seconds for duration, bytes for size
- Keep cardinality low: Limit unique attribute combinations
- Clone when sharing: Don’t create duplicate histograms
- Use f64 for most cases: Better precision for measurements
- 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