Commit 416b61e0 authored by Sean McGivern's avatar Sean McGivern 🏖 Committed by Clement Ho

Merge branch '33823_do_not_enable_defaults_when_metrics_folder_is_missing' into 'master'

Do not enable prometheus metrics when metrics folder is missing

See merge request !12263
parent 9c763521
...@@ -311,6 +311,10 @@ ...@@ -311,6 +311,10 @@
= f.label :prometheus_metrics_enabled do = f.label :prometheus_metrics_enabled do
= f.check_box :prometheus_metrics_enabled = f.check_box :prometheus_metrics_enabled
Enable Prometheus Metrics Enable Prometheus Metrics
- unless Gitlab::Metrics.metrics_folder_present?
.help-block
%strong.cred WARNING:
Environment variable `prometheus_multiproc_dir` does not exist or is not pointing to a valid directory.
%fieldset %fieldset
%legend Background Jobs %legend Background Jobs
......
...@@ -5,6 +5,11 @@ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) ...@@ -5,6 +5,11 @@ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
# set default directory for multiproces metrics gathering
if ENV['RAILS_ENV'] == 'development' || ENV['RAILS_ENV'] == 'test'
ENV['prometheus_multiproc_dir'] ||= 'tmp/prometheus_multiproc_dir'
end
# Default Bootsnap configuration from https://github.com/Shopify/bootsnap#usage # Default Bootsnap configuration from https://github.com/Shopify/bootsnap#usage
require 'bootsnap' require 'bootsnap'
Bootsnap.setup( Bootsnap.setup(
......
...@@ -5,8 +5,16 @@ module Gitlab ...@@ -5,8 +5,16 @@ module Gitlab
module Prometheus module Prometheus
include Gitlab::CurrentSettings include Gitlab::CurrentSettings
def metrics_folder_present?
ENV.has_key?('prometheus_multiproc_dir') &&
::Dir.exist?(ENV['prometheus_multiproc_dir']) &&
::File.writable?(ENV['prometheus_multiproc_dir'])
end
def prometheus_metrics_enabled? def prometheus_metrics_enabled?
@prometheus_metrics_enabled ||= current_application_settings[:prometheus_metrics_enabled] || false return @prometheus_metrics_enabled if defined?(@prometheus_metrics_enabled)
@prometheus_metrics_enabled = prometheus_metrics_enabled_unmemoized
end end
def registry def registry
...@@ -36,6 +44,12 @@ module Gitlab ...@@ -36,6 +44,12 @@ module Gitlab
NullMetric.new NullMetric.new
end end
end end
private
def prometheus_metrics_enabled_unmemoized
metrics_folder_present? && current_application_settings[:prometheus_metrics_enabled] || false
end
end end
end end
end end
...@@ -15,6 +15,36 @@ describe Gitlab::Metrics do ...@@ -15,6 +15,36 @@ describe Gitlab::Metrics do
end end
end end
describe '.prometheus_metrics_enabled_unmemoized' do
subject { described_class.send(:prometheus_metrics_enabled_unmemoized) }
context 'prometheus metrics enabled in config' do
before do
allow(described_class).to receive(:current_application_settings).and_return(prometheus_metrics_enabled: true)
end
context 'when metrics folder is present' do
before do
allow(described_class).to receive(:metrics_folder_present?).and_return(true)
end
it 'metrics are enabled' do
expect(subject).to eq(true)
end
end
context 'when metrics folder is missing' do
before do
allow(described_class).to receive(:metrics_folder_present?).and_return(false)
end
it 'metrics are disabled' do
expect(subject).to eq(false)
end
end
end
end
describe '.prometheus_metrics_enabled?' do describe '.prometheus_metrics_enabled?' do
it 'returns a boolean' do it 'returns a boolean' do
expect(described_class.prometheus_metrics_enabled?).to be_in([true, false]) expect(described_class.prometheus_metrics_enabled?).to be_in([true, false])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment