Skip to content

Commit 8d0264f

Browse files
author
Chris Sinjakli
authored
Merge pull request #131 from prometheus/sinjo-default-unaggregated-gauges
Export per-pid values from gauges by default
2 parents 00d093a + cbecc25 commit 8d0264f

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

lib/prometheus/client/data_stores/direct_file_store.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ class DirectFileStore
2828
class InvalidStoreSettingsError < StandardError; end
2929
AGGREGATION_MODES = [MAX = :max, MIN = :min, SUM = :sum, ALL = :all]
3030
DEFAULT_METRIC_SETTINGS = { aggregation: SUM }
31+
DEFAULT_GAUGE_SETTINGS = { aggregation: ALL }
3132

3233
def initialize(dir:)
3334
@store_settings = { dir: dir }
3435
FileUtils.mkdir_p(dir)
3536
end
3637

3738
def for_metric(metric_name, metric_type:, metric_settings: {})
38-
settings = DEFAULT_METRIC_SETTINGS.merge(metric_settings)
39+
default_settings = DEFAULT_METRIC_SETTINGS
40+
if metric_type == :gauge
41+
default_settings = DEFAULT_GAUGE_SETTINGS
42+
end
43+
44+
settings = default_settings.merge(metric_settings)
3945
validate_metric_settings(settings)
4046

4147
MetricStore.new(metric_name: metric_name,

spec/prometheus/client/data_stores/direct_file_store_spec.rb

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,64 @@
5858
end
5959

6060

61-
it "sums values from different processes" do
62-
allow(Process).to receive(:pid).and_return(12345)
63-
metric_store1 = subject.for_metric(:metric_name, metric_type: :counter)
64-
metric_store1.set(labels: { foo: "bar" }, val: 1)
65-
metric_store1.set(labels: { foo: "baz" }, val: 7)
66-
metric_store1.set(labels: { foo: "yyy" }, val: 3)
67-
68-
allow(Process).to receive(:pid).and_return(23456)
69-
metric_store2 = subject.for_metric(:metric_name, metric_type: :counter)
70-
metric_store2.set(labels: { foo: "bar" }, val: 3)
71-
metric_store2.set(labels: { foo: "baz" }, val: 2)
72-
metric_store2.set(labels: { foo: "zzz" }, val: 1)
73-
74-
expect(metric_store2.all_values).to eq(
75-
{ foo: "bar" } => 4.0,
76-
{ foo: "baz" } => 9.0,
77-
{ foo: "yyy" } => 3.0,
78-
{ foo: "zzz" } => 1.0,
79-
)
80-
81-
# Both processes should return the same value
82-
expect(metric_store1.all_values).to eq(metric_store2.all_values)
61+
context "for a non-gauge metric" do
62+
it "sums values from different processes by default" do
63+
allow(Process).to receive(:pid).and_return(12345)
64+
metric_store1 = subject.for_metric(:metric_name, metric_type: :counter)
65+
metric_store1.set(labels: { foo: "bar" }, val: 1)
66+
metric_store1.set(labels: { foo: "baz" }, val: 7)
67+
metric_store1.set(labels: { foo: "yyy" }, val: 3)
68+
69+
allow(Process).to receive(:pid).and_return(23456)
70+
metric_store2 = subject.for_metric(:metric_name, metric_type: :counter)
71+
metric_store2.set(labels: { foo: "bar" }, val: 3)
72+
metric_store2.set(labels: { foo: "baz" }, val: 2)
73+
metric_store2.set(labels: { foo: "zzz" }, val: 1)
74+
75+
expect(metric_store2.all_values).to eq(
76+
{ foo: "bar" } => 4.0,
77+
{ foo: "baz" } => 9.0,
78+
{ foo: "yyy" } => 3.0,
79+
{ foo: "zzz" } => 1.0,
80+
)
81+
82+
# Both processes should return the same value
83+
expect(metric_store1.all_values).to eq(metric_store2.all_values)
84+
end
85+
end
86+
87+
context "for a gauge metric" do
88+
it "exposes each process's individual value by default" do
89+
allow(Process).to receive(:pid).and_return(12345)
90+
metric_store1 = subject.for_metric(
91+
:metric_name,
92+
metric_type: :gauge,
93+
)
94+
metric_store1.set(labels: { foo: "bar" }, val: 1)
95+
metric_store1.set(labels: { foo: "baz" }, val: 7)
96+
metric_store1.set(labels: { foo: "yyy" }, val: 3)
97+
98+
allow(Process).to receive(:pid).and_return(23456)
99+
metric_store2 = subject.for_metric(
100+
:metric_name,
101+
metric_type: :gauge,
102+
)
103+
metric_store2.set(labels: { foo: "bar" }, val: 3)
104+
metric_store2.set(labels: { foo: "baz" }, val: 2)
105+
metric_store2.set(labels: { foo: "zzz" }, val: 1)
106+
107+
expect(metric_store1.all_values).to eq(
108+
{ foo: "bar", pid: "12345" } => 1.0,
109+
{ foo: "bar", pid: "23456" } => 3.0,
110+
{ foo: "baz", pid: "12345" } => 7.0,
111+
{ foo: "baz", pid: "23456" } => 2.0,
112+
{ foo: "yyy", pid: "12345" } => 3.0,
113+
{ foo: "zzz", pid: "23456" } => 1.0,
114+
)
115+
116+
# Both processes should return the same value
117+
expect(metric_store1.all_values).to eq(metric_store2.all_values)
118+
end
83119
end
84120

85121
context "with a metric that takes MAX instead of SUM" do
@@ -155,7 +191,7 @@
155191
allow(Process).to receive(:pid).and_return(12345)
156192
metric_store1 = subject.for_metric(
157193
:metric_name,
158-
metric_type: :gauge,
194+
metric_type: :counter,
159195
metric_settings: { aggregation: :all }
160196
)
161197
metric_store1.set(labels: { foo: "bar" }, val: 1)
@@ -165,7 +201,7 @@
165201
allow(Process).to receive(:pid).and_return(23456)
166202
metric_store2 = subject.for_metric(
167203
:metric_name,
168-
metric_type: :gauge,
204+
metric_type: :counter,
169205
metric_settings: { aggregation: :all }
170206
)
171207
metric_store2.set(labels: { foo: "bar" }, val: 3)

0 commit comments

Comments
 (0)