Do not enable prometheus metrics when data folder is not present.

+ Set defaults correctly only for when not in production or staging
+ set ENV['prometheus_multiproc_dir'] in config/boot.rb instead of config.ru

Test prometheus metrics unmemoized
This commit is contained in:
Pawel Chojnacki 2017-06-15 23:41:47 +02:00
parent a4a5cbf29a
commit ed5c7d11b1
4 changed files with 49 additions and 2 deletions

View file

@ -325,6 +325,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

View file

@ -6,7 +6,9 @@ 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 # set default directory for multiproces metrics gathering
if ENV['RAILS_ENV'] == 'development' || ENV['RAILS_ENV'] == 'test'
ENV['prometheus_multiproc_dir'] ||= 'tmp/prometheus_multiproc_dir' 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'

View file

@ -5,8 +5,13 @@ 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 @prometheus_metrics_enabled ||= prometheus_metrics_enabled_unmemoized
end end
def registry def registry
@ -36,6 +41,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

View file

@ -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])