debian-mirror-gitlab/lib/gitlab/prometheus/additional_metrics_parser.rb

46 lines
1.2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
module Prometheus
module AdditionalMetricsParser
2019-12-04 20:38:33 +05:30
CONFIG_ROOT = 'config/prometheus'
2018-03-27 19:54:05 +05:30
MUTEX = Mutex.new
2017-09-10 17:25:29 +05:30
extend self
2018-11-20 20:47:30 +05:30
def load_groups_from_yaml(file_name)
2018-03-27 19:54:05 +05:30
yaml_metrics_raw(file_name).map(&method(:group_from_entry))
2017-09-10 17:25:29 +05:30
end
private
def validate!(obj)
2021-06-08 01:23:25 +05:30
raise ParsingError, obj.errors.full_messages.join('\n') unless obj.valid?
2017-09-10 17:25:29 +05:30
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
2018-03-27 19:54:05 +05:30
def yaml_metrics_raw(file_name)
load_yaml_file(file_name)&.map(&:deep_symbolize_keys).freeze
2017-09-10 17:25:29 +05:30
end
2018-03-27 19:54:05 +05:30
# 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
2017-09-10 17:25:29 +05:30
end
2018-03-27 19:54:05 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-09-10 17:25:29 +05:30
end
end
end