Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metrics_service.rb « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 350c3639e9295ff44c843cc3f5ac790c10c5c836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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