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

Export per-pid values from gauges by default #131

Merged
merged 1 commit into from
Jun 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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