lib/elastic_apm/config.rb in elastic-apm-0.2.0 vs lib/elastic_apm/config.rb in elastic-apm-0.3.0

- old
+ new

@@ -1,10 +1,11 @@ # frozen_string_literal: true require 'logger' module ElasticAPM + # rubocop:disable Metrics/ClassLength # @api private class Config DEFAULTS = { server_url: 'http://localhost:8200', secret_token: nil, @@ -14,81 +15,102 @@ framework_name: nil, framework_version: nil, log_path: '-', log_level: Logger::INFO, + logger: nil, - timeout: 10, - open_timeout: 10, + http_timeout: 10, + http_open_timeout: 10, transaction_send_interval: 10, debug_transactions: false, debug_http: false, - enabled_injectors: %w[net_http], + enabled_injectors: %w[net_http json], - current_user_method: :current_user, current_user_id_method: :id, current_user_email_method: :email, current_user_username_method: :username, - view_paths: [] + view_paths: [], + root_path: Dir.pwd }.freeze - LOCK = Mutex.new + ENV_TO_KEY = { + 'ELASTIC_APM_APP_NAME' => 'app_name', + 'ELASTIC_APM_SERVER_URL' => 'server_url', + 'ELASTIC_APM_SECRET_TOKEN' => 'secret_token' + }.freeze + # rubocop:disable Metrics/MethodLength def initialize(options = nil) options = {} if options.nil? - DEFAULTS.merge(options).each do |key, value| + # Start with the defaults + DEFAULTS.each do |key, value| send("#{key}=", value) end - return unless block_given? + # Set options from ENV + ENV_TO_KEY.each do |env_key, key| + next unless (value = ENV[env_key]) + send("#{key}=", value) + end - yield self + # Set options from arguments + options.each do |key, value| + send("#{key}=", value) + end + + yield self if block_given? end + # rubocop:enable Metrics/MethodLength attr_accessor :server_url attr_accessor :secret_token attr_accessor :app_name - attr_writer :environment + attr_reader :environment attr_accessor :framework_name attr_accessor :framework_version attr_accessor :log_path attr_accessor :log_level - attr_accessor :timeout - attr_accessor :open_timeout + attr_accessor :http_timeout + attr_accessor :http_open_timeout attr_accessor :transaction_send_interval attr_accessor :debug_transactions attr_accessor :debug_http attr_accessor :enabled_injectors attr_accessor :view_paths + attr_accessor :root_path attr_accessor :current_user_method attr_accessor :current_user_id_method attr_accessor :current_user_email_method attr_accessor :current_user_username_method - attr_writer :logger + attr_reader :logger # rubocop:disable Metrics/MethodLength, Metrics/AbcSize def app=(app) case app_type?(app) when :sinatra self.app_name = format_name(app_name || app.to_s) self.framework_name = 'Sinatra' self.framework_version = Sinatra::VERSION + self.enabled_injectors += %w[sinatra] + self.root_path = Dir.pwd when :rails self.app_name = format_name(app_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 else # TODO: define custom? self.app_name = 'ruby' end @@ -105,23 +127,20 @@ end nil end - def environment - @environment ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] + def use_ssl? + server_url.start_with?('https') end - def logger - @logger ||= - LOCK.synchronize do - build_logger(log_path, log_level) - end + def environment=(env) + @environment = env || ENV['RAILS_ENV'] || ENV['RACK_ENV'] end - def use_ssl? - server_url.start_with?('https') + def logger=(logger) + @logger = logger || build_logger(log_path, log_level) end private def build_logger(path, level) @@ -132,6 +151,7 @@ def format_name(str) str.gsub('::', '_') end end + # rubocop:enable Metrics/ClassLength end