diff --git a/config/initializers/log_slow_sql_queries.rb b/config/initializers/log_slow_sql_queries.rb index 6bd3f31882e..324c9cd9a2f 100644 --- a/config/initializers/log_slow_sql_queries.rb +++ b/config/initializers/log_slow_sql_queries.rb @@ -42,8 +42,8 @@ Rails.application.configure do # Skip transaction that may be blocked next if data[:sql].match?(/BEGIN|COMMIT/) - # Skip tenant creation (load dump) - next if data[:sql][..120].include?("Dumped by pg_dump") + # Skip tenant creation (load dump sql which is around 300kB) + next if data[:sql][..120].include?("Dumped by pg_dump") || data[:sql].size > 200_000 # Skip smaller durations duration = ((finish - start) * 1000).round(4) diff --git a/lib/open_project/logging/log_delegator.rb b/lib/open_project/logging/log_delegator.rb index c1aac281b32..364bfd539a8 100644 --- a/lib/open_project/logging/log_delegator.rb +++ b/lib/open_project/logging/log_delegator.rb @@ -5,7 +5,7 @@ module OpenProject ## # Consume a message and let it be handled # by all handlers - def log(exception, context = {}) + def log(exception, context = {}) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity # in case we're getting ActionController::Parameters context = if context.respond_to?(:to_unsafe_h) context.to_unsafe_h @@ -28,9 +28,11 @@ module OpenProject # Set current contexts context[:level] ||= context[:exception] ? :error : :info - context[:current_user] ||= User.current + if the_current_user = current_user + context[:current_user] ||= the_current_user + end - registered_handlers.values.each do |handler| + registered_handlers.each_value do |handler| handler.call message, context rescue StandardError => e Rails.logger.error "Failed to delegate log to #{handler.inspect}: #{e.inspect}\nMessage: #{message.inspect}" @@ -103,6 +105,14 @@ module OpenProject extended = OpenProject::Logging.extend_payload!(payload, context) OpenProject::Logging.formatter.call extended end + + def current_user + User.current + # Probably more performant to rescue "ActiveRecord::StatementInvalid: PG::UndefinedTable" + # than to check for table existence with `User.connection.data_source_exists?(User.table_name)` + rescue StandardError + nil + end end end end