Skip to content

Commit 3f052fd

Browse files
committed
Use binary search for histogram buckets
1 parent 5514b2b commit 3f052fd

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

lib/prometheus/client/histogram.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# encoding: UTF-8
1+
# frozen_string_literal: true
22

33
require 'prometheus/client/metric'
44

@@ -37,8 +37,8 @@ def self.linear_buckets(start:, width:, count:)
3737
count.times.map { |idx| start.to_f + idx * width }
3838
end
3939

40-
def self.exponential_buckets(start:, factor: 2, count:)
41-
count.times.map { |idx| start.to_f * factor ** idx }
40+
def self.exponential_buckets(start:, count:, factor: 2)
41+
count.times.map { |idx| start.to_f * factor**idx }
4242
end
4343

4444
def with_labels(labels)
@@ -67,16 +67,16 @@ def type
6767
# https://prometheus.io/docs/practices/histograms/#count-and-sum-of-observations
6868
# for details.
6969
def observe(value, labels: {})
70-
bucket = buckets.find {|upper_limit| upper_limit >= value }
71-
bucket = "+Inf" if bucket.nil?
70+
bucket = buckets.bsearch { |upper_limit| upper_limit >= value }
71+
bucket = '+Inf' if bucket.nil?
7272

7373
base_label_set = label_set_for(labels)
7474

7575
# This is basically faster than doing `.merge`
7676
bucket_label_set = base_label_set.dup
7777
bucket_label_set[:le] = bucket.to_s
7878
sum_label_set = base_label_set.dup
79-
sum_label_set[:le] = "sum"
79+
sum_label_set[:le] = 'sum'
8080

8181
@store.synchronize do
8282
@store.increment(labels: bucket_label_set, by: 1)
@@ -88,7 +88,7 @@ def observe(value, labels: {})
8888
def get(labels: {})
8989
base_label_set = label_set_for(labels)
9090

91-
all_buckets = buckets + ["+Inf", "sum"]
91+
all_buckets = buckets + ['+Inf', 'sum']
9292

9393
@store.synchronize do
9494
all_buckets.each_with_object({}) do |upper_limit, acc|
@@ -104,8 +104,8 @@ def values
104104
values = @store.all_values
105105

106106
result = values.each_with_object({}) do |(label_set, v), acc|
107-
actual_label_set = label_set.reject{|l| l == :le }
108-
acc[actual_label_set] ||= @buckets.map{|b| [b.to_s, 0.0]}.to_h
107+
actual_label_set = label_set.reject { |l| l == :le }
108+
acc[actual_label_set] ||= @buckets.map { |b| [b.to_s, 0.0] }.to_h
109109
acc[actual_label_set][label_set[:le].to_s] = v
110110
end
111111

@@ -118,7 +118,7 @@ def init_label_set(labels)
118118
base_label_set = label_set_for(labels)
119119

120120
@store.synchronize do
121-
(buckets + ["+Inf", "sum"]).each do |bucket|
121+
(buckets + ['+Inf', 'sum']).each do |bucket|
122122
@store.set(labels: base_label_set.merge(le: bucket.to_s), val: 0)
123123
end
124124
end
@@ -135,8 +135,8 @@ def accumulate_buckets(h)
135135
bucket_acc += bucket_value
136136
end
137137

138-
inf_value = h["+Inf"] || 0.0
139-
h["+Inf"] = inf_value + bucket_acc
138+
inf_value = h['+Inf'] || 0.0
139+
h['+Inf'] = inf_value + bucket_acc
140140
end
141141

142142
def reserved_labels

0 commit comments

Comments
 (0)