Skip to content

Commit 3f98158

Browse files
author
Aleksei Lipniagov
committed
Fix metric files being wiped after the app starts
When we hit our app with the initial request, in `warmup`, some metrics already being created as well as corresponding files. If we do `multiproc_file_dir` cleanup after that, we delete the files from the dir while keeping them in memory which leads to the incorrect behavior: the metric is being updated in in-memory, while is not present in the db, not sent to Prometheus as the result.
1 parent 7a48b4d commit 3f98158

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
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

+15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@ end
1717

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

20+
21+
# The following is necessary to ensure stale Prometheus metrics don't accumulate over time.
22+
# It needs to be done as early as here to ensure metrics files aren't deleted.
23+
# After we hit our app in `warmup`, first metrics and corresponding files already being created,
24+
# for example in `lib/gitlab/metrics/requests_rack_middleware.rb`.
25+
def cleanup_prometheus_multiproc_dir
26+
if dir = ::Prometheus::Client.configuration.multiprocess_files_dir
27+
old_metrics = Dir[File.join(dir, '*.db')]
28+
29+
FileUtils.rm_rf(old_metrics)
30+
end
31+
end
32+
2033
warmup do |app|
34+
cleanup_prometheus_multiproc_dir
35+
2136
client = Rack::MockRequest.new(app)
2237
client.get('/')
2338
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

0 commit comments

Comments
 (0)