Skip to content

Commit

Permalink
Export per-pid values from gauges by default
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Sinjakli <chris@gocardless.com>
  • Loading branch information
Chris Sinjakli committed Jun 14, 2019
1 parent 3652cf7 commit cbecc25
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
8 changes: 7 additions & 1 deletion lib/prometheus/client/data_stores/direct_file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ 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 }
FileUtils.mkdir_p(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,
Expand Down
84 changes: 60 additions & 24 deletions spec/prometheus/client/data_stores/direct_file_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit cbecc25

Please sign in to comment.