Skip to content

Commit 62ab050

Browse files
author
Joao Amaro
committed
Implements #init_label_set method for all metric classes
1 parent 8d0264f commit 62ab050

7 files changed

Lines changed: 86 additions & 0 deletions

File tree

lib/prometheus/client/histogram.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ def values
9494
end
9595
end
9696

97+
def init_label_set(labels)
98+
base_label_set = label_set_for(labels)
99+
100+
@store.synchronize do
101+
[*@buckets, "+Inf"].each do |bucket|
102+
# This is basically faster than doing `.merge`
103+
bucket_label_set = base_label_set.dup
104+
bucket_label_set[:le] = bucket.to_s
105+
sum_label_set = base_label_set.dup
106+
sum_label_set[:le] = "sum"
107+
108+
@store.set(labels: bucket_label_set, val: 0)
109+
@store.set(labels: sum_label_set, val: 0)
110+
end
111+
end
112+
end
113+
97114
private
98115

99116
# Modifies the passed in parameter

lib/prometheus/client/metric.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def with_labels(labels)
5555
store_settings: @store_settings)
5656
end
5757

58+
def init_label_set(labels)
59+
@store.set(labels: label_set_for(labels), val: 0)
60+
end
61+
5862
# Returns all label sets with their values
5963
def values
6064
@store.all_values

lib/prometheus/client/summary.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ def values
4545
end
4646
end
4747

48+
def init_label_set(labels)
49+
base_label_set = label_set_for(labels)
50+
51+
@store.synchronize do
52+
@store.set(labels: base_label_set.merge(quantile: "count"), val: 0)
53+
@store.set(labels: base_label_set.merge(quantile: "sum"), val: 0)
54+
end
55+
end
56+
4857
private
4958

5059
def reserved_labels

spec/prometheus/client/counter_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@
7979
end.to change { counter.get }.by(100.0)
8080
end
8181
end
82+
83+
describe '#init_label_set' do
84+
let(:expected_labels) { [:test] }
85+
86+
it 'initializes the metric for a given label set' do
87+
expect(counter.values).to eql({})
88+
89+
counter.init_label_set(test: 'value')
90+
91+
expect(counter.values).to eql({test: 'value'} => 0.0)
92+
end
93+
end
8294
end

spec/prometheus/client/gauge_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,16 @@
161161
end.to change { gauge.get }.by(-100.0)
162162
end
163163
end
164+
165+
describe '#init_label_set' do
166+
let(:expected_labels) { [:test] }
167+
168+
it 'initializes the metric for a given label set' do
169+
expect(gauge.values).to eql({})
170+
171+
gauge.init_label_set(test: 'value')
172+
173+
expect(gauge.values).to eql({test: 'value'} => 0.0)
174+
end
175+
end
164176
end

spec/prometheus/client/histogram_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,20 @@
118118
)
119119
end
120120
end
121+
122+
describe '#init_label_set' do
123+
let(:expected_labels) { [:status] }
124+
125+
it 'initializes the metric for a given label set' do
126+
expect(histogram.values).to eql({})
127+
128+
histogram.init_label_set(status: 'bar')
129+
histogram.init_label_set(status: 'foo')
130+
131+
expect(histogram.values).to eql(
132+
{ status: 'bar' } => { "2.5" => 0.0, "5" => 0.0, "10" => 0.0, "+Inf" => 0.0, "sum" => 0.0 },
133+
{ status: 'foo' } => { "2.5" => 0.0, "5" => 0.0, "10" => 0.0, "+Inf" => 0.0, "sum" => 0.0 },
134+
)
135+
end
136+
end
121137
end

spec/prometheus/client/summary_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,20 @@
9999
)
100100
end
101101
end
102+
103+
describe '#init_label_set' do
104+
let(:expected_labels) { [:status] }
105+
106+
it 'initializes the metric for a given label set' do
107+
expect(summary.values).to eql({})
108+
109+
summary.init_label_set(status: 'bar')
110+
summary.init_label_set(status: 'foo')
111+
112+
expect(summary.values).to eql(
113+
{ status: 'bar' } => { "count" => 0.0, "sum" => 0.0 },
114+
{ status: 'foo' } => { "count" => 0.0, "sum" => 0.0 },
115+
)
116+
end
117+
end
102118
end

0 commit comments

Comments
 (0)