Why buckets matter
Buckets determine how your histogram data is organized. Each bucket represents an upper bound, and Prometheus counts how many observations fall into each range. Choosing appropriate buckets is essential for:- Calculating accurate percentiles (p50, p95, p99)
- Understanding the distribution of your measurements
- Avoiding data loss from values outside your bucket range
Exponential buckets
Use exponential buckets when your values span multiple orders of magnitude. This is common for metrics like request duration, where most requests are fast but some can be very slow.Function signature
Parameters
- start: The upper bound of the first bucket
- factor: The multiplier for each subsequent bucket
- count: The number of buckets to generate
How it works
Each bucket’s upper bound is calculated by multiplying the previous bucket by the factor:Examples
- Request duration
- Response size
- Basic example
For HTTP request duration in seconds:This covers request durations from 5ms to 320ms with exponentially growing buckets.
Exponential buckets are ideal for request duration, query time, and other latency metrics where values can vary by orders of magnitude.
Linear buckets
Use linear buckets when your values fall within a narrow, predictable range. This is common for metrics like queue length or percentage values.Function signature
Parameters
- start: The upper bound of the first bucket
- step: The amount to add for each subsequent bucket
- count: The number of buckets to generate
How it works
Each bucket’s upper bound is calculated by adding the step to the previous bucket:Examples
- Queue length
- Percentage
- Basic example
For tracking queue length:This tracks queue lengths from 0-10, 10-20, 20-30, etc.
Linear buckets work well for queue sizes, cache hit rates, and any metric with a predictable range.
Using buckets with histograms
Once you’ve generated buckets, pass them tocreate_histogram:
Manual buckets
You can also define buckets manually as a list of floats:Choosing between exponential and linear
Use this guide to choose the right bucket generation function:Check the spread
If the maximum is more than 10x the minimum, use exponential buckets. Otherwise, consider linear buckets.
Consider the distribution
If most values cluster at one end of the range, exponential buckets provide better granularity. If values are evenly distributed, use linear buckets.
Bucket recommendations by metric type
| Metric Type | Recommended | Example |
|---|---|---|
| Request duration | Exponential | exponential(start: 0.005, factor: 2, count: 8) |
| Database query time | Exponential | exponential(start: 0.001, factor: 4, count: 6) |
| Response size | Exponential | exponential(start: 100.0, factor: 10, count: 5) |
| Queue length | Linear | linear(start: 10.0, step: 10.0, count: 10) |
| Cache hit rate | Linear | linear(start: 10.0, step: 10.0, count: 10) |
| Connection pool usage | Linear | linear(start: 5.0, step: 5.0, count: 10) |
Error handling
Both functions returnResult(Buckets, String). Common errors include:
The error messages come from the underlying prometheus.erl library and provide details about what went wrong.
Best practices
- Start with exponential buckets for duration metrics
- Use 5-10 buckets for most histograms (more buckets = more memory)
- Ensure your highest bucket is above your expected p99 value
- Test bucket boundaries with real data and adjust as needed
- Document your bucket choices for future reference