Skip to content

Commit 049948b

Browse files
committed
Add :clean_dir option and clean_pid method to DirectFileStore.
Signed-off-by: Stefan Sundin <stefan@stefansundin.com>
1 parent 7129453 commit 049948b

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

lib/prometheus/client/data_stores/direct_file_store.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ class InvalidStoreSettingsError < StandardError; end
3333
DEFAULT_METRIC_SETTINGS = { aggregation: SUM }
3434
DEFAULT_GAUGE_SETTINGS = { aggregation: ALL }
3535

36-
def initialize(dir:)
36+
def initialize(dir:, clean_dir: false)
3737
@store_settings = { dir: dir }
3838
FileUtils.mkdir_p(dir)
39+
40+
if clean_dir
41+
Dir.glob(File.join(dir, "#{ MetricStore::FILENAME_PREFIX }_*___*.bin")).each do |file_path|
42+
File.unlink(file_path)
43+
end
44+
end
3945
end
4046

4147
def for_metric(metric_name, metric_type:, metric_settings: {})
@@ -52,6 +58,12 @@ def for_metric(metric_name, metric_type:, metric_settings: {})
5258
metric_settings: settings)
5359
end
5460

61+
def clean_pid(pid)
62+
Dir.glob(File.join(@store_settings[:dir], "#{ MetricStore::FILENAME_PREFIX }_*___#{pid}.bin")).each do |file_path|
63+
File.unlink(file_path)
64+
end
65+
end
66+
5567
private
5668

5769
def validate_metric_settings(metric_settings)
@@ -68,6 +80,7 @@ def validate_metric_settings(metric_settings)
6880
end
6981

7082
class MetricStore
83+
FILENAME_PREFIX = "prometheus_#{ Prometheus::Client::VERSION }"
7184
attr_reader :metric_name, :store_settings
7285

7386
def initialize(metric_name:, store_settings:, metric_settings:)
@@ -168,12 +181,12 @@ def internal_store
168181

169182
# Filename for this metric's PStore (one per process)
170183
def filemap_filename
171-
filename = "metric_#{ metric_name }___#{ process_id }.bin"
184+
filename = "#{ FILENAME_PREFIX }_#{ metric_name }___#{ process_id }.bin"
172185
File.join(@store_settings[:dir], filename)
173186
end
174187

175188
def stores_for_metric
176-
Dir.glob(File.join(@store_settings[:dir], "metric_#{ metric_name }___*"))
189+
Dir.glob(File.join(@store_settings[:dir], "#{ FILENAME_PREFIX }_#{ metric_name }___*.bin"))
177190
end
178191

179192
def process_id

spec/prometheus/client/data_stores/direct_file_store_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
require 'examples/data_store_example'
55

66
describe Prometheus::Client::DataStores::DirectFileStore do
7-
subject { described_class.new(dir: "/tmp/prometheus_test") }
7+
subject { described_class.new(dir: "/tmp/prometheus_test", clean_dir: true) }
88
let(:metric_store) { subject.for_metric(:metric_name, metric_type: :counter) }
99

10-
# Reset the PStores
11-
before do
12-
Dir.glob('/tmp/prometheus_test/*').each { |file| File.delete(file) }
13-
end
14-
1510
it_behaves_like Prometheus::Client::DataStores
1611

1712
it "only accepts valid :aggregation as Metric Settings" do
@@ -57,6 +52,15 @@
5752
ms2.increment(labels: {}, by: 1)
5853
end
5954

55+
it "can clean files from old processes" do
56+
allow(Process).to receive(:pid).and_return(12345)
57+
ms = metric_store
58+
ms.increment(labels: {}, by: 1)
59+
expect(ms.all_values).to eq({} => 1.0)
60+
subject.clean_pid(12345)
61+
expect(ms.all_values).to be_empty
62+
end
63+
6064
context "when process is forked" do
6165
it "opens a new internal store to avoid two processes using the same file" do
6266
allow(Process).to receive(:pid).and_return(12345)

0 commit comments

Comments
 (0)