diff options
-rw-r--r-- | app/controllers/metrics_controller.rb | 41 | ||||
-rw-r--r-- | app/services/metrics_service.rb | 38 | ||||
-rw-r--r-- | spec/controllers/metrics_controller_spec.rb | 5 |
3 files changed, 47 insertions, 37 deletions
diff --git a/app/controllers/metrics_controller.rb b/app/controllers/metrics_controller.rb index 8b99de06d85..7191a66fe46 100644 --- a/app/controllers/metrics_controller.rb +++ b/app/controllers/metrics_controller.rb @@ -1,50 +1,21 @@ -require 'prometheus/client/formats/text' - class MetricsController < ActionController::Base protect_from_forgery with: :exception + before_action :validate_prometheus_metrics include RequiresHealthToken - before_action :ensure_prometheus_metrics_are_enabled - - CHECKS = [ - Gitlab::HealthChecks::DbCheck, - Gitlab::HealthChecks::RedisCheck, - Gitlab::HealthChecks::FsShardsCheck - ].freeze - def metrics - metrics_text = Prometheus::Client::Formats::Text.marshal_multiprocess(multiprocess_metrics_path) - response = "#{health_metrics_text}\n#{metrics_text}" + response = "#{metrics_service.health_metrics_text}\n#{metrics_service.prometheus_metrics_text}" render text: response, content_type: 'text/plain; version=0.0.4' end private - def ensure_prometheus_metrics_are_enabled - return render_404 unless Gitlab::Metrics.prometheus_metrics_enabled? - end - - def multiprocess_metrics_path - Rails.root.join(ENV['prometheus_multiproc_dir']) + def metrics_service + @metrics_service ||= MetricsService.new end - def health_metrics_text - results = CHECKS.flat_map(&:metrics) - - types = results.map(&:name).uniq.map { |metric_name| "# TYPE #{metric_name} gauge" } - metrics = results.map(&method(:metric_to_prom_line)) - - types.concat(metrics).join("\n") - end - - def metric_to_prom_line(metric) - labels = metric.labels&.map { |key, value| "#{key}=\"#{value}\"" }&.join(',') || '' - - if labels.empty? - "#{metric.name} #{metric.value}" - else - "#{metric.name}{#{labels}} #{metric.value}" - end + def validate_prometheus_metrics + render_404 unless Gitlab::Metrics.prometheus_metrics_enabled? end end diff --git a/app/services/metrics_service.rb b/app/services/metrics_service.rb new file mode 100644 index 00000000000..350c3639e92 --- /dev/null +++ b/app/services/metrics_service.rb @@ -0,0 +1,38 @@ +require 'prometheus/client/formats/text' + +class MetricsService + CHECKS = [ + Gitlab::HealthChecks::DbCheck, + Gitlab::HealthChecks::RedisCheck, + Gitlab::HealthChecks::FsShardsCheck + ].freeze + + def prometheus_metrics_text + Prometheus::Client::Formats::Text.marshal_multiprocess(multiprocess_metrics_path) + end + + def health_metrics_text + results = CHECKS.flat_map(&:metrics) + + types = results.map(&:name).uniq.map { |metric_name| "# TYPE #{metric_name} gauge" } + metrics = results.map(&method(:metric_to_prom_line)) + + types.concat(metrics).join("\n") + end + + private + + def multiprocess_metrics_path + Rails.root.join(ENV['prometheus_multiproc_dir']) + end + + def metric_to_prom_line(metric) + labels = metric.labels&.map { |key, value| "#{key}=\"#{value}\"" }&.join(',') || '' + + if labels.empty? + "#{metric.name} #{metric.value}" + else + "#{metric.name}{#{labels}} #{metric.value}" + end + end +end diff --git a/spec/controllers/metrics_controller_spec.rb b/spec/controllers/metrics_controller_spec.rb index c09c3a1f6b7..99ad7b7738b 100644 --- a/spec/controllers/metrics_controller_spec.rb +++ b/spec/controllers/metrics_controller_spec.rb @@ -50,7 +50,9 @@ describe MetricsController do end context 'prometheus metrics are disabled' do - allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(false) + before do + allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(false) + end it 'returns proper response' do get :metrics @@ -65,6 +67,5 @@ describe MetricsController do expect(response.status).to eq(404) end end - end end |