summaryrefslogtreecommitdiff
path: root/lib/gitlab/prometheus/additional_metrics_parser.rb
blob: cb95daf22602b68b3aea27aeab7aabe0508cee06 (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
module Gitlab
  module Prometheus
    module AdditionalMetricsParser
      extend self

      def load_groups_from_yaml
        additional_metrics_raw.map(&method(:group_from_entry))
      end

      private

      def validate!(obj)
        raise ParsingError.new(obj.errors.full_messages.join('\n')) unless obj.valid?
      end

      def group_from_entry(entry)
        entry[:name] = entry.delete(:group)
        entry[:metrics]&.map! do |entry|
          Metric.new(entry).tap(&method(:validate!))
        end

        MetricGroup.new(entry).tap(&method(:validate!))
      end

      def additional_metrics_raw
        load_yaml_file&.map(&:deep_symbolize_keys).freeze
      end

      def load_yaml_file
        @loaded_yaml_file ||= YAML.load_file(Rails.root.join('config/prometheus/additional_metrics.yml'))
      end
    end
  end
end