From d87274310714f00e073d416a5e39f324b43c0ae1 Mon Sep 17 00:00:00 2001 From: Lawrence Jones Date: Tue, 14 Jan 2020 09:49:07 +0000 Subject: [PATCH] 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 --- lib/prometheus/client.rb | 8 ++++++++ spec/prometheus/client_spec.rb | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/prometheus/client.rb b/lib/prometheus/client.rb index fe09d9ba..c1ed619d 100644 --- a/lib/prometheus/client.rb +++ b/lib/prometheus/client.rb @@ -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 diff --git a/spec/prometheus/client_spec.rb b/spec/prometheus/client_spec.rb index baa52d57..3b46eb34 100644 --- a/spec/prometheus/client_spec.rb +++ b/spec/prometheus/client_spec.rb @@ -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