Skip to content

Commit

Permalink
Merge pull request prometheus#148 from prometheus/sinjo-reopen-on-fork
Browse files Browse the repository at this point in the history
Reopen files in DirectFileStore when the process has forked
  • Loading branch information
Chris Sinjakli authored Jul 26, 2019
2 parents 0a003e9 + 8a170e4 commit 64d0b1f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/prometheus/client/data_stores/direct_file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ def store_key(labels)
end

def internal_store
@internal_store ||= FileMappedDict.new(filemap_filename)
if @store_opened_by_pid != process_id
@store_opened_by_pid = process_id
@internal_store = FileMappedDict.new(filemap_filename)
else
@internal_store
end
end

# Filename for this metric's PStore (one per process)
Expand Down
12 changes: 12 additions & 0 deletions spec/prometheus/client/data_stores/direct_file_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@
ms2.increment(labels: {}, by: 1)
end

context "when process is forked" do
it "opens a new internal store to avoid two processes using the same file" do
allow(Process).to receive(:pid).and_return(12345)
metric_store = subject.for_metric(:metric_name, metric_type: :counter)
metric_store.increment(labels: {}, by: 1)

allow(Process).to receive(:pid).and_return(23456)
metric_store.increment(labels: {}, by: 1)
expect(Dir.glob('/tmp/prometheus_test/*').size).to eq(2)
expect(metric_store.all_values).to eq({} => 2.0)
end
end

context "for a non-gauge metric" do
it "sums values from different processes by default" do
Expand Down

0 comments on commit 64d0b1f

Please sign in to comment.