Class: PhobosPrometheus::ConfigParser

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/phobos_prometheus/config_parser.rb

Overview

Config validates and parses configuration yml

Constant Summary collapse

ROOT_MISSING_COLLECTORS =
'No histograms, gauges nor counters are configured. ' \
'Metrics will not be recorded'
ROOT_INVALID_KEY =
'Invalid configuration option detected at root level, ignoring'
ROOT_KEYS =
[:metrics_prefix, :counters, :histograms, :buckets, :gauges].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log_error, #log_info, #log_warn

Constructor Details

#initialize(path) ⇒ ConfigParser

Returns a new instance of ConfigParser.



161
162
163
164
165
166
167
168
# File 'lib/phobos_prometheus/config_parser.rb', line 161

def initialize(path)
  @config = Helper.read_config(path)
  validate_config
  @config.counters = [] unless @config.counters
  @config.histograms = [] unless @config.histograms
  @config.gauges = [] unless @config.gauges
  @config.freeze
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



154
155
156
# File 'lib/phobos_prometheus/config_parser.rb', line 154

def config
  @config
end

Instance Method Details

#assert_required_root_keysObject



200
201
202
203
# File 'lib/phobos_prometheus/config_parser.rb', line 200

def assert_required_root_keys
  @config.counters || @config.histograms || @config.gauges || \
    log_warn(ROOT_MISSING_COLLECTORS)
end

#validate_bucketsObject



192
193
194
# File 'lib/phobos_prometheus/config_parser.rb', line 192

def validate_buckets
  BucketsValidator.new(@config.to_h[:buckets] || []).validate
end

#validate_configObject



170
171
172
173
174
175
176
# File 'lib/phobos_prometheus/config_parser.rb', line 170

def validate_config
  validate_root
  validate_counters
  validate_histograms
  validate_buckets
  validate_gauges
end

#validate_countersObject



184
185
186
# File 'lib/phobos_prometheus/config_parser.rb', line 184

def validate_counters
  CountersValidator.new(@config.to_h[:counters] || []).validate
end

#validate_gaugesObject



196
197
198
# File 'lib/phobos_prometheus/config_parser.rb', line 196

def validate_gauges
  GaugesValidator.new(@config.to_h[:gauges] || []).validate
end

#validate_histogramsObject



188
189
190
# File 'lib/phobos_prometheus/config_parser.rb', line 188

def validate_histograms
  HistogramsValidator.new(@config.to_h[:histograms] || [], @config.buckets).validate
end

#validate_rootObject



178
179
180
181
182
# File 'lib/phobos_prometheus/config_parser.rb', line 178

def validate_root
  assert_required_root_keys
  Helper.check_invalid_keys(ROOT_KEYS, @config.to_h) || \
    log_warn(ROOT_INVALID_KEY)
end