Skip to content

Commit

Permalink
Implements #init_label_set method for all metric classes
Browse files Browse the repository at this point in the history
Signed-off-by: Joao Bernardo <jb.amaro95@gmail.com>
  • Loading branch information
jbernardo95 committed Oct 1, 2019
1 parent 8d0264f commit 6ad23c9
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/prometheus/client/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ def values
end
end

def init_label_set(labels)
base_label_set = label_set_for(labels)

@store.synchronize do
[*@buckets, "+Inf"].each do |bucket|
# This is basically faster than doing `.merge`
bucket_label_set = base_label_set.dup
bucket_label_set[:le] = bucket.to_s
sum_label_set = base_label_set.dup
sum_label_set[:le] = "sum"

@store.set(labels: bucket_label_set, val: 0)
@store.set(labels: sum_label_set, val: 0)
end
end
end

private

# Modifies the passed in parameter
Expand Down
4 changes: 4 additions & 0 deletions lib/prometheus/client/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def with_labels(labels)
store_settings: @store_settings)
end

def init_label_set(labels)
@store.set(labels: label_set_for(labels), val: 0)
end

# Returns all label sets with their values
def values
@store.all_values
Expand Down
9 changes: 9 additions & 0 deletions lib/prometheus/client/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def values
end
end

def init_label_set(labels)
base_label_set = label_set_for(labels)

@store.synchronize do
@store.set(labels: base_label_set.merge(quantile: "count"), val: 0)
@store.set(labels: base_label_set.merge(quantile: "sum"), val: 0)
end
end

private

def reserved_labels
Expand Down
12 changes: 12 additions & 0 deletions spec/prometheus/client/counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@
end.to change { counter.get }.by(100.0)
end
end

describe '#init_label_set' do
let(:expected_labels) { [:test] }

it 'initializes the metric for a given label set' do
expect(counter.values).to eql({})

counter.init_label_set(test: 'value')

expect(counter.values).to eql({test: 'value'} => 0.0)
end
end
end
12 changes: 12 additions & 0 deletions spec/prometheus/client/gauge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,16 @@
end.to change { gauge.get }.by(-100.0)
end
end

describe '#init_label_set' do
let(:expected_labels) { [:test] }

it 'initializes the metric for a given label set' do
expect(gauge.values).to eql({})

gauge.init_label_set(test: 'value')

expect(gauge.values).to eql({test: 'value'} => 0.0)
end
end
end
16 changes: 16 additions & 0 deletions spec/prometheus/client/histogram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,20 @@
)
end
end

describe '#init_label_set' do
let(:expected_labels) { [:status] }

it 'initializes the metric for a given label set' do
expect(histogram.values).to eql({})

histogram.init_label_set(status: 'bar')
histogram.init_label_set(status: 'foo')

expect(histogram.values).to eql(
{ status: 'bar' } => { "2.5" => 0.0, "5" => 0.0, "10" => 0.0, "+Inf" => 0.0, "sum" => 0.0 },
{ status: 'foo' } => { "2.5" => 0.0, "5" => 0.0, "10" => 0.0, "+Inf" => 0.0, "sum" => 0.0 },
)
end
end
end
16 changes: 16 additions & 0 deletions spec/prometheus/client/summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,20 @@
)
end
end

describe '#init_label_set' do
let(:expected_labels) { [:status] }

it 'initializes the metric for a given label set' do
expect(summary.values).to eql({})

summary.init_label_set(status: 'bar')
summary.init_label_set(status: 'foo')

expect(summary.values).to eql(
{ status: 'bar' } => { "count" => 0.0, "sum" => 0.0 },
{ status: 'foo' } => { "count" => 0.0, "sum" => 0.0 },
)
end
end
end

0 comments on commit 6ad23c9

Please sign in to comment.