Skip to content

Commit 5971cd1

Browse files
committed
Merge branch '65278-fix-puma-master-counter-wipe' into 'master'
Fix active metric files being wiped after the app starts See merge request gitlab-org/gitlab-ce!31668
2 parents 7a22b4b + 35fb27d commit 5971cd1

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Fix active metric files being wiped after the app starts
3+
merge_request: 31668
4+
author:
5+
type: fixed

config.ru

+18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@ end
1717

1818
require ::File.expand_path('../config/environment', __FILE__)
1919

20+
# The following is necessary to ensure stale Prometheus metrics don't accumulate over time.
21+
# It needs to be done as early as here to ensure metrics files aren't deleted.
22+
# After we hit our app in `warmup`, first metrics and corresponding files already being created,
23+
# for example in `lib/gitlab/metrics/requests_rack_middleware.rb`.
24+
def cleanup_prometheus_multiproc_dir
25+
if dir = ::Prometheus::Client.configuration.multiprocess_files_dir
26+
old_metrics = Dir[File.join(dir, '*.db')]
27+
28+
FileUtils.rm_rf(old_metrics)
29+
end
30+
end
31+
32+
def master_process?
33+
Prometheus::PidProvider.worker_id.in? %w(unicorn_master puma_master)
34+
end
35+
2036
warmup do |app|
37+
cleanup_prometheus_multiproc_dir if master_process?
38+
2139
client = Rack::MockRequest.new(app)
2240
client.get('/')
2341
end

config/initializers/7_prometheus_metrics.rb

-19
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,3 @@ def prometheus_default_multiproc_dir
5151
end
5252
end
5353
end
54-
55-
def cleanup_prometheus_multiproc_dir
56-
# The following is necessary to ensure stale Prometheus metrics don't
57-
# accumulate over time. It needs to be done in this hook as opposed to
58-
# inside an init script to ensure metrics files aren't deleted after new
59-
# unicorn workers start after a SIGUSR2 is received.
60-
if dir = ::Prometheus::Client.configuration.multiprocess_files_dir
61-
old_metrics = Dir[File.join(dir, '*.db')]
62-
FileUtils.rm_rf(old_metrics)
63-
end
64-
end
65-
66-
Gitlab::Cluster::LifecycleEvents.on_master_start do
67-
cleanup_prometheus_multiproc_dir
68-
end
69-
70-
Gitlab::Cluster::LifecycleEvents.on_master_restart do
71-
cleanup_prometheus_multiproc_dir
72-
end

lib/gitlab/cluster/puma_worker_killer_observer.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ def callback
1515
private
1616

1717
def log_termination(worker)
18-
labels = { worker: "worker_#{worker.index}" }
19-
20-
@counter.increment(labels)
18+
@counter.increment
2119
end
2220
end
2321
end

spec/lib/gitlab/cluster/puma_worker_killer_observer_spec.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
it 'increments timeout counter' do
1818
worker = double(index: 0)
1919

20-
expect(counter)
21-
.to receive(:increment)
22-
.with({ worker: 'worker_0' })
20+
expect(counter).to receive(:increment)
2321

2422
subject.callback.call(worker)
2523
end

0 commit comments

Comments
 (0)