diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-05-19 18:03:10 +0300 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-06-02 20:45:58 +0300 |
commit | 0f4050430d400daffbc5a68b15d79b896bb8a692 (patch) | |
tree | 7140c4edba672350570f53f26effe85ba4ba6289 /app/controllers/metrics_controller.rb | |
parent | cf932df2348dc3ccd06ca557b68edc60f518c893 (diff) |
Split metrics from health controller into metrics controller
Diffstat (limited to 'app/controllers/metrics_controller.rb')
-rw-r--r-- | app/controllers/metrics_controller.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/app/controllers/metrics_controller.rb b/app/controllers/metrics_controller.rb new file mode 100644 index 00000000000..a4ba77e235f --- /dev/null +++ b/app/controllers/metrics_controller.rb @@ -0,0 +1,41 @@ +require 'prometheus/client/formats/text' + +class MetricsController < ActionController::Base + protect_from_forgery with: :exception + + CHECKS = [ + Gitlab::HealthChecks::DbCheck, + Gitlab::HealthChecks::RedisCheck, + Gitlab::HealthChecks::FsShardsCheck, + ].freeze + + def metrics + render_404 unless Gitlab::Metrics.prometheus_metrics_enabled? + + metrics_text = Prometheus::Client::Formats::Text.marshal_multiprocess(Settings.gitlab['prometheus_multiproc_dir']) + response = health_metrics_text + "\n" + metrics_text + + render text: response, content_type: 'text/plain; version=0.0.4' + end + + private + + 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 + end +end |