Skip to content

Commit 0e0f17f

Browse files
committed
Implements #init_label_set method for all metric classes
Signed-off-by: Joao Bernardo <jb.amaro95@gmail.com>
1 parent 8d0264f commit 0e0f17f

File tree

8 files changed

+85
-0
lines changed

8 files changed

+85
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ class MyComponent
256256
end
257257
```
258258

259+
### `init_label_set`
260+
261+
The time series of a metric are not initialized until something happens. For counters, for example, this means that the time series do not exist until the counter is incremented for the first time.
262+
263+
To get around this problem the client provides the `init_label_set` method that can be used to initialise the time series of a metric for a given label set.
264+
259265
### Reserved labels
260266

261267
The following labels are reserved by the client library, and attempting to use them in a

lib/prometheus/client/histogram.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ 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", "sum"]).each do |bucket|
102+
@store.set(labels: base_label_set.merge(le: bucket.to_s), val: 0)
103+
end
104+
end
105+
end
106+
97107
private
98108

99109
# 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)