Skip to content

Commit acd1466

Browse files
author
Chris Sinjakli
authored
Merge pull request #177 from lawrencejones/lawrence-add-histogram-helpers
Histogram bucket helpers
2 parents 2f91bbd + 844e451 commit acd1466

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
@@ -158,6 +158,11 @@ histogram.get(labels: { service: 'users' })
158158
# => { 0.005 => 3, 0.01 => 15, 0.025 => 18, ..., 2.5 => 42, 5 => 42, 10 = >42 }
159159
```
160160

161+
Histograms provide default buckets of `[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]`
162+
163+
You can specify your own buckets, either explicitly, or using the `Histogram.linear_buckets`
164+
or `Histogram.exponential_buckets` methods to define regularly spaced buckets.
165+
161166
### Summary
162167

163168
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)