From cbecc2514ffa40dee8b73d8b8a1fc7198832e178 Mon Sep 17 00:00:00 2001 From: Chris Sinjakli Date: Fri, 14 Jun 2019 17:55:03 +0100 Subject: [PATCH] Export per-pid values from gauges by default Signed-off-by: Chris Sinjakli --- .../client/data_stores/direct_file_store.rb | 8 +- .../data_stores/direct_file_store_spec.rb | 84 +++++++++++++------ 2 files changed, 67 insertions(+), 25 deletions(-) diff --git a/lib/prometheus/client/data_stores/direct_file_store.rb b/lib/prometheus/client/data_stores/direct_file_store.rb index 8c653e31..59b6e53f 100644 --- a/lib/prometheus/client/data_stores/direct_file_store.rb +++ b/lib/prometheus/client/data_stores/direct_file_store.rb @@ -28,6 +28,7 @@ class DirectFileStore class InvalidStoreSettingsError < StandardError; end AGGREGATION_MODES = [MAX = :max, MIN = :min, SUM = :sum, ALL = :all] DEFAULT_METRIC_SETTINGS = { aggregation: SUM } + DEFAULT_GAUGE_SETTINGS = { aggregation: ALL } def initialize(dir:) @store_settings = { dir: dir } @@ -35,7 +36,12 @@ def initialize(dir:) end def for_metric(metric_name, metric_type:, metric_settings: {}) - settings = DEFAULT_METRIC_SETTINGS.merge(metric_settings) + default_settings = DEFAULT_METRIC_SETTINGS + if metric_type == :gauge + default_settings = DEFAULT_GAUGE_SETTINGS + end + + settings = default_settings.merge(metric_settings) validate_metric_settings(settings) MetricStore.new(metric_name: metric_name, diff --git a/spec/prometheus/client/data_stores/direct_file_store_spec.rb b/spec/prometheus/client/data_stores/direct_file_store_spec.rb index 9285f941..52611adb 100644 --- a/spec/prometheus/client/data_stores/direct_file_store_spec.rb +++ b/spec/prometheus/client/data_stores/direct_file_store_spec.rb @@ -58,28 +58,64 @@ end - it "sums values from different processes" do - allow(Process).to receive(:pid).and_return(12345) - metric_store1 = subject.for_metric(:metric_name, metric_type: :counter) - metric_store1.set(labels: { foo: "bar" }, val: 1) - metric_store1.set(labels: { foo: "baz" }, val: 7) - metric_store1.set(labels: { foo: "yyy" }, val: 3) - - allow(Process).to receive(:pid).and_return(23456) - metric_store2 = subject.for_metric(:metric_name, metric_type: :counter) - metric_store2.set(labels: { foo: "bar" }, val: 3) - metric_store2.set(labels: { foo: "baz" }, val: 2) - metric_store2.set(labels: { foo: "zzz" }, val: 1) - - expect(metric_store2.all_values).to eq( - { foo: "bar" } => 4.0, - { foo: "baz" } => 9.0, - { foo: "yyy" } => 3.0, - { foo: "zzz" } => 1.0, - ) - - # Both processes should return the same value - expect(metric_store1.all_values).to eq(metric_store2.all_values) + context "for a non-gauge metric" do + it "sums values from different processes by default" do + allow(Process).to receive(:pid).and_return(12345) + metric_store1 = subject.for_metric(:metric_name, metric_type: :counter) + metric_store1.set(labels: { foo: "bar" }, val: 1) + metric_store1.set(labels: { foo: "baz" }, val: 7) + metric_store1.set(labels: { foo: "yyy" }, val: 3) + + allow(Process).to receive(:pid).and_return(23456) + metric_store2 = subject.for_metric(:metric_name, metric_type: :counter) + metric_store2.set(labels: { foo: "bar" }, val: 3) + metric_store2.set(labels: { foo: "baz" }, val: 2) + metric_store2.set(labels: { foo: "zzz" }, val: 1) + + expect(metric_store2.all_values).to eq( + { foo: "bar" } => 4.0, + { foo: "baz" } => 9.0, + { foo: "yyy" } => 3.0, + { foo: "zzz" } => 1.0, + ) + + # Both processes should return the same value + expect(metric_store1.all_values).to eq(metric_store2.all_values) + end + end + + context "for a gauge metric" do + it "exposes each process's individual value by default" do + allow(Process).to receive(:pid).and_return(12345) + metric_store1 = subject.for_metric( + :metric_name, + metric_type: :gauge, + ) + metric_store1.set(labels: { foo: "bar" }, val: 1) + metric_store1.set(labels: { foo: "baz" }, val: 7) + metric_store1.set(labels: { foo: "yyy" }, val: 3) + + allow(Process).to receive(:pid).and_return(23456) + metric_store2 = subject.for_metric( + :metric_name, + metric_type: :gauge, + ) + metric_store2.set(labels: { foo: "bar" }, val: 3) + metric_store2.set(labels: { foo: "baz" }, val: 2) + metric_store2.set(labels: { foo: "zzz" }, val: 1) + + expect(metric_store1.all_values).to eq( + { foo: "bar", pid: "12345" } => 1.0, + { foo: "bar", pid: "23456" } => 3.0, + { foo: "baz", pid: "12345" } => 7.0, + { foo: "baz", pid: "23456" } => 2.0, + { foo: "yyy", pid: "12345" } => 3.0, + { foo: "zzz", pid: "23456" } => 1.0, + ) + + # Both processes should return the same value + expect(metric_store1.all_values).to eq(metric_store2.all_values) + end end context "with a metric that takes MAX instead of SUM" do @@ -155,7 +191,7 @@ allow(Process).to receive(:pid).and_return(12345) metric_store1 = subject.for_metric( :metric_name, - metric_type: :gauge, + metric_type: :counter, metric_settings: { aggregation: :all } ) metric_store1.set(labels: { foo: "bar" }, val: 1) @@ -165,7 +201,7 @@ allow(Process).to receive(:pid).and_return(23456) metric_store2 = subject.for_metric( :metric_name, - metric_type: :gauge, + metric_type: :counter, metric_settings: { aggregation: :all } ) metric_store2.set(labels: { foo: "bar" }, val: 3)