Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements #init_label_set method for all metric classes #156

Merged
merged 1 commit into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
jbernardo95 marked this conversation as resolved.
Show resolved Hide resolved

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
jbernardo95 marked this conversation as resolved.
Show resolved Hide resolved

# 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