lib/elastic_apm/config.rb in elastic-apm-0.4.5 vs lib/elastic_apm/config.rb in elastic-apm-0.5.0

- old
+ new

@@ -5,10 +5,13 @@ module ElasticAPM # rubocop:disable Metrics/ClassLength # @api private class Config DEFAULTS = { + config_file: 'config/elastic_apm.yml', + worker_process: false, + server_url: 'http://localhost:8200', secret_token: nil, service_name: nil, service_version: nil, @@ -76,20 +79,23 @@ [:float, 'transaction_sample_rate'], 'ELASTIC_APM_VERIFY_SERVER_CERT' => [:bool, 'verify_server_cert'], 'ELASTIC_APM_TRANSACTION_MAX_SPANS' => [:int, 'transaction_max_spans'] }.freeze - def initialize(options = nil) - options = {} if options.nil? - + def initialize(options = {}) set_defaults - set_from_env + set_from_args(options) + set_from_config_file + set_from_env yield self if block_given? end + attr_accessor :config_file + attr_accessor :worker_process + attr_accessor :server_url attr_accessor :secret_token attr_accessor :service_name attr_accessor :service_version @@ -165,16 +171,20 @@ @logger = logger || build_logger(log_path, log_level) end private - def set_defaults - DEFAULTS.each do |key, value| + def assign(options) + options.each do |key, value| send("#{key}=", value) end end + def set_defaults + assign(DEFAULTS) + end + # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity def set_from_env ENV_TO_KEY.each do |env_key, key| next unless (value = ENV[env_key]) @@ -193,27 +203,31 @@ end end # rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity def set_from_args(options) - options.each do |key, value| - send("#{key}=", value) - end + assign(options) end + def set_from_config_file + return unless File.exist?(config_file) + assign(YAML.load_file(config_file) || {}) + end + def set_sinatra(app) self.service_name = format_name(service_name || app.to_s) self.framework_name = framework_name || 'Sinatra' self.framework_version = framework_version || Sinatra::VERSION self.enabled_injectors += %w[sinatra] self.root_path = Dir.pwd end - def set_rails(app) - self.service_name = format_name(service_name || app.class.parent_name) - self.framework_name = 'Ruby on Rails' - self.framework_version = Rails::VERSION::STRING - self.logger = Rails.logger + def set_rails(app) # rubocop:disable Metrics/AbcSize + self.service_name ||= format_name(service_name || app.class.parent_name) + self.framework_name ||= 'Ruby on Rails' + self.framework_version ||= Rails::VERSION::STRING + self.logger ||= Rails.logger + self.root_path = Rails.root.to_s self.view_paths = app.config.paths['app/views'].existent end def build_logger(path, level)