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 Nov 5, 2019
1 parent 8d0264f commit 0e0f17f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ class MyComponent
end
```

### `init_label_set`

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.

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.

### Reserved labels

The following labels are reserved by the client library, and attempting to use them in a
Expand Down
10 changes: 10 additions & 0 deletions lib/prometheus/client/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def values
end
end

def init_label_set(labels)
base_label_set = label_set_for(labels)

@store.synchronize do
(buckets + ["+Inf", "sum"]).each do |bucket|
@store.set(labels: base_label_set.merge(le: bucket.to_s), 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 0e0f17f

Please sign in to comment.