Skip to content

Commit

Permalink
Histogram bucket helpers
Browse files Browse the repository at this point in the history
Most client libraries provide helpers for generating linear/exponential
histogram buckets. This provides the same interface as the golang
client.
  • Loading branch information
lawrencejones committed Jan 14, 2020
1 parent 3652cf7 commit 9fdbdaf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/prometheus/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ def self.registry
def self.config
@config ||= Config.new
end

def self.linear_buckets(start:, width:, count:)
count.times.map { |idx| start.to_f + idx * width }
end

def self.exponential_buckets(start:, factor: 2, count:)
count.times.map { |idx| start.to_f * factor ** idx }
end
end
end
16 changes: 16 additions & 0 deletions spec/prometheus/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,20 @@
expect(Prometheus::Client.registry).to eql(Prometheus::Client.registry)
end
end

describe ".linear_buckets" do
it "generates buckets" do
expect(described_class.linear_buckets(start: 1, width: 2, count: 5)).to eql(
[1.0, 3.0, 5.0, 7.0, 9.0]
)
end
end

describe ".exponential_buckets" do
it "generates buckets" do
expect(described_class.exponential_buckets(start: 1, factor: 2, count: 5)).to eql(
[1.0, 2.0, 4.0, 8.0, 16.0]
)
end
end
end

0 comments on commit 9fdbdaf

Please sign in to comment.