From d8045610cbb004996cb92c79c153f5af89fc775b Mon Sep 17 00:00:00 2001 From: Jack Rothrock Date: Wed, 1 May 2024 17:44:59 -0600 Subject: [PATCH] Fix lint --- .rubocop.yml | 15 +++++++ lib/scout_apm/logging/config.rb | 44 ++++++++++--------- lib/scout_apm/logging/context.rb | 25 ++++++----- lib/scout_apm/logging/logger.rb | 8 ++-- lib/scout_apm/logging/monitor/monitor.rb | 4 +- .../logging/monitor_manager/manager.rb | 6 +-- spec/scout_apm_logging_spec.rb | 2 +- 7 files changed, 61 insertions(+), 43 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 8dc9907..e33acbf 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -30,6 +30,21 @@ Naming/FileName: Style/Documentation: Enabled: true +Naming/PredicateName: + Enabled: false + +Style/ClassVars: + Enabled: false + +Metrics/MethodLength: + Max: 20 + +Lint/LiteralAsCondition: + Enabled: false + +Layout/LineLength: + Max: 130 + Style/PercentLiteralDelimiters: PreferredDelimiters: '%w': "[]" diff --git a/lib/scout_apm/logging/config.rb b/lib/scout_apm/logging/config.rb index 2a49fd4..5e36488 100644 --- a/lib/scout_apm/logging/config.rb +++ b/lib/scout_apm/logging/config.rb @@ -2,54 +2,56 @@ module ScoutApm module Logging + # Holds the configuration values for Scout APM Logging. class Config < ScoutApm::Config - KNOWN_CONFIG_OPTIONS = [ - 'config_file', - 'log_level', - 'log_stderr', - 'log_stdout', - 'log_file_path', - 'log_class', - 'logging_ingest_key', - 'logging_monitor', - 'monitor_pid_file' - ] + KNOWN_CONFIG_OPTIONS = %w[ + config_file + log_level + log_stderr + log_stdout + log_file_path + log_class + logging_ingest_key + logging_monitor + monitor_pid_file + ].freeze SETTING_COERCIONS = { - 'monitor_logs' => BooleanCoercion.new, - } + 'monitor_logs' => BooleanCoercion.new + }.freeze - def self.with_file(context, file_path=nil, config={}) + def self.with_file(context, file_path = nil, config = {}) overlays = [ ConfigEnvironment.new, ConfigFile.new(context, file_path, config), ConfigDefaults.new, - ConfigNull.new, + ConfigNull.new ] new(context, overlays) end + # Defaults in case no config file has been found. class ConfigDefaults DEFAULTS = { 'log_level' => 'info', - 'monitor_pid_file' => '/tmp/scout_apm_log_monitor.pid', + 'monitor_pid_file' => '/tmp/scout_apm_log_monitor.pid' }.freeze - + def value(key) DEFAULTS[key] end - + def has_key?(key) - DEFAULTS.has_key?(key) + DEFAULTS.key?(key) end - # Defaults are here, but not counted as user specified. + # Defaults are here, but not counted as user specified. def any_keys_found? false end def name - "defaults" + 'defaults' end end end diff --git a/lib/scout_apm/logging/context.rb b/lib/scout_apm/logging/context.rb index 2ee17cc..00c17f5 100644 --- a/lib/scout_apm/logging/context.rb +++ b/lib/scout_apm/logging/context.rb @@ -2,6 +2,7 @@ module ScoutApm module Logging + # Contains context around Scout APM logging, such as environment, configuration, and the logger. class Context # Initially start up without attempting to load a configuration file. We # need to be able to lookup configuration options like "application_root" @@ -9,7 +10,7 @@ class Context # located # # Later in initialization, we set config= to include the file. - def initialize() + def initialize @logger = LoggerFactory.build_minimal_logger end @@ -27,29 +28,29 @@ def logger def config=(config) @config = config - + @logger = nil - + # TODO # log_configuration_settings end end + # Create a logger based on the configuration settings. class LoggerFactory def self.build(config, environment) ScoutApm::Logging::Logger.new(environment.root, - { - :log_level => config.value('log_level'), - :log_file_path => config.value('log_file_path'), - :stdout => config.value('log_stdout') || environment.platform_integration.log_to_stdout?, - :stderr => config.value('log_stderr'), - :logger_class => config.value('log_class'), - } - ) + { + log_level: config.value('log_level'), + log_file_path: config.value('log_file_path'), + stdout: config.value('log_stdout') || environment.platform_integration.log_to_stdout?, + stderr: config.value('log_stderr'), + logger_class: config.value('log_class') + }) end def self.build_minimal_logger - ScoutApm::Logging::Logger.new(nil, :stdout => true) + ScoutApm::Logging::Logger.new(nil, stdout: true) end end end diff --git a/lib/scout_apm/logging/logger.rb b/lib/scout_apm/logging/logger.rb index 833fc4d..3d6d37e 100644 --- a/lib/scout_apm/logging/logger.rb +++ b/lib/scout_apm/logging/logger.rb @@ -2,23 +2,23 @@ module ScoutApm module Logging + # Custom logger for ScoutApm Logging. class Logger < ScoutApm::Logger - private def determine_log_destination case true when stdout? - STDOUT + $stdout when stderr? - STDERR + $stderr when validate_path(@opts[:log_file]) @opts[:log_file] when validate_path("#{log_file_path}/scout_apm_logging.log") "#{log_file_path}/scout_apm_logging.log" else # Safe fallback - STDOUT + $stdout end end end diff --git a/lib/scout_apm/logging/monitor/monitor.rb b/lib/scout_apm/logging/monitor/monitor.rb index b77d632..f8ee27e 100644 --- a/lib/scout_apm/logging/monitor/monitor.rb +++ b/lib/scout_apm/logging/monitor/monitor.rb @@ -24,7 +24,7 @@ def self.instance def initialize @context = ScoutApm::Logging::Context.new - context.config = ScoutApm::Logging::Config.with_file(context, context.config.value("config_file")) + context.config = ScoutApm::Logging::Config.with_file(context, context.config.value('config_file')) end def setup! @@ -44,7 +44,7 @@ def run! def add_exit_handler at_exit do - File.delete(context.config.value("monitor_pid_file")) + File.delete(context.config.value('monitor_pid_file')) end end end diff --git a/lib/scout_apm/logging/monitor_manager/manager.rb b/lib/scout_apm/logging/monitor_manager/manager.rb index 9399272..5b6908e 100644 --- a/lib/scout_apm/logging/monitor_manager/manager.rb +++ b/lib/scout_apm/logging/monitor_manager/manager.rb @@ -14,7 +14,7 @@ def self.instance def initialize @context = ScoutApm::Logging::Context.new - context.config = ScoutApm::Logging::Config.with_file(context, context.config.value("config_file")) + context.config = ScoutApm::Logging::Config.with_file(context, context.config.value('config_file')) end def setup! @@ -22,12 +22,12 @@ def setup! end def create_process - return if File.exist? context.config.value("monitor_pid_file") + return if File.exist? context.config.value('monitor_pid_file') gem_directory = File.expand_path('../../../..', __dir__) daemon_process = Process.spawn("ruby #{gem_directory}/bin/scout_apm_logging_monitor", pgroup: true) - File.write(context.config.value("monitor_pid_file"), daemon_process) + File.write(context.config.value('monitor_pid_file'), daemon_process) # TODO: Add exit handlers? end diff --git a/spec/scout_apm_logging_spec.rb b/spec/scout_apm_logging_spec.rb index ca39911..a23e8e9 100644 --- a/spec/scout_apm_logging_spec.rb +++ b/spec/scout_apm_logging_spec.rb @@ -11,7 +11,7 @@ end it 'checks lifecycle of the daemon process' do - pid_file = ScoutApm::Logging::MonitorManager.instance.context.config.value("monitor_pid_file") + pid_file = ScoutApm::Logging::MonitorManager.instance.context.config.value('monitor_pid_file') # Check if the PID file exists expect(File.exist?(pid_file)).to be_truthy