Read about our upcoming Code of Conduct on this issue

additional_metrics_parser.rb 1.21 KB
Newer Older
1
2
3
module Gitlab
  module Prometheus
    module AdditionalMetricsParser
4
5
      CONFIG_ROOT = 'config/prometheus'.freeze
      MUTEX = Mutex.new
6
7
      extend self

8
      def load_groups_from_yaml(file_name)
9
        yaml_metrics_raw(file_name).map(&method(:group_from_entry))
10
11
12
13
      end

      private

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

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

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

27
28
      def yaml_metrics_raw(file_name)
        load_yaml_file(file_name)&.map(&:deep_symbolize_keys).freeze
29
30
      end

31
32
33
34
35
36
37
38
      # rubocop:disable Gitlab/ModuleWithInstanceVariables
      def load_yaml_file(file_name)
        return YAML.load_file(Rails.root.join(CONFIG_ROOT, file_name)) if Rails.env.development?

        MUTEX.synchronize do
          @loaded_yaml_cache ||= {}
          @loaded_yaml_cache[file_name] ||= YAML.load_file(Rails.root.join(CONFIG_ROOT, file_name))
        end
39
      end
40
      # rubocop:enable Gitlab/ModuleWithInstanceVariables
41
42
43
    end
  end
end