diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-05-23 16:42:36 +0300 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-06-02 20:45:58 +0300 |
commit | 254830c1f963f344585a45d96a03985e1ec2df0e (patch) | |
tree | 2526feaa8293ca858cc9cb3c9f66a567eeb4b9f4 /app/services | |
parent | 466beeb31f9ede870f1e6f4e85642a375663eaf2 (diff) |
Move most of MetricsController logic to MetricsService
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/metrics_service.rb | 38 |
1 files changed, 38 insertions, 0 deletions
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 |