Skip to content

Commit 844e451

Browse files
lawrencejonesDaniel Magliola
authored andcommitted
Histogram bucket helpers
Most client libraries provide helpers for generating linear/exponential histogram buckets. This provides the same interface as the golang client. Signed-off-by: Lawrence Jones <lawrjone@gmail.com>
1 parent 3652cf7 commit 844e451

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ histogram.get(labels: { service: 'users' })
153153
# => { 0.005 => 3, 0.01 => 15, 0.025 => 18, ..., 2.5 => 42, 5 => 42, 10 = >42 }
154154
```
155155

156+
Histograms provide default buckets of `[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]`
157+
158+
You can specify your own buckets, either explicitly, or using the `Histogram.linear_buckets`
159+
or `Histogram.exponential_buckets` methods to define regularly spaced buckets.
160+
156161
### Summary
157162

158163
Summary, similar to histograms, is an accumulator for samples. It captures

lib/prometheus/client/histogram.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ def initialize(name,
3333
store_settings: store_settings)
3434
end
3535

36+
def self.linear_buckets(start:, width:, count:)
37+
count.times.map { |idx| start.to_f + idx * width }
38+
end
39+
40+
def self.exponential_buckets(start:, factor: 2, count:)
41+
count.times.map { |idx| start.to_f * factor ** idx }
42+
end
43+
3644
def with_labels(labels)
3745
self.class.new(name,
3846
docstring: docstring,

spec/prometheus/client/histogram_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@
3737
end
3838
end
3939

40+
describe ".linear_buckets" do
41+
it "generates buckets" do
42+
expect(described_class.linear_buckets(start: 1, width: 2, count: 5)).
43+
to eql([1.0, 3.0, 5.0, 7.0, 9.0])
44+
end
45+
end
46+
47+
describe ".exponential_buckets" do
48+
it "generates buckets" do
49+
expect(described_class.exponential_buckets(start: 1, factor: 2, count: 5)).
50+
to eql([1.0, 2.0, 4.0, 8.0, 16.0])
51+
end
52+
end
53+
4054
describe '#observe' do
4155
it 'records the given value' do
4256
expect do

0 commit comments

Comments
 (0)