Skip to content

Commit c792af8

Browse files
author
Daniel Magliola
authored
Merge pull request #156 from jbernardo95/init-label-set
Implements #init_label_set method for all metric classes
2 parents 9f08a53 + 0e0f17f commit c792af8

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
@@ -254,6 +254,12 @@ class MyComponent
254254
end
255255
```
256256

257+
### `init_label_set`
258+
259+
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.
260+
261+
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.
262+
257263
### Reserved labels
258264

259265
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
@@ -102,4 +102,16 @@
102102
end
103103
end
104104
end
105+
106+
describe '#init_label_set' do
107+
let(:expected_labels) { [:test] }
108+
109+
it 'initializes the metric for a given label set' do
110+
expect(counter.values).to eql({})
111+
112+
counter.init_label_set(test: 'value')
113+
114+
expect(counter.values).to eql({test: 'value'} => 0.0)
115+
end
116+
end
105117
end

spec/prometheus/client/gauge_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,16 @@
184184
end.to change { gauge.get }.by(-100.0)
185185
end
186186
end
187+
188+
describe '#init_label_set' do
189+
let(:expected_labels) { [:test] }
190+
191+
it 'initializes the metric for a given label set' do
192+
expect(gauge.values).to eql({})
193+
194+
gauge.init_label_set(test: 'value')
195+
196+
expect(gauge.values).to eql({test: 'value'} => 0.0)
197+
end
198+
end
187199
end

spec/prometheus/client/histogram_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,20 @@
147147
)
148148
end
149149
end
150+
151+
describe '#init_label_set' do
152+
let(:expected_labels) { [:status] }
153+
154+
it 'initializes the metric for a given label set' do
155+
expect(histogram.values).to eql({})
156+
157+
histogram.init_label_set(status: 'bar')
158+
histogram.init_label_set(status: 'foo')
159+
160+
expect(histogram.values).to eql(
161+
{ status: 'bar' } => { "2.5" => 0.0, "5" => 0.0, "10" => 0.0, "+Inf" => 0.0, "sum" => 0.0 },
162+
{ status: 'foo' } => { "2.5" => 0.0, "5" => 0.0, "10" => 0.0, "+Inf" => 0.0, "sum" => 0.0 },
163+
)
164+
end
165+
end
150166
end

spec/prometheus/client/summary_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,20 @@
126126
)
127127
end
128128
end
129+
130+
describe '#init_label_set' do
131+
let(:expected_labels) { [:status] }
132+
133+
it 'initializes the metric for a given label set' do
134+
expect(summary.values).to eql({})
135+
136+
summary.init_label_set(status: 'bar')
137+
summary.init_label_set(status: 'foo')
138+
139+
expect(summary.values).to eql(
140+
{ status: 'bar' } => { "count" => 0.0, "sum" => 0.0 },
141+
{ status: 'foo' } => { "count" => 0.0, "sum" => 0.0 },
142+
)
143+
end
144+
end
129145
end

0 commit comments

Comments
 (0)