Skip to content

Commit

Permalink
Merge pull request #147 from prometheus/sinjo-consistent-metric-stores
Browse files Browse the repository at this point in the history
Test that stores return floats and fix DirectFileStore to do so
  • Loading branch information
Chris Sinjakli committed Jul 24, 2019
2 parents 61bf297 + 42088b3 commit 0a003e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/prometheus/client/data_stores/direct_file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def all_values
end

# Aggregate all the different values for each label_set
stores_data.each_with_object({}) do |(label_set, values), acc|
aggregate_hash = Hash.new { |hash, key| hash[key] = 0.0 }
stores_data.each_with_object(aggregate_hash) do |(label_set, values), acc|
acc[label_set] = aggregate_values(values)
end
end
Expand Down
32 changes: 25 additions & 7 deletions spec/examples/data_store_example.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
# encoding: UTF-8

# NOTE: Do not change instances of `eql` to `eq` in this file.
#
# The interface of a store is a labelset (hash of hashes) to a double. It's important
# that we check the values are doubles rather than integers. `==`, which is what `eq`
# calls allows conversion between floats and integers (i.e. `5 == 5.0`). `eql` enforces
# that the two numbers are of the same type.
shared_examples_for Prometheus::Client::DataStores do
describe "MetricStore#set and #get" do
it "returns the value set for each labelset" do
expect(metric_store.get(labels: { foo: "bar" })).to eql(0.0)
end
end

describe "MetricStore#set and #get" do
it "returns the value set for each labelset" do
metric_store.set(labels: { foo: "bar" }, val: 5)
metric_store.set(labels: { foo: "baz" }, val: 2)
expect(metric_store.get(labels: { foo: "bar" })).to eq(5)
expect(metric_store.get(labels: { foo: "baz" })).to eq(2)
expect(metric_store.get(labels: { foo: "bat" })).to eq(0)
expect(metric_store.get(labels: { foo: "bar" })).to eql(5.0)
expect(metric_store.get(labels: { foo: "baz" })).to eql(2.0)
expect(metric_store.get(labels: { foo: "bat" })).to eql(0.0)
end
end

Expand All @@ -20,9 +32,9 @@
metric_store.increment(labels: { foo: "baz" }, by: 7)
metric_store.increment(labels: { foo: "zzz" }, by: 3)

expect(metric_store.get(labels: { foo: "bar" })).to eq(6)
expect(metric_store.get(labels: { foo: "baz" })).to eq(9)
expect(metric_store.get(labels: { foo: "zzz" })).to eq(3)
expect(metric_store.get(labels: { foo: "bar" })).to eql(6.0)
expect(metric_store.get(labels: { foo: "baz" })).to eql(9.0)
expect(metric_store.get(labels: { foo: "zzz" })).to eql(3.0)
end
end

Expand All @@ -49,10 +61,16 @@
metric_store.set(labels: { foo: "bar" }, val: 5)
metric_store.set(labels: { foo: "baz" }, val: 2)

expect(metric_store.all_values).to eq(
expect(metric_store.all_values).to eql(
{ foo: "bar" } => 5.0,
{ foo: "baz" } => 2.0,
)
end

context "for a combination of labels that hasn't had a value set" do
it "returns 0.0" do
expect(metric_store.all_values[{ foo: "bar" }]).to eql(0.0)
end
end
end
end

0 comments on commit 0a003e9

Please sign in to comment.